-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgzip_h.js
75 lines (63 loc) · 2.23 KB
/
gzip_h.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
const pkg = require('./package.json');
const in_folder = './dist/host';
const out_folder = './dist/gzip';
const fs = require('node:fs');
const { promisify } = require('node:util');
const { createGzip } = require('node:zlib');
const { pipeline } = require('node:stream');
const pipe = promisify(pipeline);
async function gzip(input, output) {
const gzip = createGzip();
const source = fs.createReadStream(input);
const destination = fs.createWriteStream(output);
await pipe(source, gzip, destination);
}
async function compile() {
const index_gz = out_folder + '/index.html.gz';
const script_gz = out_folder + '/script.js.gz';
const style_gz = out_folder + '/style.css.gz';
try {
if (!fs.existsSync(out_folder)) fs.mkdirSync(out_folder);
await gzip(in_folder + '/index.html', index_gz);
await gzip(in_folder + '/script.js', script_gz);
await gzip(in_folder + '/style.css', style_gz);
} catch (err) {
console.error(err);
return;
}
let index_len = fs.statSync(index_gz).size;
let script_len = fs.statSync(script_gz).size;
let style_len = fs.statSync(style_gz).size;
let code = `#pragma once
#include <Arduino.h>
/*
${pkg.name}.h v${pkg.version}
index: ${index_len} bytes
script: ${script_len} bytes
style: ${style_len} bytes
total: ${((index_len + script_len + style_len) / 1024).toFixed(2)} kB
Build: ${new Date()}
*/
`;
function addBin(fname, gzip) {
let data = fs.readFileSync(gzip).toString('hex');
let code = '\r\n' + `const uint8_t ${pkg.name}_${fname}[] PROGMEM = {`;
for (let i = 0; i < data.length; i += 2) {
if (i % 48 == 0) code += '\r\n ';
code += '0x' + data[i] + data[i + 1];
if (i < data.length - 2) code += ', ';
}
code += '\r\n};\r\n'
code += `const size_t ${pkg.name}_${fname}_len = ${data.length / 2};`;
code += '\r\n'
return code;
}
code += addBin('index', index_gz);
code += addBin('script', script_gz);
code += addBin('style', style_gz);
fs.writeFile(`${out_folder}/${pkg.name}.h`, code, err => {
if (err) console.error(err);
else console.log('Done!');
});
}
compile();