forked from aisuda/amis-editor-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload2cdn.js
59 lines (48 loc) · 1.5 KB
/
upload2cdn.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
const bosSDK = require('bce-sdk-js');
const fs = require('fs');
const walk = require('fs-walk');
const path = require('path');
const tokenFile = process.argv[2] || '~/bos.credentials';
if (!fs.existsSync(tokenFile)) {
console.error(tokenFile + ' does not exists!');
process.exit(1);
}
const config = JSON.parse(fs.readFileSync(tokenFile, 'utf-8'));
const client = new bosSDK.BosClient(config);
const bucketName = config.bucket || 'bce-cdn';
const prefix = 'fex/';
async function main() {
const folder = 'gh-pages';
const productName = 'amis-editor-gh-pages';
if (!fs.existsSync(folder)) {
throw new Error('文件夹不存在');
}
if (!productName) {
throw new Error('请指定产品名称');
}
const promises = [];
walk.walkSync(folder, (basedir, filename, stat) => {
if (stat.isDirectory() || /\.html$/.test(filename)) {
return;
}
const relativePath = path.relative(folder, basedir);
const objectName = path.join(prefix, productName, relativePath, filename);
promises.push(() =>
client
.putObjectFromFile(bucketName, objectName, path.join(basedir, filename))
.then(
() => console.log(` ==> ${objectName}`),
res => console.error(res)
)
);
});
if (!promises.length) {
console.log('Nothing need to upload');
}
return promises.reduce((preview, current) => {
return preview.then(current);
}, Promise.resolve());
}
main()
.then(() => console.log('Done!'))
.catch(e => console.error(e.message, e.stack));