├── .gitignore ├── README.md ├── bin └── install ├── jshintignore ├── jshintrc └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What is it? 2 | ----------- 3 | 4 | ### Note: This module is simply a wrapper around [git-validate](https://github.com/nlf/git-validate). If you wish to use a linter other than jshint, I highly recommend you look there instead as this module will always bundle jshint for historical reasons. 5 | 6 | [![Join the chat at https://gitter.im/nlf/precommit-hook](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/nlf/precommit-hook?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 7 | 8 | This 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. 9 | 10 | ### WARNING: If you already have a `.git/hooks/pre-commit` file, this package will overwrite it. 11 | 12 | Breaking changes 13 | ---------------- 14 | 15 | Version 2.0.0 of this module (and the current master branch in this repository) has been updated to use [git-validate](https://github.com/nlf/git-validate), as such the configuration key in package.json will be changed to "pre-commit" with a dash, instead of the old "precommit". All other functionality should work the same. 16 | 17 | Why should I use it? 18 | -------------------- 19 | 20 | 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 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. 21 | 22 | Having a tool that automates this process has been priceless for us, and has very much improved the quality of our code. 23 | 24 | Usage 25 | ----- 26 | 27 | When 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. 28 | 29 | A `.validate.json` file will also be created to provide *defaults* to [git-validate](https://github.com/nlf/git-validate) in order to run your scripts. This configuration is only used if you have not specified your own configuration in `package.json`. 30 | 31 | If you need to make changes to the hook's configuration, you should *always* make them in your `package.json` as the `.validate.json` file *will* be overwritten every time the install script is run. 32 | 33 | If 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. 34 | 35 | Package.json 36 | ------------ 37 | 38 | ```javascript 39 | { 40 | "name": "your_project", 41 | "description": "just an example", 42 | "scripts": { 43 | "validate": "./command/to/run", 44 | "test": "./other/command" 45 | } 46 | } 47 | ``` 48 | 49 | The contents of the validate and test properties are the shell commands to perform those functions. Having these specified in your package.json also 50 | lends you the ability to be able to run them manually like so: 51 | 52 | ``` 53 | npm run-script validate 54 | npm test 55 | ``` 56 | 57 | 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. 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 ."`. 58 | 59 | You may configure what scripts will be run by the hook, by passing an array of script names to the `"pre-commit"` key in your package.json. 60 | 61 | ```javascript 62 | { 63 | "name": "your_project", 64 | "description": "just an example", 65 | "scripts": { 66 | "lint": "jshint --with --different-options", 67 | "validate": "./command/to/run", 68 | "test": "./other/command" 69 | }, 70 | "pre-commit": ["lint", "test"] 71 | } 72 | ``` 73 | 74 | This example would run only the `lint` and `test` scripts, in that order. 75 | 76 | Usage 77 | ----- 78 | 79 | npm install precommit-hook 80 | 81 | 82 | Everything else is automatic! 83 | 84 | I 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. 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": "latest" 96 | } 97 | } 98 | ``` 99 | 100 | JSHint Defaults 101 | --------------- 102 | 103 | The default `.jshintrc` looks like the following: 104 | 105 | ```javascript 106 | { 107 | "node": true, // node environment 108 | 109 | "curly": true, // enforce using curly braces around blocks 110 | "latedef": true, // enforce defining a variable before using it 111 | "quotmark": true, // allows either " or ' but you must be consistent 112 | "undef": true, // error on use of undefined variables 113 | "unused": true, // error on variables that are defined but never used 114 | "trailing": true // error on trailing whitespace 115 | } 116 | ``` 117 | 118 | And the default `.jshintignore` contains only one line 119 | 120 | ``` 121 | node_modules 122 | ``` 123 | 124 | Contact 125 | ------- 126 | 127 | Like the project? Hate it? Just want to say hi? Find me on twitter [@quitlahok](http://twitter.com/quitlahok) 128 | 129 | License 130 | ------- 131 | 132 | MIT 133 | -------------------------------------------------------------------------------- /bin/install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var Validate = require('git-validate'); 4 | 5 | Validate.copy('../jshintrc', '.jshintrc'); 6 | Validate.copy('../jshintignore', '.jshintignore'); 7 | Validate.installScript('lint', 'jshint .'); 8 | Validate.installScript('validate', 'npm ls'); 9 | Validate.configureHook('pre-commit', ['lint', 'validate', 'test']); 10 | -------------------------------------------------------------------------------- /jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | 4 | "curly": true, 5 | "latedef": true, 6 | "quotmark": true, 7 | "undef": true, 8 | "unused": true, 9 | "trailing": true 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Nathan LaFreniere (https://github.com/nlf)", 3 | "name": "precommit-hook", 4 | "description": "A useful pre-commit hook for git based projects that lints and runs npm test", 5 | "version": "3.0.0", 6 | "license": "MIT", 7 | "homepage": "https://github.com/nlf/precommit-hook", 8 | "repository": { 9 | "url": "https://github.com/nlf/precommit-hook.git" 10 | }, 11 | "dependencies": { 12 | "git-validate": "^2.0.0", 13 | "jshint": "*" 14 | }, 15 | "scripts": { 16 | "install": "node bin/install" 17 | } 18 | } 19 | --------------------------------------------------------------------------------