This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildtool.js
129 lines (117 loc) · 3.62 KB
/
buildtool.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { renderFile } = require("ejs");
const { rm, readFile, writeFile, mkdir, cp, readdir } = require("fs/promises");
const purify = require("dompurify");
const { marked } = require("marked");
const { compileAsync } = require("sass");
const tasks = new (class TaskManager {
tasks = new Map();
executedTasks = [];
register(fn, name = null) {
name = name || fn.name;
this.tasks.set(name, { fn, depend: [] });
}
depend(name, dep) {
this.tasks.get(name).depend.push(dep);
}
async run(name) {
if (this.executedTasks.includes(name)) return;
const task = this.tasks.get(name);
if (!task) throw Error(`No task found: '${task}'`);
for (const taskName of task.depend) {
await this.run(taskName);
}
console.log(`:${name}`);
await task.fn();
}
})();
// Actual tasks
async function buildPages(root) {
for (const name of ["index", "qna", "news"]) {
const string = await renderFile(
`${__dirname}/template.ejs`,
{
require,
fragment: name,
...JSON.parse(await readFile(`${root}/${name}.json`)),
forward: {
__dirname,
},
},
{
async: true,
}
);
await writeFile(`${__dirname}/build/${name}.html`, string);
}
}
async function buildNews() {
await mkdir(`${__dirname}/build/news`, { recursive: true });
for (const news of await readdir(`${__dirname}/news`).then((a) =>
a.sort((a, b) => Number(a.split(".")[0]) - Number(b.split(".")[0]))
)) {
const string = await renderFile(
`${__dirname}/template.ejs`,
{
require,
fragment: "display-news",
title: "placeholder",
description: "placeholder",
forward: {
html: await marked(await readFile(
`${__dirname}/news/${news}`,
"utf8"
), { async: true, silent: true, sanitize: true, sanitizer: purify.sanitize }),
},
},
{
async: true,
}
);
await writeFile(
`${__dirname}/build/news/${news.replace(/\.md/, ".html")}`,
string
);
}
{
const string = await renderFile(
`${__dirname}/feed.ejs`,
{
require,
__dirname,
},
{
async: true,
}
);
await writeFile(`${__dirname}/build/feed.rss`, string);
}
}
async function buildCSS(root) {
await compileAsync(`${__dirname}/style.scss`)
.then($ => writeFile(`${root}/style.css`, $.css));
await compileAsync(`${__dirname}/article.scss`)
.then($ => writeFile(`${root}/article.css`, $.css));
}
async function build() {
await rm(`${__dirname}/build`, { recursive: true, force: true });
const root = `${__dirname}/pages`;
await mkdir(`${__dirname}/build`);
await Promise.all([
buildCSS(`${__dirname}/build`),
buildNews().then(_ => buildPages(root)),
(async () => {
await mkdir(`${__dirname}/build/img`);
await cp(`${__dirname}/img`, `${__dirname}/build/img`, {
recursive: true,
force: true,
});
})(),
]);
}
tasks.register(build);
async function main(taskList) {
for (const task of taskList) {
await tasks.run(task);
}
}
if (require.main == module) main(process.argv.slice(2));