Skip to content

Commit

Permalink
Add -staged commands and git pre-commit hook
Browse files Browse the repository at this point in the history
Running all of the linters on all of the files is resource-intensive and
not very fun. In a perfect world, software engineers should only have to
worry about checking their staged files and they should be able to trust
that the rest of the files are already linted.

Using the -staged suffix runs all fixers and testers (with the exception
of depcheck) on only the staged files, and any mutations (e.g. Prettier
formatting) are also added to the staged file so that it can be
committed.
  • Loading branch information
christianbundy committed Oct 7, 2020
1 parent ed5bc7c commit 9b6008c
Show file tree
Hide file tree
Showing 6 changed files with 611 additions and 21 deletions.
1 change: 1 addition & 0 deletions .depcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignores: ["depcheck", "eslint", "prettier", "stylelint", "typescript", "stylelint-config-recommended", "lint-staged"]
5 changes: 5 additions & 0 deletions .lint-staged-fix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*": "prettier --write",
"*.css": "stylelint --fix --allow-empty-input",
"*.{js,jsx,md,ts,tsx}": "eslint -c .eslintrc.js --fix"
}
5 changes: 5 additions & 0 deletions .lint-staged-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.{js,jsx,md,ts,tsx}": "eslint -c .eslintrc.js ",
"*.css": "stylelint --config ${stylelintConfig} --allow-empty-input",
"*": "prettier --check"
}
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const fs = require("fs");
const path = require("path");
const crossSpawn = require("cross-spawn");
const resolveBin = require("resolve-bin");

const packageString = fs.readFileSync(
path.join(process.cwd(), "package.json"),
Expand Down Expand Up @@ -33,6 +32,16 @@ const fixCommands = [
`eslint -c ${eslintConfig} --fix **/*.{js,jsx,md,ts,tsx}`,
];

const lintStagedFix = path.join(__dirname, ".lint-staged-fix.json");
const lintStagedTest = path.join(__dirname, ".lint-staged-test.json");

const testStagedCommands = [
`lint-staged --config ${lintStagedTest}`,
"depcheck",
];

const fixStagedCommands = [`lint-staged --config ${lintStagedFix}`];

const run = (command) => {
const moduleName = command.split(" ")[0];
const restOfCommand = command.split(" ").slice(1);
Expand All @@ -46,8 +55,7 @@ const run = (command) => {
restOfCommand.push(...suffixArgs);
}
process.stdout.write(`=> ${moduleName} ${restOfCommand.join(" ")}\n`);
const bin = resolveBin.sync(moduleName);
const result = crossSpawn.sync(bin, restOfCommand, {
const result = crossSpawn.sync("npx", [moduleName, ...restOfCommand], {
cwd: process.cwd(),
stdio: "inherit",
});
Expand All @@ -73,6 +81,13 @@ if (subCommand === "test") {
} else if (subCommand === "both") {
runAll(fixCommands);
runAll(testCommands);
} else if (subCommand === "test-staged") {
runAll(testStagedCommands);
} else if (subCommand === "fix-staged") {
runAll(fixStagedCommands);
} else if (subCommand === "both-staged") {
runAll(fixStagedCommands);
runAll(testStagedCommands);
} else {
console.log("Usage: common-good <test|fix|both>");
console.log("Usage: common-good <test|fix|both>[-staged]");
}
Loading

0 comments on commit 9b6008c

Please sign in to comment.