Skip to content

Fix: Migrate husky hooks to v9 format #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2025

Conversation

MaoShizhong
Copy link
Contributor

@MaoShizhong MaoShizhong commented Mar 21, 2025

Because

The current installed Husky version is v9 but package.json contains the old v4 way of setting hooks. This doesn't work in v9 so lint-staged was never being run pre-commit. While we already have GH workflows for the same actions, it helps to have this at the local level too and reduce "appease linter/formatter" commits. Belts and braces.

This PR

  • Migrates to husky v9 way of setting a pre-commit hook
  • Adds prepare npm script to ensure npm install also initialises husky (its setup files include a .gitignore that ignores themselves) https://typicode.github.io/husky/how-to.html#manual-setup
  • Amends lint-staged tasks for different file types to also include Prettier

Issue

N/A

Additional Information

Example trying to commit a single change to index.js that includes a let foo; somewhere.

Current behaviour:

image

With PR fix:

image

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project Contributing Guide
  • The title of this PR follows the location of change: brief description of change format, e.g. Callbacks command: Update verbiage
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If this PR adds new features or functionality, I have added new tests
  • If applicable, I have ensured all tests related to any command files included in this PR pass, and/or all snapshots are up to date

Runs automatically on npm install

Creates .husky/_ directory (which is gitignored, hence needs this script to auto init on install)
package.json field not compatible with Husky v9 (was a v4 feature)
We want to run the whole test script if a file is change, not only on
changed test files. Else we could introduce a failing change but if the
right test file isn't staged, the test won't be run.
Copy link
Contributor

@JustWaveThings JustWaveThings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this will improve DX by getting those errors pre-commit. Thanks for doing this @MaoShizhong 🚀

@JustWaveThings JustWaveThings merged commit 066de91 into TheOdinProject:main Mar 28, 2025
3 checks passed
@MaoShizhong MaoShizhong deleted the fix-husky branch March 28, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants