Created
June 14, 2022 19:35
-
-
Save pdehaan/d97caecbc670099228cb7792fbf0156b to your computer and use it in GitHub Desktop.
Get a list of all Prettier-able files in a repo.
This file contains hidden or 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
import { execSync } from "node:child_process"; | |
import path from "node:path"; | |
import minimatch from "minimatch"; | |
import prettier from "prettier"; | |
const ignores = [ | |
".circleci/**", | |
".github/**", | |
".vscode/**", | |
".yarn/**", | |
"docs/**", | |
"**/CHANGELOG.md", | |
"**/README.md", | |
"**/ansible/**", | |
]; | |
const files = getPrettyableFiles(ignores); | |
console.log(files.join("\n")); | |
const exts = getPrettyableExts(files); | |
console.log(exts); | |
function getPrettyableFiles(ignores = []) { | |
const supportedExts = getSupportedExts(); | |
return gitLsFiles().filter((file) => { | |
for (const rule of ignores) { | |
if (minimatch(file, rule, { dot: true })) { | |
return false; | |
} | |
} | |
const ext = path.extname(file); | |
return supportedExts.has(ext); | |
}); | |
} | |
function gitLsFiles() { | |
return execSync("git ls-files").toString().trim().split("\n"); | |
} | |
function getPrettyableExts(files = []) { | |
const extMap = new Map(); | |
for (const file of files) { | |
const ext = path.extname(file); | |
const count = extMap.get(ext) || 0; | |
extMap.set(ext, count + 1); | |
} | |
return extMap; | |
} | |
function getSupportedExts() { | |
const { languages } = prettier.getSupportInfo(); | |
const lintables = languages.reduce((acc = [], lang = {}) => { | |
return acc.concat(lang.extensions); | |
}, []); | |
return new Set(lintables.sort()); | |
} |
Author
pdehaan
commented
Jun 14, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment