-
Notifications
You must be signed in to change notification settings - Fork 279
/
gulpfile.js
122 lines (104 loc) · 3.28 KB
/
gulpfile.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
const gulp = require("gulp");
const uglify = require("gulp-uglify");
const cssnano = require("gulp-cssnano");
const through = require("through2");
const path = require("path");
const fs = require("fs");
const readline = require("readline");
const destination = "new_http.c";
function dumpFileSize() {
return through.obj(function (file, enc, cb) {
console.log(
`Processing ${file.basename}, original length ${file.stat.size}`
);
cb(null, file);
});
}
/** This function injects C for a const field in new_http.c */
function generateCode(field_name, is_script) {
return through.obj(function (file, enc, cb) {
if (file.isBuffer()) {
const contents = file.contents;
let output = String(contents);
output = output.replace(/\"/g, '\\"');
console.log(
`Processing ${file.basename}, reduced length ${contents.length}`
);
const prefix = is_script ? "<script type='text/javascript'>" : "<style>";
const suffix = is_script ? "</script>" : "</style>";
output = `const char ${field_name}[] = "${prefix}${output}${suffix}";`;
const target_path = path.join(path.dirname(file.path), destination);
//console.log(`Updated ${target_path}`);
const rl = readline.createInterface({
input: fs.createReadStream(target_path),
crlfDelay: Infinity,
});
const merged_contents = [];
const marker_start = `//region_start ${field_name}`;
const marker_end = `//region_end ${field_name}`;
let region_state = 0;
rl.on("line", (line) => {
if (line.trim() === marker_start) {
region_state = 1;
merged_contents.push(marker_start);
merged_contents.push(output);
merged_contents.push(marker_end);
} else {
//Skip all existing content lines till region ends
if (region_state === 1) {
if (line.trim() === marker_end) {
region_state = 2;
}
} else {
merged_contents.push(line);
}
}
});
rl.on("close", () => {
if (region_state === 0) {
//Starting marker was not found, append
merged_contents.push("");
merged_contents.push(marker_start);
merged_contents.push(output);
merged_contents.push(marker_end);
}
if (region_state === 1) {
cb(`Ending marker "${marker_end}" was not found.`, file);
} else {
fs.writeFile(
target_path,
merged_contents.join("\r\n"),
"utf8",
(err) => {
cb(err, file);
}
);
}
});
return;
}
cb(null, file);
});
}
function minifyJs() {
return gulp
.src("./src/httpserver/script.js")
.pipe(dumpFileSize())
.pipe(uglify())
.pipe(generateCode("pageScript", true));
}
function minifyHassDiscoveryJs() {
return gulp
.src("./src/httpserver/script_ha_discovery.js")
.pipe(dumpFileSize())
.pipe(uglify())
.pipe(generateCode("ha_discovery_script", true));
}
function minifyCss() {
return gulp
.src("./src/httpserver/style.css")
.pipe(dumpFileSize())
.pipe(cssnano())
.pipe(generateCode("htmlHeadStyle", false));
}
exports.default = gulp.series(minifyJs, minifyHassDiscoveryJs, minifyCss);