-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (73 loc) · 3.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var fs = require('fs'),
path = require('path'),
_ = require('underscore'),
hfs = require('mass_fs');
var copyDir = function (opts) {
// 设置参数
opts = _.extend({
rootPath: '', // 根目录
filePathList: [], // 文件路径列表
createPath: './', // 生成的文件路径
isLog: true, // 是否打印日志
isTimeStamp: false, // 是否给html的js引用添加时间戳
callBack: '' // 回调函数
}, opts);
var fileLength = opts.filePathList; // 文件数量
// 过滤掉`rootPath`, 生成目标目录结构
var filePathList = opts.filePathList.map(function (str) {
return {
oldFilePath: str,
newFilePath: path.join(path.resolve(opts.createPath), str.replace(opts.rootPath, ''))
}
});
// 打印日志
if (opts.isLog) {
console.log('根目录:\n' + opts.rootPath + '\n');
console.log('目标目录结构:');
console.log(filePathList);
}
// 创建文件
filePathList.forEach(function (obj) {
fs.exists(obj.oldFilePath, function(exists){
if(!exists){return;}
var stat = fs.lstatSync(obj.oldFilePath);
if(stat.isDirectory()){return;}
hfs.mkdir(path.dirname(obj.newFilePath), function () {
if (path.basename(obj.newFilePath).indexOf('.html') > 0) {
var data = fs.readFileSync(obj.oldFilePath).toString(),
timeMap = (new Date()).getTime(),
seaConfigReg = /<script id="seaConfig">(.+)<\/script>\n/ig,
seaConfigStr = 'seajs.config({\'map\': [[ /^(.*\.(?:css|js))(.*)$/i, \'$1?t=' + timeMap + '\' ] ] });',
imgTimeStampReg = /([url\(|src\=][\'|\"]?.*\.png|gif|jpg)(\?t=\d+)?([\'|\"]?)/ig,
cssTimeStampReg = /(<link.*href=[\'|\"].*\.css)(\?t=\d+)?([\'|\"]>)/ig,
jsTimeStampReg = /(<script.*src=[\'|\"].*\.js)(\?t=\d+)?([\'|\"]>)/ig;
// 给seajs加载的模块加时间戳
data = data.replace(seaConfigReg, '');
data = data.replace('<script src="${webroot}${theme_dir}/script/sea.js"></script>', '<script src="${webroot}${theme_dir}/script/sea.js"></script>\n<script id="seaConfig">' + seaConfigStr + '</script>');
data = data.replace('<script src="${webroot}${theme_dir}/mobile/script/sea.js"></script>', '<script src="${webroot}${theme_dir}/mobile/script/sea.js"></script>\n<script id="seaConfig">' + seaConfigStr + '</script>');
// 给页内的外链加时间戳
[imgTimeStampReg, cssTimeStampReg, jsTimeStampReg].forEach(function (reg) {
data = data.replace(reg, '$1?t=' + timeMap + '$3');
});
// 创建该文件
hfs.writeFile(obj.newFilePath, data);
} else if(path.basename(obj.newFilePath).indexOf('.css') > 0){
var data = fs.readFileSync(obj.oldFilePath).toString(),
timeMap = (new Date()).getTime(),
imgTimeStampReg = /([url\(|src\=][\'|\"]?.*\.png|gif|jpg)(\?t=\d+)?([\'|\"]?)/ig;
data = data.replace(imgTimeStampReg, '$1?t=' + timeMap + '$3');
// 创建该文件
hfs.writeFile(obj.newFilePath, data);
} else {
fs.createReadStream(obj.oldFilePath).pipe(fs.createWriteStream(obj.newFilePath));
}
fileLength--;
if (fileLength == 0) {
opts.isLog && console.log('\n*^_^*\ncopy完成');
typeof opts.callBack == "function" && opts.callBack();
}
});
})
});
};
module.exports = copyDir;