-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
106 lines (90 loc) · 3.89 KB
/
build.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
/*
* @Author: qin yang
* @Date: 2016-12-09 09:51:37
* @Last Modified by: qinyang
* @Last Modified time: 2017-04-08 08:48:31
*/
require('babel-core/register');
var config = require('./.config.json');
var yargs = require('yargs');
var Util_File = require('./dev/utils/file');
var child_process = require('child_process');
var bearychat = require('./bearychat.js');
var jenkinsInfo = require('./decodeJenkinsInfo.js');
var bearychatUser = config.bearychatUser;
var package = require('./package.json');
var path = require('path');
var fs = require('fs');
var type,
distPath = './dist',
distDeployPath = './distDeploy',
spawnSync = child_process.spawnSync,
indexHtmlPaths = [];
var gruntResult = spawnSync('npm', ['run', 'grunt-build'], { stdio: 'inherit' });
if (gruntResult.status) {
bearychat({ text: 'grunt打包失败 分支:' + jenkinsInfo.branch, user: bearychatUser });
process.exit(1);
}
bearychat({ text: '√ 成功生成打包文件 分支:' + jenkinsInfo.branch, user: bearychatUser });
var copyResult = spawnSync('cp', ['-Rf', distPath, distDeployPath], { stdio: 'inherit' });
if (copyResult.status) {
bearychat({ text: '复制dist文件失败 分支:' + jenkinsInfo.branch, user: bearychatUser });
process.exit(1);
}
var fileList = Util_File.readFileList(distDeployPath) || [];
var gzipType = ['js', 'css'];
var gzipFileMap = {};
fileList.forEach((item) => {
if (gzipType.indexOf(item.type) >= 0 && !item.isGzip) {
var fBefore = new Util_File(item.path);
var md5Before = fBefore.file.getMd5();
var oldPath = item.path;
var mapPath = oldPath + '.map';
var newPath = oldPath.replace('.' + item.type, '.' + md5Before.slice(0, 10) + '.' + item.type);
var newPathDetail = path.parse(newPath);
var newOriginalPath = path.resolve(newPath, '../original-sources/' + newPathDetail.base);
spawnSync('mkdir', ['-p', path.resolve(newPath, '../original-sources')]);
// 如果原来的路径和gzip之后的路径一样,则需要先重命名原文件
if (oldPath === newPath) {
var _oldPath = path.resolve(oldPath, '../_' + fBefore.file.name);
fBefore.file.rename(_oldPath);
oldPath = _oldPath;
}
// // 在有source map的源文件末尾添加上source map 链接
// var fContent = fBefore.file.read();
// fContent += '\n' + '//# sourceMappingURL=' + `${newPathDetail.base}.map`;
// fBefore.file.write(fContent);
// 进行gzip压缩,并重命名源码文件
var gzipResult = spawnSync('gzip', ['-9', '-c', oldPath], { stdio: ['inherit', fs.openSync(`${oldPath}.gz`, 'w'), 'inherit'] });
if (gzipResult.status) {
bearychat({ text: 'gzip压缩失败, 文件:' + fBefore.file.name + ', 分支:' + jenkinsInfo.branch, user: bearychatUser });
process.exit(1);
}
var gzPath = oldPath + '.gz';
var f = new Util_File(gzPath);
f.file.rename(newPath);
gzipFileMap[item.name] = f.file.name;
// // 重命名.map文件
// var fm = new Util_File(mapPath);
// fm.file.rename(newPath + '.map');
// 重命名原来的source文件,全部放到 original-sources 文件夹下
fBefore.file.rename(newOriginalPath);
}
});
indexHtmlPaths = fileList.filter(function (item) {
return item.name === 'index.html';
});
indexHtmlPaths.forEach(function (item) {
var indexHtmlPath = item.path;
var indexHtml = new Util_File(indexHtmlPath);
var indexContent = indexHtml.file.read();
for (var before in gzipFileMap) {
var after = gzipFileMap[before];
indexContent = indexContent.replace(new RegExp(before, 'g'), after);
}
indexHtml.file.write(indexContent);
var versionFile = path.resolve(indexHtmlPath, '../backup/index.' + package.version + '.' + new Date().getTime() + '.html');
spawnSync('mkdir', [path.resolve(indexHtmlPath, '../backup')]);
spawnSync('cp', [indexHtmlPath, versionFile], { stdio: 'inherit' });
});
process.exit(0);