-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackminimal.js
140 lines (108 loc) · 3.68 KB
/
packminimal.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
#!/usr/bin/env node
// packminimal.js
// copy all files for the minimal web server into the mindist folder
// ready to be published on http://homeding.github.io/vmxx
// ===== Packages used =====
import yargs from 'yargs';
import debug from 'debug';
import * as sass from 'sass';
import * as HTMLMinifier from 'html-minifier-terser';
import * as JSMinifier from 'terser';
import shell from 'shelljs'
const distFolder = "dist-mini";
// ===== Command line support =====
console.log('HomeDing: Packing dist-mini Folder');
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');
debug.log = console.log.bind(console);
// pack all assets
async function packAssets(assets) {
const asyncJobs = [];
for (const op of assets) {
let txt;
if (!op.tar) { op.tar = op.src; }
switch (op.m) {
case 'c': // sync copy
shell.cp(op.src, distFolder + '/' + op.tar);
break;
case 'm': // async minify html
asyncJobs.push(new Promise(res => {
txt = shell.cat(op.src).stdout;
HTMLMinifier.minify(txt, {
collapseWhitespace: true,
removeComments: true,
removeTagWhitespace: true,
minifyCSS: true,
minifyJS: true,
verbose: true,
quoteCharacter: "'"
}).then(txt => {
shell.ShellString(txt).to(distFolder + '/' + op.tar);
res();
});
}));
break;
case 'js': // async minify javascript
asyncJobs.push(new Promise(res => {
txt = shell.cat(op.src).stdout;
JSMinifier.minify(txt, {
compress: { drop_console: true, drop_debugger: true },
mangle: true,
output: { indent_level: 0, beautify: false }
}).then(out => {
shell.ShellString(out.code).to(distFolder + '/' + op.tar);
res();
});
}));
break;
case 'json': // minify json
txt = shell.cat(op.src).stdout;
txt = JSON.stringify(JSON.parse(txt), null, 0);
shell.ShellString(txt).to(distFolder + '/' + op.tar);
break;
case 'css': // sync minify css
txt = sass.compile(op.src, {
style: 'compressed',
sourceMap: false
}).css;
shell.ShellString(txt).to(distFolder + '/' + op.tar);
break;
case 'xml': // sync minify xml
txt = shell.cat(op.src).stdout;
txt = txt.replace(/([>])\s+/g, '$1');
shell.ShellString(txt).to(distFolder + '/' + op.tar);
break;
default:
break;
}
}
return (Promise.all(asyncJobs));
}
logInfo(`Starting...`);
// array with files that get copied as they are
const assets = [
{ m: 'xml', src: 'browserconfig.xml' },
{ m: 'json', src: 'site.min.webmanifest', tar: 'site.webmanifest'},
{ m: 'xml', src: 'favicon.svg' },
// { m: 'xml', src: 'icons-mini.svg', tar:'icons.svg'},
{ m: 'css', src: 'css/iot.scss', tar: 'iotstyle.css' },
{ m: 'js', src: 'micro-mini.js', tar: 'micro.js' },
{ m: 'm', src: 'ding.htm' },
{ m: 'm', src: 'microide.htm' },
{ m: 'm', src: 'updateicons.min.htm', tar: 'updateicons.htm' }
];
// create fresh dist folders
shell.rm('-rf', distFolder);
shell.mkdir(distFolder);
packAssets(assets).then(() => {
logInfo(`Files created.`);
let listText =
shell.ls('-R', distFolder).grep(/^.*\..*$/).stdout;
shell.ShellString(listText).to(distFolder + '/list.txt');
logInfo(`list.txt file written.`);
});