Skip to content

Commit eebfc6d

Browse files
Add pre-commit hook for Prettier (#2856)
* Add pre-commit hook for Prettier * fix gh actions * update package.json
1 parent d3609a6 commit eebfc6d

6 files changed

Lines changed: 603 additions & 4 deletions

File tree

.cursor/rules/contributor-workflow.mdc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ Pull requests run **`npm run format:check`** (Prettier against `.prettierrc` and
6969
**`npm run format`** locally to fix style before pushing. Generated Snaps API pages under
7070
`snaps/reference/snaps-api/` are excluded from formatting in git (see `.prettierignore`).
7171

72+
After **`npm install`**, [Husky](https://typicode.github.io/husky/) installs a Git **`pre-commit`** hook
73+
that runs [**`lint-staged`**](https://github.com/lint-staged/lint-staged) using the rules in
74+
`package.json`. Staged files matching those patterns are auto-formatted with Prettier (and other
75+
commands in `lint-staged`, such as ESLint on `src/`). Restage if the hook edits files. Hooks are
76+
local only and are not required for CI to pass.
77+
7278
Pull requests are checked by a Vale-based linter
7379
([docs-spelling-check](https://github.com/Consensys/github-actions/tree/main/docs-spelling-check))
7480
that enforces Microsoft style, Consensys terminology, and spelling. Fix any linter warnings before

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ jobs:
5757
cache: npm
5858
- name: Install dependencies
5959
run: npm ci
60+
env:
61+
# Install devDependencies (Prettier, Husky). NODE_ENV=production skips them and breaks prepare.
62+
NODE_ENV: development
63+
HUSKY: 0
6064
- name: Check formatting
6165
run: npm run format:check
6266

@@ -84,6 +88,9 @@ jobs:
8488
node-version-file: '.nvmrc'
8589
- name: Install dependencies
8690
run: npm ci
91+
env:
92+
NODE_ENV: development
93+
HUSKY: 0
8794
- name: Generate Snaps API reference
8895
run: npm run docusaurus snaps:generate
8996
- name: LinkCheck

.husky/pre-commit

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env sh
2+
# Git GUIs (Fork, etc.) often omit Node from PATH. Resolve node, then run lint-staged without npx.
3+
cd "$(dirname "$0")/.." || exit 1
4+
5+
export PATH="$HOME/.volta/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
6+
7+
if [ -s "$HOME/.nvm/nvm.sh" ]; then
8+
export NVM_DIR="$HOME/.nvm"
9+
# shellcheck disable=SC1090
10+
. "$HOME/.nvm/nvm.sh"
11+
nvm use default >/dev/null 2>&1 || nvm use node >/dev/null 2>&1 || true
12+
fi
13+
14+
if ! command -v node >/dev/null 2>&1; then
15+
echo "husky: Node.js not found. Install Node.js or open your terminal and run: which node" >&2
16+
echo "husky: If you use nvm, ensure default is set (nvm alias default <version>)." >&2
17+
exit 1
18+
fi
19+
20+
# Call the CLI with explicit node so we do not rely on the stub shebang (#!/usr/bin/env node).
21+
exec node ./node_modules/lint-staged/bin/lint-staged.js

0 commit comments

Comments
 (0)