• Stars
    star
    479
  • Rank 91,752 (Top 2 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An npm installable git pre-commit hook used to lint and test your code

What is it?

Note: This module is simply a wrapper around 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.

Join the chat at https://gitter.im/nlf/precommit-hook

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.

WARNING: If you already have a .git/hooks/pre-commit file, this package will overwrite it.

Breaking changes

Version 2.0.0 of this module (and the current master branch in this repository) has been updated to use 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.

Why should I use it?

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.

Having a tool that automates this process has been priceless for us, and has very much improved the quality of our code.

Usage

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.

A .validate.json file will also be created to provide defaults to git-validate in order to run your scripts. This configuration is only used if you have not specified your own configuration in package.json.

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.

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.

Package.json

{
  "name": "your_project",
  "description": "just an example",
  "scripts": {
    "validate": "./command/to/run",
    "test": "./other/command"
  }
}

The contents of the validate and test properties are the shell commands to perform those functions. Having these specified in your package.json also lends you the ability to be able to run them manually like so:

npm run-script validate
npm test

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 .".

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.

{
  "name": "your_project",
  "description": "just an example",
  "scripts": {
    "lint": "jshint --with --different-options",
    "validate": "./command/to/run",
    "test": "./other/command"
  },
  "pre-commit": ["lint", "test"]
}

This example would run only the lint and test scripts, in that order.

Usage

npm install precommit-hook

Everything else is automatic!

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.

{
  "name": "your_project",
  "description": "just an example",
  "scripts": {
    "validate": "./command/to/run",
    "test": "./other/command"
  },
  "devDependencies": {
    "precommit-hook": "latest"
  }
}

JSHint Defaults

The default .jshintrc looks like the following:

{
  "node": true, // node environment

  "curly": true, // enforce using curly braces around blocks
  "latedef": true, // enforce defining a variable before using it
  "quotmark": true, // allows either " or ' but you must be consistent
  "undef": true, // error on use of undefined variables
  "unused": true, // error on variables that are defined but never used
  "trailing": true // error on trailing whitespace
}

And the default .jshintignore contains only one line

node_modules

Contact

Like the project? Hate it? Just want to say hi? Find me on twitter @quitlahok

License

MIT

More Repositories

1

dlite

The simplest way to use Docker on OS X
Go
2,345
star
2

node-github-hook

A simple node.js based github post-receive server
JavaScript
278
star
3

dhyve-os

a tiny OS for running docker in xhyve
Shell
194
star
4

git-validate

A module to help you create your own git hooks
JavaScript
96
star
5

bucker

A simple logging library for node.js
JavaScript
82
star
6

dhyve

docker meets xhyve
Shell
79
star
7

connect-mysql

A very small MySQL session store for connect
JavaScript
54
star
8

blankie

a hapi CSP plugin
JavaScript
52
star
9

riakpbc

really basic, really simple riak protocol buffer client library for node.js
JavaScript
35
star
10

riakdown

A riak-backed leveldown interface based on abstract-leveldown
JavaScript
28
star
11

protobuf.js

A super rough, super simple, protocol buffer encoding library
JavaScript
24
star
12

git-gh

github extensions for git
C
17
star
13

gitenforcer

A github pull request monitoring bot, with middleware support
JavaScript
16
star
14

muckraker

a simple lightweight wrapper around pg-promise
JavaScript
15
star
15

mudskipper

a resourceful router plugin for hapi
JavaScript
15
star
16

browser-agents

A bunch of user agent strings for the major browser families
JavaScript
15
star
17

ape

API document generation for github made simple
JavaScript
14
star
18

lab-babel

A transform to allow testing babel.js transpiled code in lab more effectively
JavaScript
13
star
19

hapi-session

A session based auth scheme for hapi
JavaScript
8
star
20

node-butils

helper functions for node.js buffers for faster i/o
JavaScript
8
star
21

markitup

simple markdown to html converter, with code syntax highlighting
JavaScript
7
star
22

wadofgum

A super minimal purely functional modeling library
JavaScript
6
star
23

node-simpleriak

A very simple riak HTTP client
JavaScript
6
star
24

node-req

JavaScript
6
star
25

terranetes

a terraform project to launch kubernetes in packet
HCL
6
star
26

boltons

a struct mapper wrapper for golang and boltdb
Go
6
star
27

connect-githubhook

a simple github post-receive hook implemented as connect middleware
JavaScript
6
star
28

node-lazydb

A lazy file-based database
JavaScript
5
star
29

squeaky

a minimal nsq client for node.js
JavaScript
5
star
30

node-proto2json

Google Protocol Buffers .proto files to JSON converter
JavaScript
4
star
31

proxcheck

An OSX bluetooth proximity checker written in python
Python
4
star
32

porker

because it's a worker, but for postgres
JavaScript
3
star
33

go-clinic

a cli library for golang that aims to be simple to use
Go
3
star
34

gh

github utilities rewritten in go
Go
3
star
35

update-itunes

A python script to keep your Mac's iTunes library up to date
Python
3
star
36

shrinkydink

an npm-shrinkwrap.json sanitizer
JavaScript
3
star
37

connect-lazydb

JavaScript
2
star
38

objstream

an objectMode to regular stream transform
JavaScript
2
star
39

kask

kubernetes as kode
JavaScript
2
star
40

gomods

A dead simple Golang package manager
Shell
2
star
41

hyperterm-tab-numbers

move the close button left, put the tab number on the right in hyperterm
JavaScript
2
star
42

piles

a stackdriver logging and tracing plugin for hapi
JavaScript
2
star
43

configmaster

a daemon to watch for configmap and secret changes in your k8s cluster and update your deployments that reference them
Go
2
star
44

riakproto

the proto files from riak converted to json
JavaScript
1
star
45

doubles

hapi plugin to redirect removing www. prefix from hostnames
JavaScript
1
star
46

mutis

mutexes for node.js, backed by redis
JavaScript
1
star
47

wadofgum-nedb

JavaScript
1
star
48

cli-logger

a logger that i use for my own cli tools
JavaScript
1
star
49

mkpasswd

a quick and dirty mkpasswd for node
JavaScript
1
star
50

fierce-ruby

Ruby
1
star
51

airodump-dradis

Ruby
1
star
52

node-dirscan

A very basic remote directory scanner
JavaScript
1
star
53

node-jschema

a JSON-schema validation library for node.js
JavaScript
1
star