UNPKG

4.75 kBMarkdownView Raw
1What is it?
2-----------
3
4This module is a handy little tool that I wrote to help enforce code quality in Node.js projects. It allows you to run any scripts defined in your package.json before a commit is made.
5
6### WARNING: If you already have a `.git/hooks/pre-commit` file, this package will overwrite it.
7
8Why should I use it?
9--------------------
10
11No one likes a messy code base. When working on a team, it becomes more and more difficult to make sure that your project's code stays consistent and error free. Since the hook can lint all of the project's code, based on your configuration, you can be sure that at the very least standards are being followed. It can also run build steps, unit tests, or any other script you like.
12
13Having a tool that automates this process has been priceless for us, and has very much improved the quality of our code.
14
15Usage
16-----
17
18When you install this project, by default it will create sane `.jshintignore` and `.jshintrc` files for you if they do not already exist. That means it's safe to upgrade the hook after customizing these files, as they will never be overwritten. If you have your jshint configuration in your package.json, then the `.jshintrc` file will not be created ever.
19
20You can prevent this behavior by modifying your package.json file. If a `lint` script is specified and does not start with the string `"jshint"` OR if you configure the hook to run specific scripts, and that list of scripts does not include `"lint"`, the jshint configuration files will not be created.
21
22For example:
23
24```javascript
25{
26 "name": "your_project",
27 "description": "just an example",
28 "scripts": {
29 "lint": "some_other_linter ."
30 }
31}
32```
33
34OR
35
36```javascript
37{
38 "name": "your_project",
39 "description": "just an example",
40 "precommit": ["test"]
41}
42```
43
44If you do not configure the hook with an array of scripts to run, it will default to `["lint", "validate", "test"]` to maintain backwards compatibility with the old version of this hook. In addition, if a `lint` script is not specified, it will default to `"jshint ."`. If a `lint` script is configured, it will not be overridden. If an array of scripts is configured, it will be used and there will be no default `lint` script.
45
46Package.json
47------------
48
49```javascript
50{
51 "name": "your_project",
52 "description": "just an example",
53 "scripts": {
54 "validate": "./command/to/run",
55 "test": "./other/command"
56 }
57}
58```
59
60The contents of the validate and test properties are the shell commands to perform those functions. Having these specified in your package.json also
61lends you the ability to be able to run them manually like so:
62
63```
64npm run-script validate
65npm test
66```
67
68These scripts can be any shell executable commands, but must exit with a status code of 0 for success and 1 or greater for failure. The `PATH` environment variable used when executing these scripts will be similar to how npm configures it. That means if you `npm install jshint` locally to your project, you can put simply `"jshint ."` for your script rather than `"./node_modules/.bin/jshint ."`.
69
70You may configure what scripts will be run by the hook, by passing an array of script names to the `"precommit"` key in your package.json.
71
72```javascript
73{
74 "name": "your_project",
75 "description": "just an example",
76 "scripts": {
77 "lint": "jshint --with --different-options",
78 "validate": "./command/to/run",
79 "test": "./other/command"
80 },
81 "precommit": ["lint", "test"]
82}
83```
84
85This example would run only the `lint` and `test` scripts, in that order.
86
87Usage
88-----
89
90 npm install precommit-hook
91
92
93Everything else is automatic!
94
95I recommend putting precommit-hook in your project's devDependencies to make sure that anyone who may be contributing to your project will have the hook installed.
96
97```
98{
99 "name": "your_project",
100 "description": "just an example",
101 "scripts": {
102 "validate": "./command/to/run",
103 "test": "./other/command"
104 },
105 "devDependencies": {
106 "precommit-hook": "latest"
107 }
108}
109```
110
111JSHint Defaults
112---------------
113
114The default `.jshintrc` looks like the following:
115
116```javascript
117{
118 "node": true, // node environment
119
120 "curly": true, // enforce using curly braces around blocks
121 "latedef": true, // enforce defining a variable before using it
122 "quotmark": true, // allows either " or ' but you must be consistent
123 "undef": true, // error on use of undefined variables
124 "unused": true, // error on variables that are defined but never used
125 "trailing": true // error on trailing whitespace
126}
127```
128
129And the default `.jshintignore` contains only one line
130
131```
132node_modules
133```
134
135Contact
136-------
137
138Like the project? Hate it? Just want to say hi? Find me on twitter [@quitlahok](http://twitter.com/quitlahok)
139
140License
141-------
142
143MIT