1 | What is it?
|
2 | -----------
|
3 |
|
4 | This module is a handy little tool that I wrote to help enforce code quality in node.js projects. It will run the JSHint linter over your code,
|
5 | followed by two optional steps: A custom validator specified by you in your package.json, and unit tests which are also specified in your package.json.
|
6 |
|
7 | To facilitate the usage of JSHint, as part of the install step two files will be created in the root of your project, .jshintrc and .jshintignore. These
|
8 | files are configuration for JSHint itself. They are included to provide some sane defaults, such as making sure your installed npm modules are not being
|
9 | linted, and that some node.js global variables won't trigger errors. You are free to customize these files after they're created, and they will not be
|
10 | overwritten.
|
11 |
|
12 | Why should I use it?
|
13 | --------------------
|
14 |
|
15 | No 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
|
16 | and error free. Since the hook lints all of the project's code, based on your configuration, you can be sure that at the very least standards are
|
17 | being followed.
|
18 |
|
19 | In addition to this, the validate script can be used to perform whatever manual checking you like. Whether it's making sure that a file has been
|
20 | updated, pre-compiling static assets, or whatever other need you may have. Additional steps are easy to forget, so why chance it?
|
21 |
|
22 | Unit tests are another thing that should always be verified before committing your code. Pushing code that breaks tests is an all too common occurrence.
|
23 |
|
24 | Having a tool that automates all of these steps has been priceless for us, and has very much improved the quality of our code.
|
25 |
|
26 | Package.json
|
27 | ------------
|
28 |
|
29 | As mentioned above, there are two optional steps run by the hook. A validator, and unit tests. Specifying these scripts is done in your package.json, such
|
30 | as the below example.
|
31 |
|
32 | ```javascript
|
33 | {
|
34 | "name": "your_project",
|
35 | "description": "just an example",
|
36 | "scripts": {
|
37 | "validate": "./command/to/run",
|
38 | "test": "./other/command"
|
39 | }
|
40 | }
|
41 | ```
|
42 |
|
43 | The contents of the validate and test properties are the shell command to be run to perform those functions. Having these specified in your package.json also
|
44 | lends you the ability to be able to run them manually like so
|
45 |
|
46 | ```
|
47 | npm run-script validate
|
48 | npm test
|
49 | ```
|
50 |
|
51 | These scripts can be any shell executable commands, but must exit with a status code of 0 for success and 1 or greater for failure.
|
52 |
|
53 | In addition to this, a new feature is the ability to manually override these commands or disable them entirely. To do so you add a precommit config to your
|
54 | package.json, similar to the below example.
|
55 |
|
56 | ```javascript
|
57 | {
|
58 | "name": "your_project",
|
59 | "description": "just an example",
|
60 | "scripts": {
|
61 | "validate": "./command/to/run",
|
62 | "test": "./other/command"
|
63 | },
|
64 | "config": {
|
65 | "precommit": {
|
66 | "lint": false,
|
67 | "validate": true,
|
68 | "test": "mocha"
|
69 | }
|
70 | }
|
71 | }
|
72 | ```
|
73 |
|
74 | This example would disable the linting step entirely, explicitly enables the validation step, and overrides the test step to run "mocha" instead of "./other/command"
|
75 |
|
76 | Usage
|
77 | -----
|
78 |
|
79 | npm install precommit-hook
|
80 |
|
81 |
|
82 | Everything else is automatic! The npm install script will create the hook, and place a .jshintrc and .jshintignore file in your project if they don't exist.
|
83 | To update, just install again. Only the hook itself will be overwritten. I recommend putting precommit-hook in your project's devDependencies to make sure
|
84 | that anyone who may be contributing to your project will have the hook installed.
|
85 |
|
86 | ```
|
87 | {
|
88 | "name": "your_project",
|
89 | "description": "just an example",
|
90 | "scripts": {
|
91 | "validate": "./command/to/run",
|
92 | "test": "./other/command"
|
93 | },
|
94 | "devDependencies": {
|
95 | "precommit-hook": ""
|
96 | }
|
97 | }
|
98 | ```
|
99 |
|
100 | Contact
|
101 | -------
|
102 |
|
103 | Like the project? Hate it? Just want to say hi? Find me on twitter [@quitlahok](http://twitter.com/quitlahok)
|
104 |
|
105 | License
|
106 | -------
|
107 |
|
108 | MIT
|