forked from BitcoinQnA/seedtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
54 lines (53 loc) · 1.78 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
const fs = require('fs');
const util = require('util');
const path = require('path');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
console.time('Build script took');
console.log('Building HTML file...');
(async () => {
try {
let result = await readFile(
path.join(__dirname, '/src/www/dev.html'),
'utf8'
);
result = result.replace(/<script id="websocket">[^]*<\/script>/, '');
console.log('Hot reload Web Socket script tags removed...');
const regex1 = new RegExp(/<script src="(?<path>...*)">[^]*?</);
let array1 = regex1.exec(result);
while (array1 !== null) {
const scriptLocation = path.join(__dirname, `/src/www/`, `${array1[1]}`);
console.log(`Adding script from ${scriptLocation}`);
const js = await readFile(scriptLocation, 'utf8');
result = result.replace(
array1[0],
`<script async>
${js}
<`
);
array1 = regex1.exec(result);
console.log('Done!');
}
const regex2 = /<link rel="stylesheet" href="(?<path>...*)?">/;
let array2 = regex2.exec(result);
while (array2 !== null) {
const scriptLocation = path.join(__dirname, `/src/www/`, `${array2[1]}`);
console.log(`Adding Stylesheet from ${scriptLocation}`);
const css = await readFile(scriptLocation, 'utf8');
result = result.replace(
array2[0],
`<style>
${css}
</style>`
);
array2 = regex2.exec(result);
console.log('Done!');
}
const output = path.join(__dirname, '/dist/index.html');
await writeFile(output, result, 'utf8');
console.log(`Task completed! Built file is available at ${output}`);
} catch (error) {
console.log('Build failed', error);
}
console.timeEnd('Build script took');
})();