-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathpushToOSS.js
59 lines (54 loc) · 1.45 KB
/
pushToOSS.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
/* eslint-disable no-console */
/* 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: '4x-antdv',
cname: 'true',
endpoint: '4x-antdv.oss-cn-beijing.aliyuncs.com',
region: 'oss-cn-beijing',
accessKeyId,
accessKeySecret,
});
const assetsPath = path.join(process.cwd(), 'site', 'dist', 'assets');
const put = (target, source) => {
return new Promise((reslove, reject) => {
client
.put(target, source)
.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(`assets/${file.name}`, path.join(assetsPath, file.name));
}
}
await put('index.html', path.join(process.cwd(), 'site', 'dist', 'index.html'));
} catch (err) {
console.error(err);
}
}
upload();