How to create git hooks in Windows with Node.js? - 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.

Related

Git pre-commit hook isn't executed

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.

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.

npm command is not found when ssh with bitbucket pipelines on shared hosting

I've installed nodejs as described here.
Everything works fine when I ssh to the server myself. But I've created a script that deploys my application and call it via bitbucket pipelines. Everything else works fine (composer install, php artisan migrate etc.), except npm install. The error message is
./deploy: line 26: npm: command not found
In bitbucket-pipelines.yml I call my script like this:
- step:
script:
- ssh user#ip_address ./deploy
When I call the script by myself everything works. The problem is only with bitbucket pipelines. I have no clue why this happens :(.
Running which composer revealed that at least composer command is not getting picked up from your assumed location i.e., ~/composer dir. Instead, it was found in /opt/cpanel/composer/bin/composer.
which npm returned the following:
no npm in (/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin:/opt/bin:/opt/cpanel/composer/bin:/home/handmast/.local/bin:/home/handmast/bin)
Now since you are able to manually run the command npm, you just need to figure about the path from where npm is running and ensure that the path is explicitly added to the user's ~/.bashrc file and things should work fine. You need to do this because as per your observation, ssh is not able to find it.
export PATH=$PATH:/path/to/npm/binary
Note: To avoid any confusion, just remember that while adding the path to your binary, you just have to add the path to the directory where npm resides. Don't add npm at the end. For example, following is incorrect way:
export PATH=$PATH:/home/handmast/bin/npm
Correct way is this:
export PATH=$PATH:/home/handmast/bin

How to tersely run a globally installed Node.js app

I was recently using mongo-express which is written in Node.js. I've installed it globally following instructions on their GitHub page. It says in order to run it:
cd YOUR_PATH/node_modules/mongo-express/ && node app.js
I was wondering if there's any workarounds to avoid changing directory to the app's folder first (using cd).
By the way, in npm's doc, it says
When in global mode, executables are linked into {prefix}/bin on Unix.
In my case (using mac), executable app.js is linked into /usr/local/bin. But if I just type app.js in the command line trying to run it, it'd say -bash: /usr/local/bin/app.js: Permission denied. Even if this works, wouldn't app.js be a too generic name and become conflicted with other apps later?
Thanks for helping.

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.

Resources