Format new code

master
Anton 2024-03-05 12:47:04 +01:00
parent 2495627f3e
commit 141dc3da00
2 changed files with 42 additions and 43 deletions

View File

@ -17,24 +17,28 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
const lintRule = process.argv[2]; const lintRule = process.argv[2];
if(!lintRule){ if (!lintRule) {
throw new Error("First argument needs to be a lint rule, something like 'no-unused-vars'"); throw new Error(
"First argument needs to be a lint rule, something like 'no-unused-vars'",
);
} }
const filePathInput = process.argv[3]; const filePathInput = process.argv[3];
const filePathMatch = filePathInput?.match(/^(?:.*?\/apps\/|apps\/|\/)?(?<path>.*\.js)$/ui); const filePathMatch = filePathInput?.match(
/^(?:.*?\/apps\/|apps\/|\/)?(?<path>.*\.js)$/iu,
);
const filePath = filePathMatch?.groups?.path; const filePath = filePathMatch?.groups?.path;
if(!filePath){ if (!filePath) {
throw new Error("Second argument needs to be a file path that looks something like './apps/_example_app/app.js'"); throw new Error(
"Second argument needs to be a file path that looks something like './apps/_example_app/app.js'",
);
} }
const exemptionsFilePath = "../apps/lint_exemptions.js"; const exemptionsFilePath = "../apps/lint_exemptions.js";
const exemptions = (await import(exemptionsFilePath)).default; const exemptions = (await import(exemptionsFilePath)).default;
const fileContents = await fs.readFile("apps/" + filePath, "utf8"); const fileContents = await fs.readFile(`apps/${filePath}`, "utf8");
const exemption = exemptions[filePath] || {}; const exemption = exemptions[filePath] || {};
exemption.hash = await hashContents(fileContents); exemption.hash = await hashContents(fileContents);
@ -43,24 +47,20 @@ rules.add(lintRule);
exemption.rules = [...rules]; exemption.rules = [...rules];
exemptions[filePath] = exemption; exemptions[filePath] = exemption;
const output = `module.exports = ${JSON.stringify(exemptions, undefined, 2)};\n`; const output = `module.exports = ${JSON.stringify(exemptions, undefined, 2)};\n`;
await fs.writeFile("bin/" + exemptionsFilePath, output); await fs.writeFile(`bin/${exemptionsFilePath}`, output);
console.log(`✔️ '${filePath}' is now exempt from the rule '${lintRule}'`); console.log(`✔️ '${filePath}' is now exempt from the rule '${lintRule}'`);
/** /**
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string * https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
*/ */
async function hashContents(message) { async function hashContents(message) {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0")) .map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string .join(""); // convert bytes to hex string
return hashHex; return hashHex;
} }

View File

@ -15,32 +15,31 @@ const exemptionsFilePath = "../apps/lint_exemptions.js";
const exemptions = (await import(exemptionsFilePath)).default; const exemptions = (await import(exemptionsFilePath)).default;
for(const filePath of Object.keys(exemptions)){ for (const filePath of Object.keys(exemptions)) {
const fileContents = await fs.readFile("apps/" + filePath, "utf8"); const fileContents = await fs.readFile(`apps/${filePath}`, "utf8");
const currentHash = await hashContents(fileContents); const currentHash = await hashContents(fileContents);
if(exemptions[filePath].hash !== currentHash){ if (exemptions[filePath].hash !== currentHash) {
delete exemptions[filePath]; delete exemptions[filePath];
console.log(`! Removed lint exemptions for '${filePath}' because it has been edited`); console.log(
} `! Removed lint exemptions for '${filePath}' because it has been edited`,
);
}
} }
const output = `module.exports = ${JSON.stringify(exemptions, undefined, 2)};\n`; const output = `module.exports = ${JSON.stringify(exemptions, undefined, 2)};\n`;
await fs.writeFile("bin/" + exemptionsFilePath, output); await fs.writeFile("bin/" + exemptionsFilePath, output);
console.log(`✔️ Synchronized all lint exemptions\n`); console.log(`✔️ Synchronized all lint exemptions\n`);
/** /**
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string * https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
*/ */
async function hashContents(message) { async function hashContents(message) {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0")) .map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string .join(""); // convert bytes to hex string
return hashHex; return hashHex;
} }