toplenboren / simple-git-hooks

A simple git hooks manager for small projects
MIT License
1.32k stars 43 forks source link

Unable to get simple-git-hooks working with Prettier #54

Closed tsongas closed 3 years ago

tsongas commented 3 years ago

Trying to replace Husky with simple-git-hooks in a create-react-app project as follows in package.json:

  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
      "prettier --write"
    ]
  },
  "simple-git-hooks": {
    "pre-commit": "npx lint-staged"
  },

I am able to commit a properly formatted file i.e. no changes made by Prettier when the hook runs, however when I leave out a semi-colon to test Prettier formatting, I get a popup in VSCode saying "Git: [STARTED] Preparing..." with Show Command Output, Cancel, and Open Git Log buttons, then when I click Show Command Output I see the following:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for src/**/*.{js,jsx,ts,tsx,json,css,scss,md}
[STARTED] prettier --write
[SUCCESS] prettier --write
[SUCCESS] Running tasks for src/**/*.{js,jsx,ts,tsx,json,css,scss,md}
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[FAILED] Prevented an empty git commit!
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...

  ⚠ lint-staged prevented an empty git commit.
  Use the --allow-empty option to continue, or check your task configuration

npm ERR! code 1
npm ERR! path /Users/chris/Documents/Jobs/AFSCME/repos-legacy/afscme-cards
npm ERR! command failed
npm ERR! command sh -c lint-staged

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/chris/.npm/_logs/2021-04-29T06_37_56_082Z-debug.log

Any idea how to fix this so Prettier runs and the changes get added to the commit?

tsongas commented 3 years ago

One thing to note, lint-staged originally looked like this:

  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
      "prettier --write",
      "git add"
    ]
  },

however I got an error saying the git add happens automatically so I should remove it from my script, which I did.

tsongas commented 3 years ago

This actually appears to be working as intended, if there are no actual changes to commit after Prettier runs, I get an error. If there are changes, they get committed.