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

View File

@ -89,7 +89,7 @@
<button type="button" onclick="U()">Manual OTA Update</button><br>
Enable ArduinoOTA: <input type="checkbox" name="AO"><br>
<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 huge thank you to everyone who helped me create WLED!<br><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">
<div class="helpB"><button type="button" onclick="H()">?</button></div><button
type="button" onclick="B()">Back</button><button type="submit">Save</button><hr>
<h2>Imma firin ma lazer (if it has DMX support)</h2>
<!-- TODO: Change to something less-meme-related //--> Proxy Universe <input
<h2>Imma firin ma lazer (if it has DMX support)</h2>Proxy Universe <input
name="PU" type="number" min="0" max="63999" required>
from E1.31 to DMX (0=disabled)<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!
<h3>Software Update</h3><button type="button" onclick="U()">Manual OTA Update
</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
href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About"
target="_blank">Contributors, dependencies and special thanks</a><br>