-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
61 lines (55 loc) · 1.49 KB
/
build.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
var fs = require("fs/promises");
var path = require("path");
var indexTemplate = "";
async function copy(from, to, isDir) {
if (await exists(from)) {
if (isDir || (await fs.stat(from)).isDirectory()) {
await fs.mkdir(to, { recursive: true })
let entries = await fs.readdir(from, { withFileTypes: true })
for (let entry of entries) {
let fromPath = path.join(from, entry.name)
let toPath = path.join(to, entry.name)
entry.isDirectory() ?
await copy(fromPath, toPath, true) :
await fs.copyFile(fromPath, toPath)
}
} else {
return await fs.copyFile(from, to)
}
}
}
async function exists(path) {
try {
await fs.access(path)
return true
} catch (err) {
console.log(err)
return false
}
}
async function makeIndex(dirPath) {
try {
await fs.writeFile(
path.join("dist/", dirPath, "/index.html"),
indexTemplate.replace(
"${dirPath}",
`https://scp-wiki-cn.wikidot.com/${dirPath}`),
"utf8")
return true
} catch (err) {
console.log(err)
return false
}
}
!(async ()=>{
await fs.rm("dist", { recursive: true, force: true });
await fs.mkdir("dist/");
indexTemplate = await fs.readFile("index.template.html", "utf8");
for (const frame of ["index.html"]) {
await copy(frame, "dist/" + frame);
}
for (const frame of ["top-rated-pages-this-month"]) {
await copy(frame, "dist/" + frame);
await makeIndex(frame);
}
})().catch(e=>console.log(e));