-
Notifications
You must be signed in to change notification settings - Fork 23
/
build-lite.js
165 lines (149 loc) · 4.09 KB
/
build-lite.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const fs = require('fs');
const fsp = fs.promises;
const fse = require('fs-extra');
const pathLib = require('path');
const readline = require('readline');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const marked = require('marked');
const TOML = require('@iarna/toml');
const dep_mappings = require('./frontend-dependencies');
const INPUT = pathLib.join(__dirname, 'public');
const OUTPUT = pathLib.join(__dirname, 'dist-lite');
async function main() {
// Create the output directory and copy all files from public
await fsp.mkdir(OUTPUT, { recursive: true });
await fse.copy(INPUT, OUTPUT, { overwrite: true });
// Copy frontend dependencies
await Promise.all(
[...dep_mappings.files, ...dep_mappings.directories].map(
([endpoint, path]) =>
fse.copy(
pathLib.join(__dirname, pathLib.normalize(path)),
pathLib.join(OUTPUT, pathLib.normalize(endpoint))
)
)
);
// Remove login page
await fsp.rm(pathLib.join(OUTPUT, 'login.html'));
// Preprocess HTML and JS files
await processHTML('home.html');
await Promise.all(
(await fsp.readdir(pathLib.join(OUTPUT, 'js'))).map((file) =>
processJS(pathLib.join('js', file))
)
);
}
// Preprocess a HTML file, respecting #ifdef and #ifndef
async function processHTML(file) {
let out;
try {
out = await fsp.open(pathLib.join(OUTPUT, file), 'w');
const input_stream = fs.createReadStream(pathLib.join(INPUT, file));
const rl = readline.createInterface({
input: input_stream,
crlfDelay: Infinity
});
// Keep track of whether we are inside an ifndef, in which case lines should
// not be copied
let ifndef = false;
for await (const line of rl) {
// Beginning or end of ifdef
if (line === '<!--#ifdef lite>' || line === '<!#endif-->') {
continue;
}
// Beginning of ifndef
if (line === '<!--#ifndef lite-->') {
ifndef = true;
continue;
}
// End of ifndef
if (line === '<!--#endif-->') {
ifndef = false;
continue;
}
if (!ifndef) {
await out.write(line + '\n');
}
}
} finally {
out.close();
}
}
// Preprocess a JS file, respecting #ifdef, #ifndef, and #include
async function processJS(file) {
let out;
try {
out = await fsp.open(pathLib.join(OUTPUT, file), 'w');
const input_stream = fs.createReadStream(pathLib.join(INPUT, file));
const rl = readline.createInterface({
input: input_stream,
crlfDelay: Infinity
});
// Keep track of whether we are inside an ifndef, in which case lines should
// not be copied
let ifndef = false;
// Keep track of if we are in an ifdef (to recognize the comment indicators)
let ifdef = false;
for await (const line of rl) {
// Beginning of ifdef
if (line === '//#ifdef lite') {
ifdef = true;
continue;
}
// Comment indicator for ifdef
if (ifdef && (line === '/*' || line === '*/')) {
continue;
}
// Beginning of ifndef
if (line === '//#ifndef lite') {
ifndef = true;
continue;
}
// End of ifdef or ifndef
if (line === '//#endif') {
ifdef = false;
ifndef = false;
continue;
}
if (line === '//#include VERSION') {
const version = (await exec('git describe')).stdout
.toString()
.trim()
.match(/^v?(.*)/)[1];
await out.write(JSON.stringify(version) + '\n');
continue;
}
if (line === '//#include CHANGELOG') {
const changelog = marked(
(await fsp.readFile(__dirname + '/CHANGELOG.md')).toString()
);
await out.write(JSON.stringify(changelog) + '\n');
continue;
}
if (line === '//#include SCHEDULE') {
const schedule = TOML.parse(
await fsp.readFile(__dirname + '/public/schedule.toml')
);
await out.write(JSON.stringify(schedule) + '\n');
continue;
}
// All other include directives
let match;
if ((match = line.match(/^\/\/#include (.*)/))) {
const path = pathLib.join(
__dirname,
pathLib.normalize(match[1])
);
await out.write((await fsp.readFile(path)) + '\n');
continue;
}
if (!ifndef) {
await out.write(line + '\n');
}
}
} finally {
if (out !== undefined) out.close();
}
}
main().catch(console.error);