Skip to content

Commit

Permalink
refactor cdata.js
Browse files Browse the repository at this point in the history
  • Loading branch information
w00000dy committed Jan 18, 2024
1 parent 3bca4a7 commit 5120045
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 78 deletions.
1 change: 1 addition & 0 deletions tools/cdata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Functions', () => {
// Create a new file
fs.writeFileSync(newFilePath, 'This is a new file.');
});

// delete the temporary file after the test
after(() => {
fs.unlinkSync('temp.txt');
Expand Down
137 changes: 59 additions & 78 deletions tools/cdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,43 @@ const CleanCSS = require("clean-css");
const minifyHtml = require('html-minifier-terser').minify;
const packageJson = require("../package.json");

// Export functions for testing
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"]

// \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) {
let lines = [];

Expand All @@ -53,22 +86,17 @@ function hexdump(buffer, isHex = false) {
return lines.join(",\n");
}

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);
html = html.replaceAll("https://github.com/atuline/WLED", repoUrl);
html = html.replaceAll("https://github.com/Aircoookie/WLED", repoUrl);
}
let version = packageJson.version;
if (version) {
html = strReplace(html, "##VERSION##", version);
html = html.replaceAll("##VERSION##", version);
}
return html;
}
Expand Down Expand Up @@ -102,106 +130,57 @@ async function minify(str, type = "plain") {
async function writeHtmlGzipped(sourceFile, resultFile, page) {
console.info("Reading " + sourceFile);
new inliner(sourceFile, async function (error, html) {
if (error) {
console.error(error);
throw error;
}
if (error) throw error;

html = adoptVersionAndRepo(html);
console.info("Inlined " + html.length + " characters");
html = await minify(html, "html-minify-ui");
console.info("Minified to " + html.length + " characters");
zlib.gzip(html, { level: zlib.constants.Z_BEST_COMPRESSION }, function (error, result) {
if (error) {
console.error(error);
throw error;
}

console.info("Compressed " + result.length + " bytes");
const array = hexdump(result);
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);
});
const result = zlib.gzipSync(html, { level: zlib.constants.Z_BEST_COMPRESSION });
console.info("Compressed " + result.length + " bytes");
const array = hexdump(result);
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("Writing " + resultFile);
fs.writeFileSync(resultFile, src);
});
}

async function specToChunk(srcDir, s) {
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") {
let str = buf.toString("utf-8");
str = adoptVersionAndRepo(str);
if (s.method == "gzip") {
if (s.mangle) str = s.mangle(str);
const zip = zlib.gzipSync(await minify(str, s.filter), { level: zlib.constants.Z_BEST_COMPRESSION });
const result = hexdump(zip.toString('hex'), true);
return `
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
const uint16_t ${s.name}_length = ${zip.length};
const uint8_t ${s.name}[] PROGMEM = {
${result}
};
`;
const result = hexdump(zip);
chunk += `const uint16_t ${s.name}_length = ${zip.length};\n`;
chunk += `const uint8_t ${s.name}[] PROGMEM = {\n${result}\n};\n\n`;
return chunk;
} else {
const chunk = `
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${await minify(str, s.filter)}${s.append || ""
}";
`;
chunk += `const char ${s.name}[] PROGMEM = R"${s.prepend || ""}${await minify(str, s.filter)}${s.append || ""}";\n\n`;
return s.mangle ? s.mangle(chunk) : chunk;
}
} else if (s.method == "binary") {
const result = hexdump(buf);
return `
// Autogenerated from ${srcDir}/${s.file}, do not edit!!
const uint16_t ${s.name}_length = ${buf.length};
const uint8_t ${s.name}[] PROGMEM = {
${result}
};
`;
chunk += `const uint16_t ${s.name}_length = ${buf.length};\n`;
chunk += `const uint8_t ${s.name}[] PROGMEM = {\n${result}\n};\n\n`;
return chunk;
}

console.warn("Unknown method: " + s.method);
throw new Error("Unknown method: " + s.method);

}

async function writeChunks(srcDir, specs, resultFile) {
let src = `/*
* 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!
*/
`;
let src = multiHeader;
for (const s of specs) {
const file = srcDir + "/" + s.file;
try {
console.info("Reading " + file + " as " + s.name);
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("Reading " + file + " as " + s.name);
src += await specToChunk(srcDir, s);
}
console.info("Writing " + src.length + " characters into " + resultFile);
fs.writeFileSync(resultFile, src);
Expand Down Expand Up @@ -254,6 +233,8 @@ if (process.env.NODE_ENV === 'test') {
return;
}

console.info(wledBanner);

if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.argv[2] !== '-f') {
console.info("Web UI is already built");
return;
Expand Down

0 comments on commit 5120045

Please sign in to comment.