Created
March 17, 2023 16:53
-
-
Save chengjianhua/b510dd56b3e28421496312e97278914e to your computer and use it in GitHub Desktop.
Deduplicate rules in .gitignore
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
#!/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