-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-nodehtml.js
65 lines (57 loc) · 2.25 KB
/
build-nodehtml.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
const fs = require('fs').promises;
// const path = require('node:path');
const nodeSrcRoot = 'src/nodes-html';
const nodeDestRoot = 'nodes';
const processFile = node => new Promise((resolve, reject) => {
const template = {
start: '<script type="text/javascript">\n',
middle: '</script>\n\n',
end: '',
};
const nodeEditor = `${nodeSrcRoot}/${node}/editor.js`;
const nodeMain = `${nodeSrcRoot}/${node}/main.html`;
const nodeHTML = `${nodeDestRoot}/${node}.html`;
fs.access(nodeEditor)
.then(() => fs.access(nodeMain))
.then(() => fs.writeFile(nodeHTML, template.start))
.then(() =>
fs.readFile(nodeEditor)
.then(content => fs.appendFile(nodeHTML, content).catch(error => console.log(error))),
)
.then(() => fs.appendFile(nodeHTML, template.middle))
.then(() =>
fs.readFile(nodeMain)
.then(content => fs.appendFile(nodeHTML, content).catch(error => console.log(error))),
)
.then(() => fs.appendFile(nodeHTML, template.end))
.then(() => resolve())
.catch(error => reject(error));
});
console.log(
'[\u001B[90m%s\u001B[0m] nodeSrcRoot: \'\u001B[36m%s\u001B[37m\'',
new Date().toLocaleTimeString(undefined, {hourCycle: 'h23'}),
nodeSrcRoot,
);
console.log(
'[\u001B[90m%s\u001B[0m] nodeDestRoot: \'\u001B[36m%s\u001B[37m\'',
new Date().toLocaleTimeString(undefined, {hourCycle: 'h23'}),
nodeDestRoot,
);
fs.readdir(nodeSrcRoot, {withFileTypes: true}).then(files => {
for (const file of files) {
if (file.isDirectory) {
processFile(file.name)
.then(() =>
console.log(
'\u001B[37m[\u001B[90m%s\u001B[37m] \'\u001B[36m%s\u001B[37m\' written',
new Date().toLocaleTimeString(undefined, {hourCycle: 'h23'}),
file.name,
))
.catch(error =>
console.log(
`\u001B[37m[\u001B[90m%s\u001B[37m] \u001B[31mERROR\u001B[0m: ${error.code}, ${error.syscall} '${error.path}'`,
new Date().toLocaleTimeString(undefined, {hourCycle: 'h23'}),
));
}
}
});