mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-11 03:13:35 +00:00
chore: adding scripts to validate util
This commit is contained in:
1 parent
85a2d46feb
commit
335496335b
3 files changed
+91
-1
No files matched your search
+3
-1
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "npx ava tests/*.test.js"
|
||||
"test": "npx ava tests/*.test.js",
|
||||
"check:reserved-order": "node util/check-reserved-order.cjs",
|
||||
"add:reserved": "node util/add-reserved-subdomain.cjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^8.0.1",
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
const { readFileSync, writeFileSync } = require("node:fs");
|
||||
const { join } = require("node:path");
|
||||
const readline = require("node:readline");
|
||||
|
||||
const filePath = join(__dirname, "reserved.json");
|
||||
const prompt = "What subdomain would you want to add to the reserved.json list: ";
|
||||
|
||||
const interface = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
|
||||
interface.question(prompt, (input) => {
|
||||
const subdomain = input.trim();
|
||||
|
||||
if (!subdomain) {
|
||||
console.error("Subdomain cannot be empty.");
|
||||
interface.close();
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (subdomain !== subdomain.toLowerCase() || /\s/.test(subdomain)) {
|
||||
console.error("Subdomain must be lowercase and cannot contain whitespace.");
|
||||
interface.close();
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const fileContents = readFileSync(filePath, "utf8");
|
||||
const reserved = JSON.parse(fileContents);
|
||||
|
||||
if (reserved.includes(subdomain)) {
|
||||
console.error(`"${subdomain}" is already in reserved.json.`);
|
||||
interface.close();
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const insertionIndex = reserved.findIndex(
|
||||
(entry) => entry > subdomain,
|
||||
);
|
||||
const index = insertionIndex === -1 ? reserved.length : insertionIndex;
|
||||
reserved.splice(index, 0, subdomain);
|
||||
|
||||
const newline = fileContents.includes("\r\n") ? "\r\n" : "\n";
|
||||
const output = `${JSON.stringify(reserved, null, 4)}${newline}`;
|
||||
writeFileSync(filePath, output);
|
||||
|
||||
const lineNumber = index + 2;
|
||||
const precedingEntry = index > 0 ? reserved[index - 1] : "[";
|
||||
const followingEntry = index < reserved.length - 1 ? reserved[index + 1] : "]";
|
||||
console.log(
|
||||
`Added "${subdomain}" on line ${lineNumber}, after "${precedingEntry}" and before "${followingEntry}".`,
|
||||
);
|
||||
interface.close();
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
const { readFileSync } = require("node:fs");
|
||||
const { join } = require("node:path");
|
||||
|
||||
const filePath = join(__dirname, "reserved.json");
|
||||
const fileContents = readFileSync(filePath, "utf8");
|
||||
const reserved = JSON.parse(fileContents);
|
||||
|
||||
for (let index = 1; index < reserved.length; index += 1) {
|
||||
if (reserved[index - 1] > reserved[index]) {
|
||||
let precedingIndex = index - 1;
|
||||
|
||||
while (
|
||||
precedingIndex >= 0 &&
|
||||
reserved[precedingIndex] > reserved[index]
|
||||
) {
|
||||
precedingIndex -= 1;
|
||||
}
|
||||
|
||||
const precedingLine = precedingIndex + 2;
|
||||
|
||||
console.error(
|
||||
`reserved.json is not in alphabetical order: line ${index + 2} ("${reserved[index]}") should go after line ${precedingLine} ("${precedingIndex >= 0 ? reserved[precedingIndex] : "["}") and before line ${index + 1} ("${reserved[index - 1]}").`,
|
||||
);
|
||||
process.exitCode = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (process.exitCode !== 1) {
|
||||
console.log("reserved.json is in alphabetical order.");
|
||||
}
|
||||
Reference in new issue
Block a user