Skip to content

Instantly share code, notes, and snippets.

@pdehaan
Created June 14, 2022 19:35
Show Gist options
  • Save pdehaan/d97caecbc670099228cb7792fbf0156b to your computer and use it in GitHub Desktop.
Save pdehaan/d97caecbc670099228cb7792fbf0156b to your computer and use it in GitHub Desktop.
Get a list of all Prettier-able files in a repo.
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());
}
@pdehaan
Copy link
Author

pdehaan commented Jun 14, 2022

Map(15) {
  '.css' => 9,
  '.gql' => 1,
  '.handlebars' => 1,
  '.hbs' => 1,
  '.html' => 20,
  '.js' => 1452,
  '.json' => 247,
  '.md' => 34,
  '.mjml' => 72,
  '.mjs' => 3,
  '.scss' => 80,
  '.ts' => 651,
  '.tsx' => 320,
  '.yaml' => 2,
  '.yml' => 3,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment