This provides a simple sample NodeJs application that makes use of the Husky library that Node.Js apps can use to setup Git to setup a hook to run JSHint library that validates JavaScript code.
-
cd to a folder to hold the folder to hold the folder created when git clone is run:
git_hook_node_jshint
-
Open a Terminal shell window and cd to the folder, then run:
npm install
This creates an npm_modules folder and populates it with dependencies from the internet.
husky@0.7.0 install /Users/mac/gits/wilsonmar/devops/node-sample1/node_modules/husky node ./bin/install.js husky setting up hooks in .git/hooks/ done Prehooks@1.0.0 /Users/mac/gits/wilsonmar/devops/node-sample1 ├── husky@0.7.0 └─┬ jshint@2.9.4 ├─┬ cli@1.0.1 │ └─┬ glob@7.1.1 │ ├── fs.realpath@1.0.0 │ ├─┬ inflight@1.0.6 │ │ └── wrappy@1.0.2 │ ├── inherits@2.0.3 │ ├── once@1.4.0 │ └── path-is-absolute@1.0.1 ├─┬ console-browserify@1.1.0 │ └── date-now@0.1.4 ├── exit@0.1.2 ├─┬ htmlparser2@3.8.3 │ ├── domelementtype@1.3.0 │ ├── domhandler@2.3.0 │ ├─┬ domutils@1.5.1 │ │ └─┬ dom-serializer@0.1.0 │ │ ├── domelementtype@1.1.3 │ │ └── entities@1.1.1 │ ├── entities@1.0.0 │ └─┬ readable-stream@1.1.14 │ ├── core-util-is@1.0.2 │ ├── isarray@0.0.1 │ └── string_decoder@0.10.31 ├── lodash@3.7.0 ├─┬ minimatch@3.0.3 │ └─┬ brace-expansion@1.1.6 │ ├── balanced-match@0.4.2 │ └── concat-map@0.0.1 ├── shelljs@0.3.0 └── strip-json-comments@1.0.4 npm WARN Prehooks@1.0.0 No repository field.
The .gitignore file defined for the project specifies that the folder is not uploaded back to GitHub. This is also the case for the npm-debug.log created in case there is an error.
Invoke the Node.Js app
node index.js
The expected response is a new Terminal line.
The package.json file defines what Node does before Git actions.
If this is in the package.json file:
"scripts": {
"precommit": "jshint index.js"
},
jshint is invoked on index.js on git commit
based on settings in file .jshintrc.
The file in the repo, these errors to appear based on the index.js in the repo:
index.js: line 3, col 15, Expected '===' and instead saw '=='. index.js: line 4, col 40, Missing semicolon. index.js: line 8, col 9, Expected '{' and instead saw 'doSomething'. index.js: line 8, col 22, Missing semicolon. index.js: line 11, col 35, Missing semicolon. index.js: line 14, col 2, Missing semicolon. index.js: line 3, col 9, 'num' is not defined. index.js: line 7, col 12, 'num' is not defined. 8 errors husky - pre-commit hook failed (add --no-verify to bypass)
Options specified are according to http://jshint.com/docs/options
To commit without performing verification, use this command:
git commit -m"update index.js" --no-verify
PROTIP: After you insert lines, save the file and get new line numbers in the error messages by running again.
If this is in the package.json file:
"scripts": {
"prepush": "jshint index.js"
},
jshint is invoked on index.js on git push
.
To push without performing verification, change the package.json file to:
git push --no-verify
A video of this is at
https://www.youtube.com/watch?v=sTnatBgmYsE
and blog
http://www.penta-code.com/prevent-bad-git-commits-and-pushes-with-husky/