diff --git a/bin/exempt-lint.mjs b/bin/exempt-lint.mjs index f2ca64fc8..08652ad05 100755 --- a/bin/exempt-lint.mjs +++ b/bin/exempt-lint.mjs @@ -1,15 +1,15 @@ #!/usr/bin/env node -/** +/** * @file * You can use this script to exempt an app file from a specific eslint rule. - * + * * This should only be used to exempt existing apps when a new lint rule is added. * You are not allowed to exempt your new app from existing lint rules. - * + * * Run it like this: * node bin/exempt-lint.mjs LINTRULE FILEPATH - * + * * Example command: * node bin/exempt-lint.mjs no-unused-vars ./apps/_example_app/app.js */ @@ -17,24 +17,28 @@ import fs from "node:fs/promises"; const lintRule = process.argv[2]; -if(!lintRule){ - throw new Error("First argument needs to be a lint rule, something like 'no-unused-vars'"); +if (!lintRule) { + throw new Error( + "First argument needs to be a lint rule, something like 'no-unused-vars'", + ); } const filePathInput = process.argv[3]; -const filePathMatch = filePathInput?.match(/^(?:.*?\/apps\/|apps\/|\/)?(?.*\.js)$/ui); +const filePathMatch = filePathInput?.match( + /^(?:.*?\/apps\/|apps\/|\/)?(?.*\.js)$/iu, +); const filePath = filePathMatch?.groups?.path; -if(!filePath){ - throw new Error("Second argument needs to be a file path that looks something like './apps/_example_app/app.js'"); +if (!filePath) { + 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 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] || {}; exemption.hash = await hashContents(fileContents); @@ -43,24 +47,20 @@ rules.add(lintRule); exemption.rules = [...rules]; exemptions[filePath] = exemption; - - 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}'`); - - /** * https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string */ async function hashContents(message) { - const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array - 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 hashHex = hashArray - .map((b) => b.toString(16).padStart(2, "0")) - .join(""); // convert bytes to hex string - return hashHex; + const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array + 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 hashHex = hashArray + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); // convert bytes to hex string + return hashHex; } diff --git a/bin/sync-lint-exemptions.mjs b/bin/sync-lint-exemptions.mjs index ee024ac84..cbf18fe3c 100755 --- a/bin/sync-lint-exemptions.mjs +++ b/bin/sync-lint-exemptions.mjs @@ -1,10 +1,10 @@ #!/usr/bin/env node -/** +/** * @file * Run this to ensure that the lint exemptions are all valid. * If any of the exempt app files have been changed, this script will remove the exemption for that file. - * + * * Run it like this: * node bin/sync-lint-exemptions.mjs */ @@ -15,32 +15,31 @@ const exemptionsFilePath = "../apps/lint_exemptions.js"; const exemptions = (await import(exemptionsFilePath)).default; -for(const filePath of Object.keys(exemptions)){ - const fileContents = await fs.readFile("apps/" + filePath, "utf8"); - const currentHash = await hashContents(fileContents); - if(exemptions[filePath].hash !== currentHash){ - delete exemptions[filePath]; - console.log(`! Removed lint exemptions for '${filePath}' because it has been edited`); - } +for (const filePath of Object.keys(exemptions)) { + const fileContents = await fs.readFile(`apps/${filePath}`, "utf8"); + const currentHash = await hashContents(fileContents); + if (exemptions[filePath].hash !== currentHash) { + delete exemptions[filePath]; + console.log( + `! Removed lint exemptions for '${filePath}' because it has been edited`, + ); + } } - const output = `module.exports = ${JSON.stringify(exemptions, undefined, 2)};\n`; await fs.writeFile("bin/" + exemptionsFilePath, output); 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 */ async function hashContents(message) { - const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array - 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 hashHex = hashArray - .map((b) => b.toString(16).padStart(2, "0")) - .join(""); // convert bytes to hex string - return hashHex; + const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array + 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 hashHex = hashArray + .map((b) => b.toString(16).padStart(2, "0")) + .join(""); // convert bytes to hex string + return hashHex; }