Git pre-commit hook isn't executed - node.js

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

So, there were two problems:
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
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.

Related

Adding a command line script to the user environment whilst installing an application using electron-builder

I'm currently working on a project with Electron 9.0.4 and Electron-Builder 22.8.0 and am faced with a problem that doesn't seem too difficult but there isn't a valid solution online! (At least I couldn't find it)
I have my main program that does all of the UI tasks, and a command line script that does some backend. The reason I have this command line script is so that I can run certain parts of the application without opening the window itself. Everything works fine on my computer. After running npm link, my CL script is added to my environment variables and I can just run it from the console. However, when I try to build with electron-builder, the problem occurs.
If I use my Setup.exe on another computer, the command line script just won't be added to the environment variables and I couldn't find instructions on how to do this in the electron, nodejs, or electron-builder documentation. What I found was a suggestion on another question to add npm -g install as a post-install script, but that had no effect either.
Someone else suggested adding npm link as a post-installation script, but firstly if I am not mistaken this function is not intended for production and secondly it created an infinite loop as npm link triggered the post-installation script over and over again.
Thats how the script is added to the project
"bin": {
"command-name": "/cl.js"
}
Any help is appreciated!
Since I couldn't find a direct solution to my problem and didn't want to look any further for a solution while being able to take a different approach.
I decided to take a step back and look for another method to solve my problem I came to the conclusion that I didn't really need to add a script to the command line. My solution was to look for a certain argument when starting the regular application.
if (process.argv.includes("cli")) { /* Do commandline stuff */ }
When the custom argument is found, I simply run the script that should've been run from the command line. Using this approach, you can create a shortcut to my executable that contains the custom argument and then instead of the application it runs the command line script.

How to create git hooks in Windows with Node.js?

I have been following this guide on how to use Node.js to script git hooks. However, the guide is using a Unix based system whilst I am running on a Windows machine.
I have also found this guide on running git hooks on a Windows machine, but it is not using Node.js
I am running a pre-install script in my package.json file to set a custom git hooks location.
I am using VSCode as my editor and would like the git hooks to run when I use the UI for commits etc. However I am using command line initially to try and get the hooks to fire.
package.json excerpt
"scripts": {
"preinstall": "git config core.hooksPath ./git.hooks"
},
In my git.hooks folder I have a pre-commit.js file.
I have updated the first line to reflect the fact I'd like to execute the script running Node.js
pre-commit.js
#!C:/Program\ Files/nodejs/node.exe
console.log('Hello world!');
process.exit(1);
If I run this script directly I get a Microsoft JScript compilation error - Invalid character on line 1 char 1.
If I do a commit, I get no errors but nothing happens.
Can anyone guide me through the process of creating a Node.js hook in Windows. I would rather create one myself than use a package.
Name the hook exactly pre-commit, without .js.
Change the first line to #!/usr/bin/env node. But make sure that C:/Program\ Files/nodejs/node.exe has been added to the environment variable PATH.
Place it in <repo>/.git/hooks.
Make it executable. In git-bash, run chmod a+x <repo>/.git/hooks/pre-commit.
Now it should work as expected.

node.js rerun script on specific event

I'm not looking for a file watcher like nodemon
I have the following scenario:
I'm running a script script.js in nodeJS, In some cases I have to switch to a different git branch (I know how to do this part).
After the git branch is switched, I would like to exit the current run and reload script.js again.
How do I cause nodeJS to reload and run the same script again?
Do I need to start another process and exit the current one?

how to run npm/grunt command from jenkins

I'm new to this area and was trying to run the following commands from jenkins:
npm install
grunt quickStart
So far I've a jenkins running on a window machine as a window service and I've also installed NodeJs plugin for jenkins.
However, I'm stuck and quit confused following instructions here, its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place.
Here is the bit that's asking me to do:
I cannot see this setting for the jenkins job I create. Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine.
Note : I've already checked out a project using git in jenkins!!!
Thanks
"its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place."
I don't think you need that I have pointed jenkins to the node installation folder an nothing more.., for this you go to Manage Jenkins->Configure System->NodeJS->NodeJS installations.. type in any name you like and point to where the node home folder is.
...cannot see this setting for the jenkins job I create...
Once you have configure that in your jenkins configuration you should have that configuration available like so:
...Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine
I'm sorry don't get what commands are you referring to...
so summarizing :
you have to tell jenkins where you have your node installation
use that configuration in your jobs
hope this helps...
The way I made it, was trough execute shell, as the build tool for nodejs is npm, I simply wrote a shell script that instructs jenkins to run npm install in the workspace directory inside jenkins where it clones the git repository and then to zip and move the package if successful to another folder.

Having trouble with post-commit hook

I am following this tutorial that is like the hello world for post-commit
I am using Ubuntu 10.04. I installed svnnotify and ran $ which svnnotify which output:
/usr/bin/svnnotify
so I changed the path in the turorial from /usr/local/bin/svnnotify to /usr/bin/svnnotify
I also tried changing the line: #!/bin/sh to #!/bin/bash since bash is the login shell in ubuntu 10.04.
I tried to run it the way the tutorial originally had it, with my changes, and combinations of the two.
Every time the commit is successful but I get
Warning: post-commit hook failed (exit code 1) with no output.
The original way had output not found
I am very new to linux and shell scripting and have exhausted everything I can think of. What am I doing wrong?
Get the script working and tested before trying to run as a commit hook. I expect that your problem is something to do with the script, maybe not being marked as executable, environment wrong, etc.. i.e. if you can't get it to run successfully from the command line, this is more of a unix/shell question and doesn't really have anything to do with SVN (yet).
I'm not sure whether this applies in the Linux world, but in the Windows versions of SVN I've used, the code which runs the hook scripts only captures STDERR. If your hook script is only writing messages to STDOUT, that would likely explain the "No Output" warning.

Resources