forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5e089d
commit b44bcb1
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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); | ||
} | ||
|
||
function equalsIgnoreCase(string1, string2) { | ||
return new RegExp(`^${string1}$`, "gi").test(string2); | ||
} | ||
|
||
const uniq = []; | ||
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]; | ||
if (line.trim() !== "") { | ||
uniq.push(line); | ||
while (equalsIgnoreCase(line, lines[++i])); | ||
} else { | ||
i++; | ||
} | ||
} | ||
|
||
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); |