start.js
5.7 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* @author PanZhao
* @date 2016年3月17日 下午12:44:06
*/
var fs = require('fs')
,colors = require('colors')
,child_process = require('child_process')
,EventProxy = require('eventproxy')
,parse = require('./parse')
,minifier = require('./minifier')
,crypto = require("crypto");
//不参与的目录
var excludes = ['scheduleApp', 'trafficManage']
,ep = new EventProxy()
,pName = 'bsth_control'
,path = process.cwd()
//根目录
,root = path.substr(0, path.indexOf('\\src\\main'))
,workspace = root.substr(0, root.indexOf('\\' + pName))
//临时目录
,dest = (workspace + '\\' + pName+'@fatso_copy').replace(/\//g,'\\')
,_static = '\\src\\main\\resources\\static'
,_pages = dest + _static + '\\pages';
//创建临时目录
fs.mkdir(dest, function(e){
if(e)
logError('创建临时目录出错,请检查目录 ' + dest + '是否存在');
setTimeout(function(){
ep.emit('mvn-clean');
}, 500);
});
//子进程
var cProcess;
ep.tail('mvn-clean',function(){
ep.emit('copy-project');
//清理target
/*logInfo('mvn clean...');
cProcess = child_process.exec("mvn clean",{cwd: workspace + '\\' + pName},function(error){
if(error)
logError(error);
logSuccess('mvn clean success');
ep.emit('copy-project');
});*/
output(cProcess);
});
//复制项目副本
ep.tail('copy-project',function(){
logInfo('copy project...');
var xcopyCom = 'XCOPY '+ root.replace(/\//g,'\\') + ' ' + dest +' /e /exclude:'+path+'\\exclude.txt';
cProcess = child_process.exec(xcopyCom,{maxBuffer: 5000*1024},function(error){
if(error)
logError(error);
logSuccess('copy project success');
ep.emit('check-js');
});
//output(cProcess);
});
//检查JS
ep.tail('check-js', function(){
ep.emit('minifier-js');
});
//合并压缩JS
ep.tail('minifier-js', function(){
logInfo('handle index.html...');
//先处理首页
ep.emit('handle-index', function(){
//递归处理片段
walk(dest + _static + '\\pages', function(item){
ep.emit('handle-fragment', item);
},
function(){
ep.emit('package-jar');
});
});
});
//打包
ep.tail('package-jar', function(file){
var packageCom = 'mvn clean package -DskipTests';
cProcess = child_process.exec(packageCom,{maxBuffer: 5000*1024, cwd: dest},function(error){
if(error)
logError(error);
logSuccess('mvn package success');
console.log(('成功打包在 ' + dest + '\\target 目录下').cyan);
});
output(cProcess);
});
//处理片段
ep.tail('handle-fragment', function(file){
//要排除的文件
for(var i = 0, ex; ex = excludes[i++];){
if(file.indexOf(_pages + '\\' + ex) != -1)
return false;
}
handleJavascript(file, function(mini, $){
var jsMini;
if(mini.inside)
jsMini = '(function(){' + mini.inside + '\n' + mini.outside + '})();';
else
jsMini = '(function(){' + mini.outside + '})();';
write(file, $.html() + '<script>' + jsMini + '</script>');
});
});
//处理首页
ep.tail('handle-index', function(cb){
var index = dest + _static + '\\index.html';
handleJavascript(index, function(mini, $){
var jsMiniText = mini.inside + mini.outside;
var code = md5(jsMiniText);
fs.open( dest + _static + '\\assets\\js\\' + code + '.js', 'a', function(err, fd){
if(err)
logError(err);
fs.write(fd,jsMiniText, function(){
var ss = $('script');
$(ss[ss.length - 1]).before('<script src="/assets/js/'+code+'.js"></script>\n');
for(var i = 0, s; s = ss[i++];){
$(s).removeAttr('data-exclude')
.removeAttr('flag')
.removeAttr('data-autocephaly');
}
write(index, $.html());
cb && cb();
});
});
});
});
function handleJavascript(item, cb){
//解析页面
var htmlResult = parse.html(item, dest + _static)
,jsArray = htmlResult.jsArray
,$ = htmlResult.$
,scrStr = htmlResult.scriptString;
//合并压缩
var mini = minifier.mergeAndMini(jsArray, scrStr, dest + _static, item);
cb && cb(mini, $);
}
function removeJsLink(s, e){
var newArray = [], flag;
for(var i = 0, si; si = s[i++];){
flag = 0;
for(var j = 0,ei; ei = e[j++];){
if(si.indexOf(ei) != -1){
flag = -1;
break;
}
}
if(flag == 0){
newArray.push(si);
}
}
return newArray;
}
function write(file, text){
fs.writeFile(file, text, function (err) {
if (err){
console.log(err.toString().red);
process.exit();
}
console.log(file.green);
});
}
function md5(text){
return crypto.createHash("md5").update(text).digest("hex");
}
function walk(path ,handleFile, over) {
fs.readdir(path, function(err, files) {
if (err) {
console.log('read dir error'.red);
} else {
files.forEach(function(item) {
var tmpPath = path + '\\' + item;
fs.stat(tmpPath, function(err1, stats) {
if (err1) {
console.log('stat error');
} else {
if (stats.isDirectory()) {
walk(tmpPath, handleFile);
} else if(item.indexOf('.html') != -1){
handleFile(tmpPath);
}
}
})
});
over && over();
}
});
}
function logInfo(t){
console.log(t);
}
function logSuccess(t){
console.log(t.green);
}
function logError(e){
console.log(e.toString().red);
process.exit();
}
function output(cProcess){
//标准输出
cProcess.stdout.on('data', function (s) {
console.log(s);
});
}