-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackembedweb.js
68 lines (53 loc) · 1.77 KB
/
packembedweb.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
// packembedweb.js
// create the upload.h file that contains the html strings for the embedded web files
// like WiFi manager, web update and upload
// ===== Packages used =====
import yargs from 'yargs';
import debug from 'debug';
import * as HTMLMinifier from 'html-minifier-terser';
import shell from 'shelljs'
const outFile = "upload.h";
// ===== Command line support =====
console.log('HomeDing: Build ${outFile} file');
const options = yargs(process.argv.slice(2))
.usage('Usage: $0')
.option('v', { alias: 'verbose', describe: 'Verbose logging', type: 'boolean', demandOption: false, default: false })
.argv;
debug.enable(options.verbose ? '*' : '*:info');
// ===== initializing modules =====
const logInfo = debug('iot:info');
const logTrace = debug('iot:trace');
debug.log = console.log.bind(console);
Array.prototype.unique = function () {
return this.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
}
logInfo(`Starting...`);
let txt = shell.cat('makeembedtemplate.txt').stdout;
const placeholders = txt.match(/\$\{\w*\.htm\}/g);
logTrace("Placeholders: ", placeholders);
const tasks = placeholders
.map(p => p.substring(2, p.length - 1))
.unique()
.map(name => {
const c = shell.cat(name).stdout;
const mc = HTMLMinifier.minify(c, {
collapseWhitespace: true,
removeComments: true,
removeTagWhitespace: true,
minifyCSS: true,
minifyJS: true,
verbose: true,
quoteCharacter: "\""
}).then(x => {
txt = txt.replace('${' + name + '}', x);
});
return (mc);
});
Promise.allSettled(tasks).then(() => {
// write to upload.h to be copied into the Arduino project.
shell.ShellString(txt).to(outFile);
logTrace('generated code:\n', txt);
logInfo(`${outFile} written.`);
})