forked from chrunlee/addmd5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddmd5.js
143 lines (137 loc) · 3.87 KB
/
addmd5.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//给jsp 或 html 中的css js 添加fileMd5值
var fs = require('fs'),
readline = require('readline'),
path = require('path'),
join = path.join,
extname = path.extname,
async = require('async'),
colors = require('colors'),
url = require('url'),
crypto = require('crypto'),
qs = require('querystring');
function AddMd5(directory,opts){
this.directory = directory;
this.fileList = [];
this.count = 0;
this.extList = opts.extList;//['.jsp','.html','.htm'];
this.exclude = opts.exclude;//['plugins'];
this.compress = opts.compress;
this.replace = opts.replace;//['${basepath}','${staticresdomain}','<%=basepath%>'];//替换字符串
return this;
}
//获得符合条件的文件
AddMd5.prototype.getFiles = function(dirPath){
var thiz = this;
if(fs.existsSync(dirPath)){
var files = fs.readdirSync(dirPath);
if(files && files.length > 0){
for(var i=0,max=files.length;i<max;i++){
var temp = files[i];
var filePath = join(dirPath,temp);
var stats = fs.statSync(filePath);
if(stats.isDirectory() && thiz.exclude.indexOf(temp) < 0){
//继续
thiz.getFiles(filePath);
}else{
var fileExt = extname(filePath).toLowerCase();
if(thiz.extList.indexOf(fileExt) > -1){
//符合
thiz.fileList.push(filePath);
}
}
}
}
}
}
AddMd5.prototype.start = function(){
var thiz = this,
directory = thiz.directory;
thiz.getFiles(directory);
console.log('共计扫描到 '.green+(''+thiz.fileList.length).red+' 个符合条件的文件'.green);
async.mapLimit(thiz.fileList,5,function(item,cb){
thiz.scan(item,cb);
},function(err,value){
console.log('共计替换 '.green+(''+thiz.count).red +' 个链接'.green)
});
};
AddMd5.prototype.scan = function(filePath,callback){
var thiz = this,compress = thiz.compress;
var is = fs.createReadStream(filePath);
var inter = readline.createInterface({input :is});
var strArr = '';
var needWrite = false;
inter.on('line',function(line){
var rst = thiz.checkLine(line);
if(rst){
needWrite = true;
//检查到有内容
//处理并替换
var md5 = rst.md5;
//检索并替换
var arr = /[src|href][\s]*=[\s]*[\"\']?([^\'\"]*)[\'\"]?/i.exec(line);
if(arr && arr.length > 0){
var src = arr[1];
var query = url.parse(src).query;
var src2 = src.indexOf('?') > -1 ? src.substring(0,src.indexOf('?')) : src;
var qsObj = qs.parse(query);
qsObj.v = md5.substring(0,5);
var qsStr = qs.stringify(qsObj);
var newSrc= src2 + '?'+qsStr;
line = line.replace(src,newSrc);
console.log('替换 [ '.green+line.red+' ]'.green);
thiz.count ++ ;
}
}
strArr+=line+(compress ? '' : '\n');
});
inter.on('close',function(){
//重新写入
if(needWrite) fs.writeFileSync(filePath,strArr);
callback(null,null);
})
};
//根据文件路径获得FILEMD5
AddMd5.prototype.getMd5 = function(filePath){
var buffer = fs.readFileSync(filePath);
var md5 = crypto.createHash('md5');
md5.update(buffer);
return md5.digest('hex').toLowerCase();
};
AddMd5.prototype.checkLine = function(str){
//检查字符串是否符合 link script
var thiz = this,replace = thiz.replace,directory = thiz.directory;
str = str.toLowerCase();
var rst = /<script .*?src=\"(.+?)\"/g.exec(str);
var src = '';
var type = '';
if(rst && rst.length > 0){
src = rst[1];
type = 'src';
}
rst = /<link .*?href=\"(.+?)\"/g.exec(str);
if(rst && rst.length > 0 && rst[1].indexOf('favicon') < 0){
src = rst[1];
type = 'href';
}
if(src){
if(replace && replace.length > 0){
for(var i=0,max=replace.length;i<max;i++){
src = src.replace(replace[i],'');
}
}
var srcObj = url.parse(src);
var query = srcObj.query;
var filePath = join(directory,srcObj.pathname);
if(fs.existsSync(filePath)){
var fileMd5 = thiz.getMd5(filePath);
return {
md5 : fileMd5,
query : query,
filePath : srcObj.pathname,
type : type
};
}
}
return null;
};
module.exports = AddMd5;