Search code examples
sveltepre-commit-hookpre-commitpnpmpre-commit.com

Pnpm for pre-commit custom hook


I work on a python/svelte full-stack app where we use pre-commit. I've installed stylelint to frontend web dir and configured a custom hook for it :

- id: stylelint
        name: stylelint
        additional_dependencies:
          - stylelint
        entry: pnpm run --dir web lint:scss
        files: ^.*\.(css|scss|svelte)$
        language: node
        pass_filenames: false

And the problem is: our web folder is managed by pnpm, but language: node will install dependencies with npm, and hook not gonna work.

The question is: how could I tell to pre-commit to use pnpm overall and install the stylelint with pnpm add -D stylelint?


Solution

  • pre-commit works best when it manages the installed tools -- without that your contributors will have to set up every tool themselves and make sure that versions are synchronized and correct (which is one of the primary goals of the tool)

    pre-commit also works best when it manages the files that are sent to tools -- that way it can control whether the correct files are sent or not (especially during the various workflow stages and when conflicts arise)

    what you're doing should already work -- however it's doing extra work and is in the unsupported pathway. the extra work here is language: node -- where it is setting up a node environment and then ~essentially discarding it because you're executing pnpm

    you can use repo: local hooks with language: system which will just run the tools that are on your $PATH. note that both of these suffer from the issues above and are provided as an unsupported escape hatch from the right way to do things. adjusting your config you'd remove additional_dependencies and adjust language accordingly


    disclaimer: I wrote pre-commit