0

I want to execute a node.js script in a pre-commit hook.

  • Expected result: node.js script is executed
  • Actual result: Nothing happens, no error, no logs, nothing
#!/usr/bin/env bash

echo "Run pre-commit hook"
node [project]/scripts/generate-social-media-preview/index.js
git add .
echo "Finished pre-commit hook"
exit 0

Of course, I also tried to run the node.js directly in the pre-commit, by changing the first line to: #!/usr/bin/env node. Same result.

The script executes playwright, manipulates an HTML template, takes a screenshot, and saves it. All of this lives in a nuxt.js project, so the script and the rest of the project are using one shared node-modules folder and package.json.

If I just run [project]/scripts/generate-social-media-preview/index.js it does exactly what it should do. I made the file executable.

I normally commit via Webstorm, but I also did it with the terminal and I never saw an output or that the images got generated accordingly.

Also, husky and pre-commit were on my list, but it behaved as with the native pre-commit hook.

I would be happy to solve this "within the code" to not have a plugin or local script which triggers this, also running this during npm run generate isn't working as my website is hosted at Netlify and of course, there isn't chromium available for playwright.

The whole code can be seen here (with all my test commits...): https://github.com/LukaHarambasic/harambasic.de/pull/46

kissu
  • 40,416
  • 14
  • 65
  • 133
luhara
  • 25
  • 7

1 Answers1

0

So, there were two problems:

  1. WebStorm doesn't handle pre-commit hooks very well and for me, somehow isn't working with husky. From now on, I'll do commands via the command line. If you want to know more, read this and this issue. -> let me know if you find a solution
  2. Committing files in a pre-commit hook isn't very reliable - check this question to see the solution with a pre-commit and post-commit hook.
luhara
  • 25
  • 7