summaryrefslogtreecommitdiffstats
path: root/html/gulpfile.js
blob: b1a4aad03305a59d7a5a2e8b30aa32c7606a5d91 (plain)
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
const { src, dest, task, series } = require('gulp');
const clean = require('gulp-clean');
const gzip = require('gulp-gzip');
const inlineSource = require('gulp-inline-source');
const rename = require('gulp-rename');
const through2 = require('through2');

const genHeader = (size, buf, len) => {
    let idx = 0;
    let data = 'unsigned char index_html[] = {\n  ';

    for (const value of buf) {
        idx++;

        const current = value < 0 ? value + 256 : value;

        data += '0x';
        data += (current >>> 4).toString(16);
        data += (current & 0xf).toString(16);

        if (idx === len) {
            data += '\n';
        } else {
            data += idx % 12 === 0 ? ',\n  ' : ', ';
        }
    }

    data += '};\n';
    data += `unsigned int index_html_len = ${len};\n`;
    data += `unsigned int index_html_size = ${size};\n`;
    return data;
};
let fileSize = 0;

task('clean', () => {
    return src('dist', { read: false, allowEmpty: true }).pipe(clean());
});

task('inline', () => {
    return src('dist/index.html').pipe(inlineSource()).pipe(rename('inline.html')).pipe(dest('dist/'));
});

task(
    'default',
    series('inline', () => {
        return src('dist/inline.html')
            .pipe(
                through2.obj((file, enc, cb) => {
                    fileSize = file.contents.length;
                    return cb(null, file);
                })
            )
            .pipe(gzip())
            .pipe(
                through2.obj((file, enc, cb) => {
                    const buf = file.contents;
                    file.contents = Buffer.from(genHeader(fileSize, buf, buf.length));
                    return cb(null, file);
                })
            )
            .pipe(rename('html.h'))
            .pipe(dest('../src/'));
    })
);