-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
145 lines (133 loc) · 3.37 KB
/
deploy.js
File metadata and controls
145 lines (133 loc) · 3.37 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const COS = require("cos-nodejs-sdk-v5");
const path = require("path");
const fs = require("fs/promises");
const distRelPath = require(path.resolve(
__dirname,
"./blogs/.vuepress/config.js",
)).dest;
const { spawn } = require("child_process");
const {
COS_SECRET_ID,
COS_SECRET_KEY,
COS_TARGET_BUCKET,
COS_BUCKET_REGION,
// CURRENT_COMMIT_ID
} = process.env;
const cos = new COS({
SecretId: COS_SECRET_ID,
SecretKey: COS_SECRET_KEY,
});
const bucketInfo = {
Bucket: COS_TARGET_BUCKET,
Region: COS_BUCKET_REGION,
};
// async function getLastCommitId() {
// return await new Promise((resolve, reject) => {
// cos.getObject({
// ...bucketInfo,
// Key: 'last_commit_id',
// }, (err, data) => {
// if (err) reject(err)
// else {
// let res = data.Body.toString().trim()
// if (/^.{8}$/.test(res)) resolve(res);
// else reject("Invalid commit id.");
// };
// });
// })
// }
// async function getDiffFileList(lastCommitId) {
// return new Promise((res, rej) => {
// const diff = spawn('git', ['diff', lastCommitId, CURRENT_COMMIT_ID, '--name-only']);
// let output = "";
// diff.stdout.on('data', data => {
// output += data
// });
// diff.on('close', code => {
// if (code === 0) {
// res(output.trim().split('\n').map(Key => ({ Key })));
// } else {
// rej("git diff failed: " + output);
// }
// });
// })
// }
async function deleteFiles(files) {
return await new Promise((resolve, reject) => {
cos.deleteMultipleObject(
{
...bucketInfo,
Objects: files,
Quiet: true,
},
(err, data) => {
if (err) reject(err);
else resolve(data);
},
);
});
}
async function uploadFiles(relPaths) {
const files = relPaths.map((i) => ({
...bucketInfo,
Key: i.substring(distRelPath.length),
FilePath: path.resolve(__dirname, i),
}));
return new Promise((resolve, reject) => {
cos.uploadFiles(
{
files,
SliceSize: 1024 * 1024,
},
function (err, data) {
if (err) reject(err);
else resolve(data);
},
);
});
}
async function getAllFilesInBucket() {
return await new Promise((resolve, reject) => {
cos.getBucket(
{
...bucketInfo,
},
(err, data) => {
if (err) reject(err);
else resolve(data.Contents.map(({ Key }) => ({ Key })));
},
);
});
}
async function listFilesInPath(dirRelPath) {
const target = [];
for (const i of await fs.readdir(dirRelPath, { withFileTypes: true })) {
if (i.isDirectory()) {
target.push(
...(await listFilesInPath(path.posix.join(dirRelPath, i.name))),
);
} else {
target.push(path.posix.join(dirRelPath, i.name));
}
}
return target;
}
process.on("unhandledRejection", err => {
console.error(err);
process.exit(-1);
});
async function deploy() {
console.info(`Deploying to ${COS_TARGET_BUCKET}.`);
const filesToDelete = await getAllFilesInBucket();
if (filesToDelete.length > 0) {
console.info("Deleting files:")
filesToDelete.forEach(i => { console.log(i.Key) });
await deleteFiles(filesToDelete);
}
const filesToUpload = await listFilesInPath(distRelPath)
console.info("Uploading files:");
console.log(filesToUpload);
await uploadFiles(filesToUpload);
console.info(`Deployment accomplished.`);
}
deploy();