-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (203 loc) · 8.15 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
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
/*
* @Author: zhangx
* @Date: 2019-04-29 14:51:55
* @Last Modified by: zhangx
* @Last Modified time: 2019-07-31 14:59:12
* @file: webpack插件-拷贝编译完成的HTML至发布项目中
*/
const fs = require('fs');
const path = require('path');
const colors = require('colors');
const mkdirs = require('jm-mkdirs');
const exec = require('child_process').exec;
class WebpackJpushPublishPlugin {
constructor(options) {
this.options = Object.assign({},options);
}
apply(compiler) {
let {
filter = /^.*\.html$/,
dir = 'publish',
retry = 3,
version,
gitLab,
env,
} = this.options;
let root = path.resolve(__dirname).replace(/node_modules.*$/,'');
/**
* git切换分支,提交操作
* @param {*} compilation webpacke编译类
* @param {*} callback 插件任务完成后的回调
*/
const publishProject = (compilation, callback) => {
gitClone(compilation, callback);
}
/**
* git切换分支,提交操作
* @param {*} compilation webpacke编译类
* @param {*} callback 插件任务完成后的回调
*/
const gitClone = (compilation, callback) => {
const publish = path.resolve(root, 'publish')
if(!fs.existsSync(publish)){
exec(`git clone ${gitLab} publish`, {
cwd: root
}, function (error, stdout, stderr) {
gitCommit(compilation, callback);
})
} else {
gitCommit(compilation, callback);
}
}
/**
* git切换分支,提交操作
* @param {*} compilation webpacke编译类
* @param {*} callback 插件任务完成后的回调
*/
const gitCommit = (compilation, callback) => {
let cwdPath = path.resolve(root, 'publish');
//分支切换完成,移动复制打包代码
copyFiles(compilation, () => {
exec(`git fetch origin ${env} && git checkout ${env} && git merge FETCH_HEAD`, {
cwd: cwdPath
}, function (error, stdout, stderr) {
if (error) {
console.log(colors.red.underline(error));
callback();
return
} else {
console.log(colors.yellow.underline('当前分支:' + env));
exec(`git add . && git commit -m 'auto-${env}-git-${version}'`, {
cwd: cwdPath
}, function (error, stdout, stderr) {
if (error) {
console.log(colors.red.underline(error));
callback();
return
} else {
console.log(colors.green('commit成功'));
exec("git push origin " + env, {
cwd: cwdPath
}, function (error, stdout, stderr) {
if (error) {
console.log(colors.red.underline(error));
callback();
return
} else {
console.log(colors.green('push成功'));
callback();
}
})
}
})
}
})
})
}
/**
* 拷贝文件到publish文件加
* @param {*} compilation webpacke编译类
* @param {*} callback 插件任务完成后的回调
*/
const copyFiles = (compilation, callback) => {
let assets = compilation.assets;
let filesNames = Object.keys(assets);
let _files = filesNames.filter(e => filter.test(e));
let _failedFiles = [];
let _retryCount = 0;
// 单个文件移动复制函数
const _copyFile = (name) => {
return new Promise((resolve, reject) => {
let names = name.split(/[\\\/]/);
names.pop();
mkdirs.sync(path.resolve(root + dir + names.join('/')));
fs.writeFile(
path.resolve(root + dir + name),
assets[name]['source'](), 'utf8',
err => {
let fi = _failedFiles.indexOf(name);
if(err) {
console.log('\n', colors.red('----' + name + ':放置失败----'), '\n', err, '\n')
if(fi == -1){
_failedFiles.push(name);
}
reject(err);
} else {
console.log('\n', colors.green('----' + name + ':成功放置Pulish----'), '\n')
if(fi != -1){
_failedFiles.splice(fi,1);
}
resolve()
}
}
)
})
}
// 文件移动复制进程
const _cpoyProcess = () => {
return Promise.all(
_files.map(e => _copyFile(e)))
.then(rs => {
Promise.resolve(rs);
},
_finish
)
}
// 文件复制失败重试 最多重试{retry}次数
const _retryProcess = (err) => {
++_retryCount;
if (err && _retryCount > retry) {
console.log('\n');
return Promise.reject(err);
} else if(_failedFiles.length && _retryCount <= retry){
return Promise.all(_failedFiles.map(e => _copyFile(e)))
.then(rs => Promise.resolve(rs), _retryProcess);
} else {
return Promise.resolve();
}
}
// 复制完成
const _finish = (err) => {
if(err){
console.log('\n', colors.red.underline(err), '\n');
} else {
console.log('\n', colors.green('所有HTML成功移入发布项目'), '\n');
}
callback(err);
};
/**
* 清空文件夹
* @param {*} path 清空路径
*/
const _delDir = (path) => {
let files = [];
if(fs.existsSync(path)){
files = fs.readdirSync(path);
files.forEach((file, index) => {
let curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()){
_delDir(curPath); //递归删除文件夹
} else {
fs.unlinkSync(curPath); //删除文件
}
});
fs.rmdirSync(path);
}
}
// 清空文件夹
_delDir(path.resolve(root + dir));
// 拷贝进程开始
_cpoyProcess().then((rs) => {
return _retryProcess();
}, _retryProcess).then(() => {
_finish()
}, _finish)
}
if (compiler.hooks) { // For webpack >= 4
compiler.hooks.afterEmit.tapAsync('gitOperate', publishProject);
} else { // For webpack < 4
compiler.plugin('after-emit', publishProject);
}
}
}
module.exports = WebpackJpushPublishPlugin;