Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Created March 17, 2023 16:53
Show Gist options
  • Save chengjianhua/b510dd56b3e28421496312e97278914e to your computer and use it in GitHub Desktop.
Save chengjianhua/b510dd56b3e28421496312e97278914e to your computer and use it in GitHub Desktop.
Deduplicate rules in .gitignore
#!/usr/bin/env zx
const gitignoreFile = argv._[0];
const content = await fs.readFile(gitignoreFile, { encoding: "utf-8" });
const rulesSet = new Set();
const lines = content.split(`\n`);
const newLines = lines.filter((l) => {
const isComment = !l.trim().startsWith("#");
if (!isComment) {
if (!l) {
if (!rulesSet.has(l)) {
rulesSet.add(l);
} else {
return false;
}
}
}
return true;
});
const newContent = newLines.join("\n");
console.log(newContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment