forked from wangsijie/static-deploy-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
110 lines (102 loc) · 2.82 KB
/
sync.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fs = require('fs');
const path = require('path');
const md5FileCb = require('md5-file');
const getClient = require('./oss');
const resolveObject = require('./resolve-object');
let client;
const md5File = dir => new Promise((resolve, reject) => {
md5FileCb(dir, (err, hash) => {
if (err) {
return reject(err);
}
resolve(hash);
})
});
const list = [];
function walk(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fileDir = path.join(dir, file);
const stats = fs.statSync(fileDir);
if (stats.isDirectory()) {
walk(fileDir);
} else {
list.push(fileDir);
}
});
// index.html文件最后上传,确保部署顺序
list.sort((a, b) => {
if (/index\.html?$/.test(a)) {
return 1;
}
if (/index\.html?$/.test(b)) {
return -1;
}
return 0;
});
}
async function upload(local, remote) {
for (const file of list) {
const hash = await md5File(file);
const remotePath = resolveObject(file.replace(local, remote));
const remoteFile = hashes.find(o => o.name === remotePath);
if (remoteFile && remoteFile.hash === hash.toUpperCase()) {
console.log('skip: ' + file);
continue;
}
console.log('uploading: ' + file);
const job = async (retry) => {
try {
await client.put(remotePath, file);
} catch (e) {
if (retry) {
console.log('retrying: ' + file);
await job(retry - 1);
} else {
console.log('error: ' + file);
}
}
}
await job(5);
}
}
async function clean(local, remote) {
const reg = new RegExp(`^${local}`);
const localList = list.map(url => url.replace(reg, remote));
for (const file of hashes) {
if (!localList.includes(file.name)) {
console.log('deleting: ' + file.name);
await client.delete(file.name);
}
}
}
let hashes = [];
async function getHashes(prefix) {
let marker;
do {
const result = await client.list({
prefix: prefix + '/',
marker,
});
if (!result.objects) {
return [];
}
marker = result.nextMarker;
hashes = [...hashes, ...result.objects.map(o => ({
name: o.name,
hash: o.etag.replace(/"/g, ''),
}))];
} while (marker);
return hashes;
}
const app = async ({ aliyun, local, remote }) => {
client = getClient(aliyun);
walk(local);
await getHashes(remote);
await upload(local, remote);
await clean(local, remote);
return {
localFiles: list,
};
}
module.exports = app;