Skip to content

Commit

Permalink
add a GitHub workflow to check cspell lists
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed Sep 28, 2024
1 parent 4d112b4 commit 06d6a68
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions scripts/sort_and_unique_file_lines.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from "node:fs";
import path from "node:path";

const filePath = process.argv[2];
const check = process.argv[3] === "--check";

if (!fs.existsSync(filePath)) {
console.error(`File ${filePath} doesn't exist.`);
console.warn(
"Usage:\n\tnode scripts/sort_and_unique_file_lines.js <filePath> [--check]",
);
process.exit(1);
}

Expand All @@ -13,14 +16,12 @@ function equalsIgnoreCase(string1, string2) {
}

const uniq = [];
const lines = fs
.readFileSync(filePath, "utf-8")
.split("\n")
.sort((a, b) => {
a = a.toUpperCase();
b = b.toUpperCase();
return a < b ? -1 : a > b ? 1 : 0;
});
const content = fs.readFileSync(filePath, "utf-8");
const lines = content.split("\n").sort((a, b) => {
a = a.toUpperCase();
b = b.toUpperCase();
return a < b ? -1 : a > b ? 1 : 0;
});

for (let i = 0; i < lines.length; ) {
const line = lines[i];
Expand All @@ -32,4 +33,17 @@ for (let i = 0; i < lines.length; ) {
}
}

fs.writeFileSync(filePath, uniq.join("\n"));
const sortedContent = uniq.join("\n") + "\n";
if (check) {
if (content !== sortedContent) {
console.error(
`The file is not formatted properly. Run 'node scripts/sort_and_unique_file_lines.js ${filePath}' to format the file.`,
);
process.exit(1);
} else {
console.log("The file looks good.");
process.exit(0);
}
}

fs.writeFileSync(filePath, sortedContent);

0 comments on commit 06d6a68

Please sign in to comment.