add husky pre-commit hook with npm#6 on windows - husky

Environment:
git version 2.25.1.windows.1
node 12.x
Npm 6.14.11
husky 7.0.4
windows 10
How to correctly setup husky & pre-commit hook? In my case I just want to add npm test script to run, so with every commit tests are run.

Although most of things are covered in docs (if not all), I will share steps which I took in order to make this work because I have put several hours effort before finally made it.
npm i husky -D
add new script to package.json - "prepare": "husky install"
execute npm run prepare -> .husky folder is created in project root directory
add new script in package.json file (read more in additional at the bottom):
"husky": "node ./node_modules/husky/lib/bin.js"
run npm run husky add ./husky/pre-commit "npm test" -> pre-commit hook is created
execute git commit
ADDITIONAL explanation to step 4:
npx husky add .husky/pre-commit "npm test" - this is command stated in docs and many stackoverflow questions/github issues or blogs, but this doesn't work by default on Windows & npm#6 (at least not for me).
Output after this command is like this, it is just showing how to use command, no pre-commit file is created inside .husky folder:
It is documented in husky docs how this can be approached differently, and that is what I tried to do in step 4.
For Windows users, if you see the help message when running npx husky
add ..., try node node_modules/.bin/husky add ... instead. This isn't
an issue with husky code and is fixed in recent versions of npm 8.
And that is all. No additional actions, nor I didn't have to unset git config core.hooksPath.

Related

Force project to use Yarn but not npm

As a team practice, I would like to force my teammate to use yarn install/ run but not npm install/ run.
Is it possible to force a package.json 's dependency be installed only via yarn install or package.json's script be run only via yarn run?
If it cannot be done, can I at least get a warning when using npm install?
Again, this is only to align the team practice, so that reduces the possibility of error/ problem produced during dev/ops. Thanks
One of the way I can think of is CI can set a rules to detect if there is new file package-lock.json file being created, make the build fail. The developer will then realized he made a mistake since his build failed.
Alternatively you can also rely on husky pre-commit hook, which essentially runs a command to check if package-lock.json existed everytime developer trying to run git commit.

How to set an environment variable to be used during "npm install"?

Our coorporate network is very closed down, so a normal method of:
npm install cypress#3.4.1
Doesnt work since it is being blocked by a proxy, we need to provide the parameter CYPRESS_INSTALL_BINARYin the following way with the help of cross-env (since we have mainly Microsoft environments here).
cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip' npm install cypress#3.4.1
This is easy to do on first install, but the problem is that everyone on the team needs to run this command. And I want it to be possible to just type npm install and they will get all requirements automatically. This is extra obvious when we want to update the cypress package, since the binary url needs to change each time.
I tried to add a preinstall script to my package.json like so:
"scripts": {
...
"preinstall": "cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip'",
...
},
But it seems like that environment variable set by cross-env is "gone" after preinstall is finished and install begins, since cypress tries and fails to download from the web. I am okay with it being temporary, but it needs to persist over the install-command. Also seen solutions with .env files but none of those have support for the install step as far as I can see.
My current solution is to run the entire cypress installation in the preinstall step, and it works but seems uneccessary to run a double install each time.
So, what I am asking for, is a way to let a developer just run the following commands on a brand new computer and be done.
git clone ...
cd ...
npm install
How can I do that?
Same situation on my side, except that I want to prevent the installation of cypress on the local machine.
Solved it by adding a .npmrc into the root of the project and adding to version control.
Contents of .npmrc:
CYPRESS_INSTALL_BINARY=0
Since the environment variable is used on install time, the solution with cross-env was not possible, since one cannot be sure that cross-env has been already installed.
Let me know whether it helped or you have already another solution.
I ended up creating a tool that takes care of supplying the correct binary, depending on environment:
https://github.com/tomasbjerre/dictator-cypress
I have it internally at my company. When we need a new version of Cypress, we release a new version of this tool internally.
I add the tool as a preinstall script:
...
"scripts": {
"preinstall": "npx dictator-cypress#0.0.28",
...
So that it runs when someone does npm install:
...
Copy linux cypress to cypress.zip
Applying: copy linux-x64/cypress.zip to .
.npmrc should have reference to cypress binary
Up to date: .npmrc should have lines
.gitignore should include the copied zip
Up to date: .gitignore should have lines
...

Which script am I missing when trying to install framework7 custom build?

npm ERR! missing script: build-core:prod
I'm receiving the above error when trying to run the custom build for framework7 - https://framework7.io/docs/custom-build.html
I have gone through each step however I am running into this problem in cmder and I'm unsure why.
The error occurs at step 7 if this info is of any value
Any help would be appreicated.
The error says it cannot find the script build-core:prod.
I could replicate this error (missing build-core:prod), by NOT INSTALLING THE DEPENDENCIES after cloning from github.
So the steps are:
1. Clone the Framework7 Github repository
git clone https://github.com/framework7io/framework7 my-app
this will download the repository in a folder called my-app
2. Install Node.js
that's another topic, hope you have that installed
3. Install Gulp
npm install --global gulp
if you don't have that
4. Install dependencies
cd my-app
npm install
be sure that you are in the root of my-app (the directory you just created for the cloned repository)
this installs all the dependencies that Framework7 Custom Build needs
5. Duplicate my-app/scripts/build-config.js
and rename the duplicate to my-app/scripts/my-config.js
6. Open my-config.js and edit it as you need
7. Go to my-app folder (the root)
and run
npm run build-core:prod -- --config scripts/my-config.js --output path/to/output/folder
path/to/output/folder is a path to the folder where you want the customised files to be, it can be (for example) f7-my-custom-build
8. The custom build is ready,
hopefully you can work with it now. :)

How to install git hooks on "npm install"?

I'd like to install a pre-commit git hook (that lints the code) when someone installs my-package.
I tried to add a postinstall script:
"scripts": {
"postinstall": "./scripts/install-git-hooks"
}
This works great. When someone runs npm install, they get the pre-commit hook installed.
However, if another-package depends on my-package, running npm install for another-package runs the postinstall script as well, which is undesired.
What's the cleanest way to avoid this undesired affect?
You can use the ghooks npm module and add it as a dev-dependency. You can configure what to run before commit in your package.json like so:
[...]
"config": {
"ghooks": {
"pre-commit": "npm test"
}
}
[...]
Hacky, but might work for you.
The trick is to identify (within the script) if it is a sub-dependency or a root dependency for the NPM installation. Simply check if ../../package.json exists. If so, it's a sub dependency and you should skip installing the hooks.
It should be noted that you are breaking any consistent installation rules, which is exactly against the spirit of the installation scripts. This is to install client-side hooks which cannot be trusted by any means, if you need the linting to be enforced, this should be done server side, where it can just reject code that doesn't comply.
Potentially this issue would be better solved like you mentioned, by having it as a custom install script, and just dealing with the additional communication overhead.

Skip "Installing dependencies with npm" step when pushing a Node.js app to Heroku

Running git push heroku master always triggers a step that prompts:
Installing dependencies with npm
This step loads and reinstalls all of the dependencies again even it already exists. This is very time consuming and I want to skip this step sometimes when I deploy that I know the dependencies are the same.
Is there any command or options that do this?
Its been a long time since you asked this question, now the Heroku buildpack caches node_modules, so install times will be much faster.
If however you still want to block npm install, here is one solution.
As of when I write this, the default Heroku build pack does not allow skipping npm install entirely. You can see in the dependencies.sh file, this line will always run:
npm install --unsafe-perm --userconfig $build_dir/.npmrc 2>&1
However, if you create a file called .npmrc in your project folder with the following contents:
dry-run
This will cause npm install to not modify your existing node_modules directory.
Note that this change will also apply to the npm prune command that Heroku runs, but WILL NOT apply to the npm rebuild command (which is probably fine).
try to remove
node_modules
for example
from you .gitignore
Simplest ways I've found are
heroku apps:rename newTemporaryName
then
heroku apps:rename originalName
or change the NODE_ENV and get it back to previous again.
heroku config:set NODE_ENV=dev
then
heroku config:set NODE_ENV=production
There are probably other, similar hacks but these should be sufficient.

Resources