Skip to content
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

Add Husky pre-commit configuration, #318. #320

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

tormi
Copy link
Member

@tormi tormi commented Nov 1, 2022

How to test:

  1. Get this branch
    git clone git@github.com:wunderio/drupal-project.git
    cd drupal-project
    git checkout feature/husky
  2. Start Lando
    lando start (or lando rebuild if you've working on existing Lando project)
  3. Create example js file
    echo 'var foo="bar"' > web/modules/custom/phpunit_example/example.js
  4. Add the file to GIT
    git add web/modules/custom/phpunit_example/example.js
  5. Try to commit
    git commit

@tormi tormi marked this pull request as draft November 1, 2022 08:01
@hkirsman
Copy link
Contributor

hkirsman commented Nov 1, 2022

It seems this overwrites Code Quality hooks so rest of the scanners don't execute anymore (phpcs, psalm etc).

Also this scans all the files, Code Quality scans only the committed files.

@tormi
Copy link
Member Author

tormi commented Nov 1, 2022

Also this scans all the files, Code Quality scans only the committed files.

Added lint-staged setup to overcome this. However, since we're using lando npm run pre-commit within Husky, it also requires git config --global user.email "you@example.com" & git config --global user.name "Your Name" inside the node container ;)

➜  dp git:(feature/husky) ✗ git commit                                            

> wunderio-drupal-project@1.0.0 pre-commit
> lint-staged

[STARTED] Preparing lint-staged...
[FAILED] 
[FAILED] *** Please tell me who you are.
[FAILED] 
[FAILED] Run
[FAILED] 
[FAILED]   git config --global user.email "you@example.com"
[FAILED]   git config --global user.name "Your Name"
[FAILED] 
[FAILED] to set your account's default identity.
[FAILED] Omit --global to set the identity only in this repository.
[FAILED] 
[FAILED] fatal: unable to auto-detect email address (got 'node@588bb4551201.(none)')
[FAILED] Cannot save the current index state
[STARTED] Running tasks for staged files...
[SKIPPED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[SKIPPED] 
[SKIPPED]   ✖ lint-staged failed due to a git error.
[STARTED] Cleaning up temporary files...
[SKIPPED] 
[SKIPPED]   ✖ lint-staged failed due to a git error.

  ✖ lint-staged failed due to a git error.
  Any lost modifications can be restored from a git stash:

    > git stash list
    stash@{0}: automatic lint-staged backup
    > git stash apply --index stash@{0}

husky - pre-commit hook exited with code 1 (error)

Test was running successfully only on staged file after I've defined git config flags noted above:

➜  dp git:(feature/husky) ✗ git commit       

> wunderio-drupal-project@1.0.0 pre-commit
> lint-staged

[STARTED] Preparing lint-staged...
[SUCCESS] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[STARTED] package.json — 1 file
[STARTED] web/**/*.{js,jsx,ts,tsx,json,css,scss,md} — 1 file
[STARTED] npx eslint --config=web/core/.eslintrc.passing.json
[FAILED] npx eslint --config=web/core/.eslintrc.passing.json [FAILED]
[FAILED] npx eslint --config=web/core/.eslintrc.passing.json [FAILED]
[FAILED] npx eslint --config=web/core/.eslintrc.passing.json [FAILED]
[STARTED] Applying modifications from tasks...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up temporary files...
[SUCCESS] Cleaning up temporary files...

✖ npx eslint --config=web/core/.eslintrc.passing.json:

/app/web/modules/custom/phpunit_example/example.js
  1:1  error    Unexpected var, use let or const instead  no-var
  1:5  warning  'foo' is assigned a value but never used  no-unused-vars
  1:8  error    Replace `="bar"` with `·=·'bar';`         prettier/prettier

✖ 3 problems (2 errors, 1 warning)
  2 errors and 0 warnings potentially fixable with the `--fix` option.

husky - pre-commit hook exited with code 1 (error)

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.

None yet

2 participants