-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·457 lines (389 loc) · 10.8 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
const fs = require('fs-extra');
const path = require('path');
const crypto = require('crypto');
const glob = require('glob');
const moment = require('moment');
const chalk = require('chalk');
const axios = require('axios');
const core = require('@actions/core');
const asyncPool = require('tiny-async-pool');
const COS = require('cos-nodejs-sdk-v5');
const CloudBase = require('@cloudbase/manager-node')
let secretId = core.getInput('secretId');
let secretKey = core.getInput('secretKey');
let envId = core.getInput('envId');
let staticSrcPath = core.getInput('staticSrcPath');
let staticDestPath = core.getInput('staticDestPath');
let bucket = core.getInput('bucket');
let region = core.getInput('region');
let isForce = core.getInput('isForce') || false;
let skipFiles = core.getInput('skipFiles') || [];
let forceFiles = core.getInput('forceFiles') || [];
let assetFileName = core.getInput('assetFileName') || 'docschina-assets.json';
let workspace = process.env.GITHUB_WORKSPACE;
if (!process.env.CI) {
const config = require('./config/index');
secretId = config.secretId;
secretKey = config.secretKey;
envId = config.envId;
staticSrcPath = config.staticSrcPath;
staticDestPath = config.staticDestPath;
bucket = config.bucket;
region = config.region;
isForce = config.isForce;
skipFiles = config.skipFiles;
forceFiles = config.forceFiles;
workspace = __dirname;
}
const assetJsonFile = path.join(workspace, assetFileName);
const cos = new COS({
SecretId: secretId,
SecretKey: secretKey,
});
const md5 = (str) => {
let md5sum = crypto.createHash('md5');
md5sum.update(str);
let content = md5sum.digest('hex');
return content;
};
const getObject = async () => {
return new Promise((resolve, reject) => {
cos.getObject(
{
Bucket: bucket /* 必须 */,
Region: region /* 必须 */,
Key: assetFileName /* 必须 */,
Output: fs.createWriteStream(assetJsonFile),
},
function (err, data) {
// console.log(err || data);
if (err) {
console.log(err);
fs.unlinkSync(assetJsonFile);
resolve(err);
} else {
console.log(data);
resolve(data);
}
}
);
});
};
/**
* 将 html 文件放到最末尾上传
* @param {Array} files
*/
const appendHtmlFiles = function (files) {
let htmlFiles = [];
let cdnFiles = [];
files.forEach((item) => {
if (path.extname(item) === '.html') {
htmlFiles.push(item);
} else {
cdnFiles.push(item);
}
});
cdnFiles = cdnFiles.concat(htmlFiles);
return cdnFiles;
};
/**
* 输出日志
* @param {*} result
* @param {*} action
*/
const logTimeResult = function (result, action = null) {
let msg = `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${result}`;
let color = null;
let map = {
error: 'red',
info: 'cyan',
success: 'green',
warn: 'yellow',
};
if (action) {
color = map[action] || null;
}
if (!color) {
core.debug(msg);
} else {
core.debug(chalk[color](msg));
}
};
const sliceUploadFileLimit = (param) => {
return sliceUploadFile(param.uploadOption);
};
/**
* 上传文件
* @param {*} cos
* @param {*} options
*/
const sliceUploadFile = function (options) {
return new Promise((resolve, reject) => {
cos.sliceUploadFile(options, (err, info) => {
if (err) {
resolve({
code: 1,
data: err,
});
} else {
resolve({
code: 0,
data: info,
});
}
});
});
};
const filterFilesByCondition = ({
scanFiles,
skipFiles,
forceFiles,
codePath,
assetJsonMap,
}) => {
let filterFiles = scanFiles.filter((file) => {
if (Array.isArray(skipFiles)) {
// 手动设置跳过的文件
for (let i = 0, len = skipFiles.length; i < len; i++) {
if (skipFiles[i] !== '' && file.indexOf(skipFiles[i]) === 0) {
return false;
}
}
}
if (Array.isArray(forceFiles)) {
// 手动设置强制上传的文件
for (let i = 0, len = forceFiles.length; i < len; i++) {
if (forceFiles[i] !== '' && file.indexOf(forceFiles[i]) === 0) {
return true;
}
}
}
let filePath = path.join(codePath, file);
// 剔走目录
let stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return false;
}
// 剔走已经上传的内容
let compareText =
path.extname(file) !== '.html'
? 1
: md5(fs.readFileSync(path.join(codePath, file)));
if (
assetJsonMap.mapv2.hasOwnProperty(file) &&
assetJsonMap.mapv2[file] === compareText
) {
return false;
}
return true;
});
// 将 html 文件放到最后再上传
return appendHtmlFiles(filterFiles);
};
const initCos = async () => {
try {
// let result = await getObject();
let assetJsonMap = {
mapv2: {},
};
// 获取 map 数据
try {
let result = await axios.get(
`http://${bucket}.cos.${region}.myqcloud.com/${assetFileName}`
);
// if (result.statusCode === 200 && !isForce) {
// assetJsonMap.mapv2 = fs.readJSONSync(assetJsonFile).mapv2 || {};
// }
assetJsonMap.mapv2 = result.data.mapv2 || {};
} catch (e) {
core.error(e.stack);
assetJsonMap.mapv2 = {};
}
if (typeof skipFiles === 'string') {
skipFiles = JSON.parse(skipFiles);
}
core.debug(`skipFiles: ${skipFiles}`);
if (typeof forceFiles === 'string') {
forceFiles = JSON.parse(forceFiles);
}
core.debug(`forceFiles: ${forceFiles}`);
let codePath = path.join(workspace, staticSrcPath);
core.debug(`codePath: ${codePath}`);
//收集需要上传的文件,传入数组
let scanFiles = glob.sync('**/**', { cwd: codePath });
core.debug(`scanFiles for ${codePath}: ${scanFiles}`);
// 剔除文件
let files = filterFilesByCondition({
scanFiles,
skipFiles,
forceFiles,
codePath,
assetJsonMap,
});
console.log('======files====:', files);
let uploadActions = [];
files.forEach((file) => {
let filePath = path.join(codePath, file);
let key = staticDestPath ? path.join(staticDestPath, file) : file;
let uploadOption = {
Bucket: bucket,
Region: region,
Key: key,
FilePath: filePath,
};
uploadActions.push({ uploadOption });
});
core.debug(`====start uploading===`);
// 开始上传文件
let incrementalFiles = [];
try {
// let info = await Promise.all(uploadActions);
let info = await asyncPool(10, uploadActions, sliceUploadFileLimit);
info.forEach((result) => {
if (!result.code && result.data && result.data.Location) {
let item = result.data;
logTimeResult(`${item.Location}-${item.statusCode}`);
let splitResult = item.Location.split('/');
let file = splitResult.splice(1, splitResult.length - 1).join('/');
if (path.extname(file) !== '.html') {
assetJsonMap.mapv2[file] = 1;
} else {
let md5Str = md5(fs.readFileSync(path.join(codePath, file)));
assetJsonMap.mapv2[file] = md5Str;
}
incrementalFiles.push(file);
} else {
result && result.data && console.log(result.data);
}
});
core.setOutput(
'deployResult',
'success: ' + JSON.stringify(incrementalFiles)
);
core.debug(`====finish uploading===`);
} catch (e) {
logTimeResult(`${e.Key}-${e.statusCode}-${e.Code}`, 'error');
core.error(e.stack);
core.setFailed(e.message);
}
fs.writeFileSync(assetJsonFile, JSON.stringify(assetJsonMap, 4, null));
await sliceUploadFile({
Bucket: bucket,
Region: region,
Key: assetFileName,
FilePath: assetJsonFile,
});
// if (fs.existsSync(assetJsonFile)) {
// fs.unlinkSync(assetJsonFile);
// }
} catch (e) {
core.error(e.stack);
core.setFailed(e.message);
}
};
/**
*
* initCloudBase
*
*/
const deployHostingFile = (param) => {
return param.cloudbase.hosting.uploadFiles({
localPath: param.filePath, // srcpath
cloudPath: param.key // cloudPath
});
};
async function downloadStorageFile(cloudbase, localPath, cloudPath) {
return cloudbase.storage.downloadFile({
cloudPath,
localPath,
});
}
async function uploadStorageFile(cloudbase, localPath, cloudPath) {
return cloudbase.storage.uploadFile({
localPath,
cloudPath,
});
}
const initCloudBase = async () => {
const cloudbase = CloudBase.init({
secretId,
secretKey,
envId // 云环境 ID,可在腾讯云-云开发控制台获取
})
let assetJsonMap = {
mapv2: {},
};
try {
let result = await downloadStorageFile(cloudbase, assetJsonFile, assetFileName);
} catch (e) {
core.error(e.message);
}
// 获取 map 数据
try {
if (fs.existsSync(assetJsonFile) && !isForce) {
assetJsonMap.mapv2 = require(assetJsonFile).mapv2 || {};
}
} catch (e) {
core.error(e.stack);
assetJsonMap.mapv2 = {};
}
if (typeof skipFiles === 'string') {
skipFiles = JSON.parse(skipFiles);
}
core.debug(`skipFiles: ${skipFiles}`);
if (typeof forceFiles === 'string') {
forceFiles = JSON.parse(forceFiles);
}
core.debug(`forceFiles: ${forceFiles}`);
let codePath = path.join(workspace, staticSrcPath);
core.debug(`codePath: ${codePath}`);
//收集需要上传的文件,传入数组
let scanFiles = glob.sync('**/**', { cwd: codePath });
core.debug(`scanFiles for ${codePath}: ${scanFiles}`);
// 剔除文件
let files = filterFilesByCondition({
scanFiles,
skipFiles,
forceFiles,
codePath,
assetJsonMap,
});
let uploadFiles = [];
files.forEach((file) => {
let filePath = path.join(codePath, file);
let key = staticDestPath ? path.join(staticDestPath, file) : file;
uploadFiles.push({ cloudbase, filePath, key });
});
// 开始上传文件
let incrementalFiles = [];
try {
let result = await asyncPool(5, uploadFiles, deployHostingFile);
files.forEach((file) => {
if (path.extname(file) !== '.html') {
assetJsonMap.mapv2[file] = 1;
} else {
let md5Str = md5(fs.readFileSync(path.join(codePath, file)));
assetJsonMap.mapv2[file] = md5Str;
}
incrementalFiles.push(file);
});
core.setOutput('deployResult', JSON.stringify(incrementalFiles));
} catch (e) {
core.error(e.message);
logTimeResult(`${e.Key}-${e.statusCode}-${e.Code}`, 'error');
core.setFailed(e.message);
}
fs.writeFileSync(assetJsonFile, JSON.stringify(assetJsonMap, 4, null));
await uploadStorageFile(cloudbase, assetJsonFile, assetFileName);
if (fs.existsSync(assetJsonFile)) {
fs.unlinkSync(assetJsonFile);
}
};
// 上传到云开发服务
if (envId) {
initCloudBase().then(() => {});
}
// 上传到腾讯云服务
else {
initCos().then(() => {});
}