amis/scripts/upload2cdn.js

74 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-06-05 10:27:12 +08:00
const bosSDK = require('bce-sdk-js');
const fs = require('fs');
const walk = require('fs-walk');
const path = require('path');
2020-08-21 16:46:01 +08:00
const ak = process.env.BOS_AK;
const sk = process.env.BOS_SK;
2019-06-05 10:27:12 +08:00
2020-08-21 16:19:26 +08:00
if (!ak || !sk) {
console.error('ak and sk is required for deploy cdn!');
process.exit(1);
2019-06-05 10:27:12 +08:00
}
2020-08-21 16:19:26 +08:00
const config = {
credentials: {
ak,
sk
},
endpoint: 'https://bj.bcebos.com',
bucket: 'bce-cdn'
};
2019-06-05 10:27:12 +08:00
const client = new bosSDK.BosClient(config);
const bucketName = config.bucket || 'bce-cdn';
const prefix = 'fex/';
async function main() {
2020-08-21 16:19:26 +08:00
const folder = 'gh-pages';
const productName = 'amis-gh-pages';
2019-06-05 10:27:12 +08:00
2020-08-21 16:19:26 +08:00
if (!fs.existsSync(folder)) {
throw new Error('文件夹不存在');
}
if (!productName) {
throw new Error('请指定产品名称');
}
2019-06-05 10:27:12 +08:00
2020-08-21 16:19:26 +08:00
const promises = [];
walk.walkSync(folder, (basedir, filename, stat) => {
if (stat.isDirectory() || /\.html$/.test(filename)) {
return;
2019-06-05 10:27:12 +08:00
}
2020-08-21 16:19:26 +08:00
const relativePath = path.relative(folder, basedir);
// 隐藏文件跳过。
if (/^\./.test(relativePath)) {
return;
2019-06-05 10:27:12 +08:00
}
2020-08-21 16:19:26 +08:00
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());
2019-06-05 10:27:12 +08:00
}
2020-08-21 16:19:26 +08:00
main()
.then(() => console.log('Done!'))
.catch(e => console.error(e.message, e.stack));