Skip to content

Commit

Permalink
chore: add precommit hook to format files (zwave-js#6205)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Aug 24, 2023
1 parent 1faa09c commit e8ebb4d
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .dprint.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"prettier": {
"associations": [
// Prettier formats those better
"package.json",
"**/package.json",
"packages/nvmedit/test/fixtures/**/*.json",
"**/*.yml",
"**/*.yaml"
Expand Down
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ if ! yarn run check-references >/dev/null 2>&1 ; then
echo "TSConfig references are out of date. Execute \033[0;34myarn run sync-references\033[0m to fix and try again" >&2
exit 1
fi

yarn lint-staged -v
3 changes: 3 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"*.{js,ts,json,md}": "node maintenance/fmt-staged.js"
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"editor.defaultFormatter": "dprint.dprint",
"[jsonc]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "dprint.dprint"
},
"[javascript]": {
Expand Down
26 changes: 26 additions & 0 deletions maintenance/fmt-staged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require("path");

// Dprint parallelizes formatting and is generally fast enough to run on the whole repo
// despite lint-staged telling us which files have changed.
const repoRoot = path.join(__dirname, "..");
require("child_process").spawnSync("yarn", ["fmt"], {
cwd: repoRoot,
});

// If it ever becomes slow, the approach below works:

// const fs = require("fs");
// const { formatWithDprint } = require("@zwave-js/fmt");

// const files = process.argv.slice(2);

// for (const file of files) {
// // Skip package.json and similar
// if (path.basename(file) === "package.json") continue;
// if (path.basename(file) === "package-lock.json") continue;

// console.log(`Formatting ${file}...`);
// const content = fs.readFileSync(file, "utf8");
// const formatted = formatWithDprint(repoRoot, file, content);
// fs.writeFileSync(file, formatted, "utf8");
// }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"fs-extra": "^10.1.0",
"husky": "^8.0.3",
"json5": "^2.2.3",
"lint-staged": "^14.0.1",
"reflect-metadata": "^0.1.13",
"semver": "^7.5.2",
"source-map-support": "^0.5.21",
Expand Down
Loading

0 comments on commit e8ebb4d

Please sign in to comment.