-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeploy.js
58 lines (45 loc) · 1.52 KB
/
deploy.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
const fs = require('fs')
const path = require('path')
const util = require('util')
const OSS = require('ali-oss')
const config = require('./oss.json')
const promisifyReaddir = util.promisify(fs.readdir)
const promisifyStat = util.promisify(fs.stat)
const ALIOSSKEY = {
region: config.region,
key: config.key,
secret: config.secret,
bucket: config.bucket
}
const client = new OSS({
region: ALIOSSKEY.region,
accessKeyId: ALIOSSKEY.key,
accessKeySecret: ALIOSSKEY.secret,
bucket: ALIOSSKEY.bucket
})
const publicPath = path.resolve(__dirname, './dist/static')
const ossPath = 'blog-cdn'
async function run(proPath = '') {
const oldFiles = await client.list({
marker: 'blog-cdn'
});
if (oldFiles.objects) {
console.log('开始删除旧文件');
const oldFileLists = oldFiles.objects.map(file => file.name);
await client.deleteMulti(oldFileLists);
console.log('删除旧文件已全部删除');
}
const dir = await promisifyReaddir(`${publicPath}${proPath}`);
for (let i = 0; i < dir.length; i++) {
const stat = await promisifyStat(path.resolve(`${publicPath}${proPath}`, dir[i]))
if (stat.isFile()) {
const fileStream = fs.createReadStream(path.resolve(`${publicPath}${proPath}`, dir[i]))
console.log(`上传文件: ${ossPath}${proPath}/${dir[i]}`)
const result = await client.putStream(`${ossPath + proPath}/${dir[i]}`, fileStream)
} else if (stat.isDirectory()) {
await run(`${proPath}/${dir[i]}`)
}
}
console.log('文件已全部上传');
}
run()