-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
112 lines (107 loc) · 3.21 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
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
const fs = require("fs");
const path = require("path");
// const licenseList = require("lilist");
class Build {
fileList = [];
currentPath = "";
/*
* src/md/ディレクトリ下のファイル名一覧を取得
*/
do() {
Promise.resolve()
.then(
() =>
new Promise((resolve) => {
const fileList = fs
.readdirSync(path.join(`./src/pug/md`))
.filter((fileName) => /\d{1,2}\w*\.md$/.test(fileName))
.map((fileName) => ({
path: path.join(__dirname, "./src/pug/md/", fileName),
name: fileName,
title: "",
thumnailPath: "",
markdown: "",
href: `./activity/${fileName.replace(".md", ".html")}`,
}));
resolve(fileList);
})
)
.then(
(fileList) =>
new Promise((resolve) => {
const fileListFilled = fileList.map((file) => {
const fBuff = fs.readFileSync(file.path);
fBuff
.toString()
.split("\n")
.map((line) => {
if (/^\s*#\s+.+$/.test(line)) {
file.title = line.replace(/^#\s/, "");
}
if (/^\s*!\[.*\]\(.*\)\s*$/.test(line)) {
file.thumnailPath = line
.replace(/^\s*!\[.*\]/, "")
.replace("(", "")
.replace(")", "")
.replace(/\s*/g, "");
}
file.markdown = fBuff;
});
return file;
});
resolve(fileListFilled);
})
)
.then(
(fileList) =>
new Promise((resolve) => {
const listViewBuff = [];
fileList.map((file) => {
// list buff
const item = {
title: file.title,
thumnailPath: file.thumnailPath,
href: file.href,
};
listViewBuff.push(item);
});
const fileBuff = `- var activityList = ${JSON.stringify(
listViewBuff
)}`;
fs.writeFile(
path.join(__dirname, "./src/pug/_generated", "_activity.pug"),
fileBuff,
() => {
resolve(fileList);
}
);
})
)
.then(
(fileList) =>
new Promise((resolve) => {
const temlpalePug = fs
.readFileSync(path.join(__dirname, "./src/pug/_template.pug"))
.toString();
fileList.map((file) => {
const outBuff = temlpalePug
.replace("XXXXX", `../md/${file.name}`)
.replace("./img", "../img");
fs.writeFileSync(
path.join(
__dirname,
"./src/pug/_generated/",
file.name.replace(".md", ".pug")
),
outBuff
);
});
resolve();
})
);
}
}
console.log("build.js start");
const build = new Build();
build.do();
console.log("build.js finish");