-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatePhotos.js
44 lines (36 loc) · 1.04 KB
/
updatePhotos.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
const fs = require("fs");
const filePath = "./public/gallery/";
const msg = (type, msg) => {
// 設置控制台輸出顏色
const reset = "\x1b[0m";
const red = "\x1b[31m";
const green = "\x1b[32m";
const yellow = "\x1b[33m";
let color = yellow;
if (["Success", "Build"].includes(type)) color = green;
else if (["Waring", "Watching"].includes(type)) color = yellow;
else if (["Error", "Failed"].includes(type)) color = red;
console.log(`${color}[${type}]${reset} ${msg}`);
};
const createFileList = () => {
msg("Building", "building...");
const files = fs.readdirSync(filePath);
try {
fs.writeFileSync(
"./src/utils/photos.json",
JSON.stringify({ files: files })
);
msg("Success", "builded.");
} catch (err) {
msg("Error", err);
}
};
const mode = process.argv[2] ?? "";
if (["-w", "--watch"].includes(mode)) {
msg("Watching", "start watching files...");
fs.watch(filePath, (event, filename) => {
msg("Watching", `${event}-detected: ${filename}`);
createFileList();
});
}
createFileList();