ant-design-vue/site/scripts/pushToOSS.js

60 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/* eslint-disable @typescript-eslint/no-var-requires */
const OSS = require('ali-oss');
const path = require('path');
const fs = require('fs');
const accessKeyId = process.env.ALI_OSS_ACCESSKEY;
const accessKeySecret = process.env.ALI_OSS_SECRETKEY;
const client = new OSS({
bucket: 'antdv-beijing',
cname: 'true',
endpoint: 'aliyuncdn.antdv.com',
// region以杭州为例oss-cn-hangzhou其他region按实际情况填写。
region: 'oss-cn-beijing',
// 阿里云主账号AccessKey拥有所有API的访问权限风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维请登录RAM控制台创建RAM账号。
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
});
const assetsPath = path.join(process.cwd(), 'dist', 'assets');
const put = file => {
return new Promise((reslove, reject) => {
client
.put(`v2/assets/${file}`, path.join(assetsPath, file))
.then(res => {
if (res.res.status !== 200) {
console.log(`${res.name} upload failed!`);
reject();
process.exit(500);
} else {
console.log(`${res.name} upload success!`);
reslove();
}
})
.catch(err => {
if (err) {
err && console.log(err);
process.exit(500);
}
});
});
};
async function upload() {
try {
const files = await fs.promises.readdir(assetsPath, {
withFileTypes: true,
});
for (const file of files) {
if (file.isFile()) {
await put(file.name);
}
}
} catch (err) {
console.error(err);
}
}
upload();