mirror of
https://github.com/wled/WLED.git
synced 2025-07-29 13:46:39 +00:00
refactor cdata.js
This commit is contained in:
parent
3bca4a73f4
commit
5120045459
@ -34,6 +34,7 @@ describe('Functions', () => {
|
|||||||
// Create a new file
|
// Create a new file
|
||||||
fs.writeFileSync(newFilePath, 'This is a new file.');
|
fs.writeFileSync(newFilePath, 'This is a new file.');
|
||||||
});
|
});
|
||||||
|
|
||||||
// delete the temporary file after the test
|
// delete the temporary file after the test
|
||||||
after(() => {
|
after(() => {
|
||||||
fs.unlinkSync('temp.txt');
|
fs.unlinkSync('temp.txt');
|
||||||
|
137
tools/cdata.js
137
tools/cdata.js
@ -23,10 +23,43 @@ const CleanCSS = require("clean-css");
|
|||||||
const minifyHtml = require('html-minifier-terser').minify;
|
const minifyHtml = require('html-minifier-terser').minify;
|
||||||
const packageJson = require("../package.json");
|
const packageJson = require("../package.json");
|
||||||
|
|
||||||
|
// Export functions for testing
|
||||||
module.exports = { isFileNewerThan, isAnyFileInFolderNewerThan };
|
module.exports = { isFileNewerThan, isAnyFileInFolderNewerThan };
|
||||||
|
|
||||||
const output = ["wled00/html_ui.h", "wled00/html_pixart.h", "wled00/html_cpal.h", "wled00/html_pxmagic.h", "wled00/html_settings.h", "wled00/html_other.h"]
|
const output = ["wled00/html_ui.h", "wled00/html_pixart.h", "wled00/html_cpal.h", "wled00/html_pxmagic.h", "wled00/html_settings.h", "wled00/html_other.h"]
|
||||||
|
|
||||||
|
// \x1b[34m is blue, \x1b[36m is cyan, \x1b[0m is reset
|
||||||
|
const wledBanner = `\x1b[34m
|
||||||
|
\t## ## ## ######## ########
|
||||||
|
\t## ## ## ## ## ## ##
|
||||||
|
\t## ## ## ## ## ## ##
|
||||||
|
\t## ## ## ## ###### ## ##
|
||||||
|
\t## ## ## ## ## ## ##
|
||||||
|
\t## ## ## ## ## ## ##
|
||||||
|
\t ### ### ######## ######## ########
|
||||||
|
\t\t\x1b[36mbuild script for web UI
|
||||||
|
\x1b[0m`;
|
||||||
|
|
||||||
|
const singleHeader = `/*
|
||||||
|
* Binary array for the Web UI.
|
||||||
|
* gzip is used for smaller size and improved speeds.
|
||||||
|
*
|
||||||
|
* Please see https://kno.wled.ge/advanced/custom-features/#changing-web-ui
|
||||||
|
* to find out how to easily modify the web UI source!
|
||||||
|
*/
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
|
const multiHeader = `/*
|
||||||
|
* More web UI HTML source arrays.
|
||||||
|
* This file is auto generated, please don't make any changes manually.
|
||||||
|
*
|
||||||
|
* Instead, see https://kno.wled.ge/advanced/custom-features/#changing-web-ui
|
||||||
|
* to find out how to easily modify the web UI source!
|
||||||
|
*/
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
function hexdump(buffer, isHex = false) {
|
function hexdump(buffer, isHex = false) {
|
||||||
let lines = [];
|
let lines = [];
|
||||||
|
|
||||||
@ -53,22 +86,17 @@ function hexdump(buffer, isHex = false) {
|
|||||||
return lines.join(",\n");
|
return lines.join(",\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
function strReplace(str, search, replacement) {
|
|
||||||
return str.split(search).join(replacement);
|
|
||||||
}
|
|
||||||
|
|
||||||
function adoptVersionAndRepo(html) {
|
function adoptVersionAndRepo(html) {
|
||||||
let repoUrl = packageJson.repository ? packageJson.repository.url : undefined;
|
let repoUrl = packageJson.repository ? packageJson.repository.url : undefined;
|
||||||
if (repoUrl) {
|
if (repoUrl) {
|
||||||
repoUrl = repoUrl.replace(/^git\+/, "");
|
repoUrl = repoUrl.replace(/^git\+/, "");
|
||||||
repoUrl = repoUrl.replace(/\.git$/, "");
|
repoUrl = repoUrl.replace(/\.git$/, "");
|
||||||
// Replace we
|
html = html.replaceAll("https://github.com/atuline/WLED", repoUrl);
|
||||||
html = strReplace(html, "https://github.com/atuline/WLED", repoUrl);
|
html = html.replaceAll("https://github.com/Aircoookie/WLED", repoUrl);
|
||||||
html = strReplace(html, "https://github.com/Aircoookie/WLED", repoUrl);
|
|
||||||
}
|
}
|
||||||
let version = packageJson.version;
|
let version = packageJson.version;
|
||||||
if (version) {
|
if (version) {
|
||||||
html = strReplace(html, "##VERSION##", version);
|
html = html.replaceAll("##VERSION##", version);
|
||||||
}
|
}
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
@ -102,45 +130,26 @@ async function minify(str, type = "plain") {
|
|||||||
async function writeHtmlGzipped(sourceFile, resultFile, page) {
|
async function writeHtmlGzipped(sourceFile, resultFile, page) {
|
||||||
console.info("Reading " + sourceFile);
|
console.info("Reading " + sourceFile);
|
||||||
new inliner(sourceFile, async function (error, html) {
|
new inliner(sourceFile, async function (error, html) {
|
||||||
if (error) {
|
if (error) throw error;
|
||||||
console.error(error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
html = adoptVersionAndRepo(html);
|
html = adoptVersionAndRepo(html);
|
||||||
console.info("Inlined " + html.length + " characters");
|
console.info("Inlined " + html.length + " characters");
|
||||||
html = await minify(html, "html-minify-ui");
|
html = await minify(html, "html-minify-ui");
|
||||||
console.info("Minified to " + html.length + " characters");
|
console.info("Minified to " + html.length + " characters");
|
||||||
zlib.gzip(html, { level: zlib.constants.Z_BEST_COMPRESSION }, function (error, result) {
|
const result = zlib.gzipSync(html, { level: zlib.constants.Z_BEST_COMPRESSION });
|
||||||
if (error) {
|
console.info("Compressed " + result.length + " bytes");
|
||||||
console.error(error);
|
const array = hexdump(result);
|
||||||
throw error;
|
let src = singleHeader;
|
||||||
}
|
src += `const uint16_t PAGE_${page}_L = ${result.length};`;
|
||||||
|
src += `const uint8_t PAGE_${page}[] PROGMEM = {\n${array}\n};\n\n`;
|
||||||
console.info("Compressed " + result.length + " bytes");
|
console.info("Writing " + resultFile);
|
||||||
const array = hexdump(result);
|
fs.writeFileSync(resultFile, src);
|
||||||
const src = `/*
|
|
||||||
* Binary array for the Web UI.
|
|
||||||
* gzip is used for smaller size and improved speeds.
|
|
||||||
*
|
|
||||||
* Please see https://kno.wled.ge/advanced/custom-features/#changing-web-ui
|
|
||||||
* to find out how to easily modify the web UI source!
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Autogenerated from ${sourceFile}, do not edit!!
|
|
||||||
const uint16_t PAGE_${page}_L = ${result.length};
|
|
||||||
const uint8_t PAGE_${page}[] PROGMEM = {
|
|
||||||
${array}
|
|
||||||
};
|
|
||||||
`;
|
|
||||||
console.info("Writing " + resultFile);
|
|
||||||
fs.writeFileSync(resultFile, src);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function specToChunk(srcDir, s) {
|
async function specToChunk(srcDir, s) {
|
||||||
const buf = fs.readFileSync(srcDir + "/" + s.file);
|
const buf = fs.readFileSync(srcDir + "/" + s.file);
|
||||||
|
let chunk = `// Autogenerated from ${srcDir}/${s.file}, do not edit!!\n`
|
||||||
|
|
||||||
if (s.method == "plaintext" || s.method == "gzip") {
|
if (s.method == "plaintext" || s.method == "gzip") {
|
||||||
let str = buf.toString("utf-8");
|
let str = buf.toString("utf-8");
|
||||||
@ -148,60 +157,30 @@ async function specToChunk(srcDir, s) {
|
|||||||
if (s.method == "gzip") {
|
if (s.method == "gzip") {
|
||||||
if (s.mangle) str = s.mangle(str);
|
if (s.mangle) str = s.mangle(str);
|
||||||
const zip = zlib.gzipSync(await minify(str, s.filter), { level: zlib.constants.Z_BEST_COMPRESSION });
|
const zip = zlib.gzipSync(await minify(str, s.filter), { level: zlib.constants.Z_BEST_COMPRESSION });
|
||||||
const result = hexdump(zip.toString('hex'), true);
|
const result = hexdump(zip);
|
||||||
return `
|
chunk += `const uint16_t ${s.name}_length = ${zip.length};\n`;
|
||||||
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
|
chunk += `const uint8_t ${s.name}[] PROGMEM = {\n${result}\n};\n\n`;
|
||||||
const uint16_t ${s.name}_length = ${zip.length};
|
return chunk;
|
||||||
const uint8_t ${s.name}[] PROGMEM = {
|
|
||||||
${result}
|
|
||||||
};
|
|
||||||
|
|
||||||
`;
|
|
||||||
} else {
|
} else {
|
||||||
const chunk = `
|
chunk += `const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${await minify(str, s.filter)}${s.append || ""}";\n\n`;
|
||||||
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
|
|
||||||
const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${await minify(str, s.filter)}${s.append || ""
|
|
||||||
}";
|
|
||||||
|
|
||||||
`;
|
|
||||||
return s.mangle ? s.mangle(chunk) : chunk;
|
return s.mangle ? s.mangle(chunk) : chunk;
|
||||||
}
|
}
|
||||||
} else if (s.method == "binary") {
|
} else if (s.method == "binary") {
|
||||||
const result = hexdump(buf);
|
const result = hexdump(buf);
|
||||||
return `
|
chunk += `const uint16_t ${s.name}_length = ${buf.length};\n`;
|
||||||
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
|
chunk += `const uint8_t ${s.name}[] PROGMEM = {\n${result}\n};\n\n`;
|
||||||
const uint16_t ${s.name}_length = ${buf.length};
|
return chunk;
|
||||||
const uint8_t ${s.name}[] PROGMEM = {
|
|
||||||
${result}
|
|
||||||
};
|
|
||||||
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.warn("Unknown method: " + s.method);
|
|
||||||
throw new Error("Unknown method: " + s.method);
|
throw new Error("Unknown method: " + s.method);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeChunks(srcDir, specs, resultFile) {
|
async function writeChunks(srcDir, specs, resultFile) {
|
||||||
let src = `/*
|
let src = multiHeader;
|
||||||
* More web UI HTML source arrays.
|
|
||||||
* This file is auto generated, please don't make any changes manually.
|
|
||||||
* Instead, see https://kno.wled.ge/advanced/custom-features/#changing-web-ui
|
|
||||||
* to find out how to easily modify the web UI source!
|
|
||||||
*/
|
|
||||||
`;
|
|
||||||
for (const s of specs) {
|
for (const s of specs) {
|
||||||
const file = srcDir + "/" + s.file;
|
const file = srcDir + "/" + s.file;
|
||||||
try {
|
console.info("Reading " + file + " as " + s.name);
|
||||||
console.info("Reading " + file + " as " + s.name);
|
src += await specToChunk(srcDir, s);
|
||||||
src += await specToChunk(srcDir, s);
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(
|
|
||||||
"Failed " + s.name + " from " + file,
|
|
||||||
e.message.length > 60 ? e.message.substring(0, 60) : e.message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.info("Writing " + src.length + " characters into " + resultFile);
|
console.info("Writing " + src.length + " characters into " + resultFile);
|
||||||
fs.writeFileSync(resultFile, src);
|
fs.writeFileSync(resultFile, src);
|
||||||
@ -254,6 +233,8 @@ if (process.env.NODE_ENV === 'test') {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.info(wledBanner);
|
||||||
|
|
||||||
if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.argv[2] !== '-f') {
|
if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.argv[2] !== '-f') {
|
||||||
console.info("Web UI is already built");
|
console.info("Web UI is already built");
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user