Skip to content

Improve pre-commit hook to run only on staged files #480

@tearingItUp786

Description

@tearingItUp786

Description

The pre-commit hook runs the others/validate.sh command on all files (not only the staged files). This leads to annoying DX where we have to stash files or revert some changes even though we might only want to add a subset of files to the commit.

Solution

The project currently has lint-staged installed and it isn't utilized by husky. After doing some digging, I think we can update it to the beow and should get us better DX. The tradeoff is that we can't run the test, lint, typecheck, and build commands concurrently anymore and they'd happen sequentially:

module.exports = {
  '*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx|vue)': [
    `npm run test --silent -- --watch=false`,
    `npm run lint --silent`,
    () => `npm run typecheck --silent`,
    () => `npm run build --silent`,
    `npm run clean --silent`, // needed so that we don't have any build artifacts kicking around when we try to commit again.
  ],
}

The above code will run the test files and lint files correctly on staged files and will run the typecheck and build commands as if they were being run from root.

Link to thread on lint-staged

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions