Format new code
parent
2495627f3e
commit
141dc3da00
|
|
@ -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\/|\/)?(?<path>.*\.js)$/ui);
|
||||
const filePathMatch = filePathInput?.match(
|
||||
/^(?:.*?\/apps\/|apps\/|\/)?(?<path>.*\.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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue