Replace repo url and version automatically in HTML

This commit is contained in:
Ruslan Gainutdinov 2020-06-08 20:59:40 +03:00
parent f5d6383f50
commit ee6f25cc47
3 changed files with 64 additions and 38 deletions

View File

@ -16,6 +16,7 @@
*/ */
const fs = require("fs"); const fs = require("fs");
const packageJson = require("../package.json");
/** /**
* *
@ -42,6 +43,28 @@ function hexdump(buffer) {
const inliner = require("inliner"); const inliner = require("inliner");
const zlib = require("zlib"); const zlib = require("zlib");
function strReplace(str, search, replacement) {
return str.split(search).join(replacement);
}
function adoptVersionAndRepo(html) {
let repoUrl = packageJson.repository ? packageJson.repository.url : undefined;
if (repoUrl) {
repoUrl = repoUrl.replace(/^git\+/, "");
repoUrl = repoUrl.replace(/\.git$/, "");
// Replace we
html = strReplace(html, "https://github.com/atuline/WLED", repoUrl);
html = strReplace(html, "https://github.com/Aircoookie/WLED", repoUrl);
}
let version = packageJson.version;
if (version) {
html = strReplace(html, "##VERSION##", version);
}
return html;
}
function writeHtmlGzipped(sourceFile, resultFile) { function writeHtmlGzipped(sourceFile, resultFile) {
console.info("Reading " + sourceFile); console.info("Reading " + sourceFile);
new inliner(sourceFile, function (error, html) { new inliner(sourceFile, function (error, html) {
@ -52,6 +75,7 @@ function writeHtmlGzipped(sourceFile, resultFile) {
throw error; throw error;
} }
html = adoptVersionAndRepo(html);
zlib.gzip(html, function (error, result) { zlib.gzip(html, function (error, result) {
if (error) { if (error) {
console.warn(error); console.warn(error);
@ -77,6 +101,8 @@ const CleanCSS = require("clean-css");
const MinifyHTML = require("html-minifier").minify; const MinifyHTML = require("html-minifier").minify;
function filter(str, type) { function filter(str, type) {
str = adoptVersionAndRepo(str);
if (type === undefined) { if (type === undefined) {
return str; return str;
} else if (type == "css-minify") { } else if (type == "css-minify") {
@ -87,7 +113,8 @@ function filter(str, type) {
maxLineLength: 80, maxLineLength: 80,
minifyCSS: true, minifyCSS: true,
minifyJS: true, minifyJS: true,
continueOnParseError: false continueOnParseError: false,
removeComments: true,
}); });
} else { } else {
console.warn("Unknown filter: " + type); console.warn("Unknown filter: " + type);
@ -101,10 +128,9 @@ function specToChunk(srcDir, s) {
const str = buf.toString("ascii"); const str = buf.toString("ascii");
const chunk = ` const chunk = `
// Autogenerated from ${srcDir}/${s.file}, do not edit!! // Autogenerated from ${srcDir}/${s.file}, do not edit!!
const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${filter( const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${filter(str, s.filter)}${
str, s.append || ""
s.filter }";
)}${s.append || ""}";
`; `;
return s.mangle ? s.mangle(chunk) : chunk; return s.mangle ? s.mangle(chunk) : chunk;
@ -128,7 +154,7 @@ ${result}
function writeChunks(srcDir, specs, resultFile) { function writeChunks(srcDir, specs, resultFile) {
let src = ""; let src = "";
specs.forEach(s => { specs.forEach((s) => {
try { try {
console.info("Reading " + srcDir + "/" + s.file + " as " + s.name); console.info("Reading " + srcDir + "/" + s.file + " as " + s.name);
src += specToChunk(srcDir, s); src += specToChunk(srcDir, s);
@ -154,7 +180,7 @@ writeChunks(
prepend: "=====(<style>", prepend: "=====(<style>",
append: "</style>)=====", append: "</style>)=====",
method: "plaintext", method: "plaintext",
filter: "css-minify" filter: "css-minify",
}, },
{ {
file: "settings.htm", file: "settings.htm",
@ -162,7 +188,7 @@ writeChunks(
prepend: "=====(", prepend: "=====(",
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify" filter: "html-minify",
}, },
{ {
file: "settings_wifi.htm", file: "settings_wifi.htm",
@ -171,14 +197,14 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace( .replace(
/function GetV().*\<\/script\>/gms, /function GetV().*\<\/script\>/gms,
"function GetV() {var d=document;\n" "function GetV() {var d=document;\n"
) ),
}, },
{ {
file: "settings_leds.htm", file: "settings_leds.htm",
@ -187,14 +213,14 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace( .replace(
/function GetV().*\<\/script\>/gms, /function GetV().*\<\/script\>/gms,
"function GetV() {var d=document;\n" "function GetV() {var d=document;\n"
) ),
}, },
{ {
file: "settings_dmx.htm", file: "settings_dmx.htm",
@ -203,7 +229,7 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => { mangle: (str) => {
const nocss = str const nocss = str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
@ -218,7 +244,7 @@ ${nocss}
const char PAGE_settings_dmx[] PROGMEM = R"=====()====="; const char PAGE_settings_dmx[] PROGMEM = R"=====()=====";
#endif #endif
`; `;
} },
}, },
{ {
file: "settings_ui.htm", file: "settings_ui.htm",
@ -227,14 +253,14 @@ const char PAGE_settings_dmx[] PROGMEM = R"=====()=====";
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace( .replace(
/function GetV().*\<\/script\>/gms, /function GetV().*\<\/script\>/gms,
"function GetV() {var d=document;\n" "function GetV() {var d=document;\n"
) ),
}, },
{ {
file: "settings_sync.htm", file: "settings_sync.htm",
@ -243,11 +269,11 @@ const char PAGE_settings_dmx[] PROGMEM = R"=====()=====";
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace(/function GetV().*\<\/script\>/gms, "function GetV() {\n") .replace(/function GetV().*\<\/script\>/gms, "function GetV() {\n"),
}, },
{ {
file: "settings_time.htm", file: "settings_time.htm",
@ -256,11 +282,11 @@ const char PAGE_settings_dmx[] PROGMEM = R"=====()=====";
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace(/function GetV().*\<\/script\>/gms, "function GetV() {\n") .replace(/function GetV().*\<\/script\>/gms, "function GetV() {\n"),
}, },
{ {
file: "settings_sec.htm", file: "settings_sec.htm",
@ -269,15 +295,15 @@ const char PAGE_settings_dmx[] PROGMEM = R"=====()=====";
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => mangle: (str) =>
str str
.replace(/\<link rel="stylesheet".*\>/gms, "") .replace(/\<link rel="stylesheet".*\>/gms, "")
.replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%") .replace(/\<style\>.*\<\/style\>/gms, "%CSS%%SCSS%")
.replace( .replace(
/function GetV().*\<\/script\>/gms, /function GetV().*\<\/script\>/gms,
"function GetV() {var d=document;\n" "function GetV() {var d=document;\n"
) ),
} },
], ],
"wled00/html_settings.h" "wled00/html_settings.h"
); );
@ -292,7 +318,8 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => str.replace(/fetch\("http\:\/\/.*\/win/gms, "fetch(\"/win") mangle: (str) =>
str.replace(/fetch\("http\:\/\/.*\/win/gms, 'fetch("/win'),
}, },
{ {
file: "msg.htm", file: "msg.htm",
@ -301,7 +328,7 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => str.replace(/\<h2\>.*\<\/body\>/gms, "<h2>%MSG%</body>") mangle: (str) => str.replace(/\<h2\>.*\<\/body\>/gms, "<h2>%MSG%</body>"),
}, },
{ {
file: "dmxmap.htm", file: "dmxmap.htm",
@ -310,13 +337,13 @@ writeChunks(
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify", filter: "html-minify",
mangle: str => ` mangle: (str) => `
#ifdef WLED_ENABLE_DMX #ifdef WLED_ENABLE_DMX
${str.replace(/function FM\(\)[ ]?\{/gms, "function FM() {%DMXVARS%\n")} ${str.replace(/function FM\(\)[ ]?\{/gms, "function FM() {%DMXVARS%\n")}
#else #else
const char PAGE_dmxmap[] PROGMEM = R"=====()====="; const char PAGE_dmxmap[] PROGMEM = R"=====()=====";
#endif #endif
` `,
}, },
{ {
file: "update.htm", file: "update.htm",
@ -324,7 +351,7 @@ const char PAGE_dmxmap[] PROGMEM = R"=====()=====";
prepend: "=====(", prepend: "=====(",
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify" filter: "html-minify",
}, },
{ {
file: "welcome.htm", file: "welcome.htm",
@ -332,7 +359,7 @@ const char PAGE_dmxmap[] PROGMEM = R"=====()=====";
prepend: "=====(", prepend: "=====(",
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify" filter: "html-minify",
}, },
{ {
file: "liveview.htm", file: "liveview.htm",
@ -340,13 +367,13 @@ const char PAGE_dmxmap[] PROGMEM = R"=====()=====";
prepend: "=====(", prepend: "=====(",
append: ")=====", append: ")=====",
method: "plaintext", method: "plaintext",
filter: "html-minify" filter: "html-minify",
}, },
{ {
file: "favicon.ico", file: "favicon.ico",
name: "favicon", name: "favicon",
method: "binary" method: "binary",
} },
], ],
"wled00/html_other.h" "wled00/html_other.h"
); );

View File

@ -89,7 +89,7 @@
<button type="button" onclick="U()">Manual OTA Update</button><br> <button type="button" onclick="U()">Manual OTA Update</button><br>
Enable ArduinoOTA: <input type="checkbox" name="AO"><br> Enable ArduinoOTA: <input type="checkbox" name="AO"><br>
<h3>About</h3> <h3>About</h3>
<a href="https://github.com/Aircoookie/WLED" target="_blank">WLED</a> version 0.10.0<br><br> <a href="https://github.com/Aircoookie/WLED/" target="_blank">WLED</a> version ##VERSION##<!-- Autoreplaced from package.json --><br><br>
<a href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About" target="_blank">Contributors, dependencies and special thanks</a><br> <a href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About" target="_blank">Contributors, dependencies and special thanks</a><br>
A huge thank you to everyone who helped me create WLED!<br><br> A huge thank you to everyone who helped me create WLED!<br><br>
(c) 2016-2019 Christian Schwinne <br> (c) 2016-2019 Christian Schwinne <br>

View File

@ -130,8 +130,7 @@ function GCH(n){for(d=document,d.getElementById("dmxchannels").innerHTML+="",i=0
</head><body onload="S()"><form id="form_s" name="Sf" method="post"> </head><body onload="S()"><form id="form_s" name="Sf" method="post">
<div class="helpB"><button type="button" onclick="H()">?</button></div><button <div class="helpB"><button type="button" onclick="H()">?</button></div><button
type="button" onclick="B()">Back</button><button type="submit">Save</button><hr> type="button" onclick="B()">Back</button><button type="submit">Save</button><hr>
<h2>Imma firin ma lazer (if it has DMX support)</h2> <h2>Imma firin ma lazer (if it has DMX support)</h2>Proxy Universe <input
<!-- TODO: Change to something less-meme-related //--> Proxy Universe <input
name="PU" type="number" min="0" max="63999" required> name="PU" type="number" min="0" max="63999" required>
from E1.31 to DMX (0=disabled)<br><i> from E1.31 to DMX (0=disabled)<br><i>
This will disable the LED data output to DMX configurable below</i><br><br><i> This will disable the LED data output to DMX configurable below</i><br><br><i>
@ -324,7 +323,7 @@ All EEPROM content (settings) will be erased.<br><br>
HTTP traffic is unencrypted. An attacker in the same network can intercept form data! HTTP traffic is unencrypted. An attacker in the same network can intercept form data!
<h3>Software Update</h3><button type="button" onclick="U()">Manual OTA Update <h3>Software Update</h3><button type="button" onclick="U()">Manual OTA Update
</button><br>Enable ArduinoOTA: <input type="checkbox" name="AO"><br><h3>About </button><br>Enable ArduinoOTA: <input type="checkbox" name="AO"><br><h3>About
</h3><a href="https://github.com/Aircoookie/WLED" target="_blank">WLED</a> </h3><a href="https://github.com/Aircoookie/WLED/" target="_blank">WLED</a>
version 0.10.0<br><br><a version 0.10.0<br><br><a
href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About" href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About"
target="_blank">Contributors, dependencies and special thanks</a><br> target="_blank">Contributors, dependencies and special thanks</a><br>