@@ -6,6 +6,8 @@ const path = require('path')
66const util = require ( 'util' )
77const tty = require ( 'tty' )
88
9+ const fs = require ( 'fs' )
10+
911/**
1012 * Representation of a hook runner.
1113 *
@@ -20,6 +22,7 @@ function Hook (fn, options) {
2022
2123 this . options = options // Used for testing only. Ignore this. Don't touch.
2224 this . config = { } // pre-commit configuration from the `package.json`.
25+ this . configFile = null // Actual contents of `pre-commit.json` if the user created one.
2326 this . json = { } // Actual content of the `package.json`.
2427 this . npm = '' // The location of the `npm` binary.
2528 this . git = '' // The location of the `git` binary.
@@ -77,7 +80,7 @@ Hook.prototype.exec = function exec (bin, args) {
7780 * @api private
7881 */
7982Hook . prototype . parse = function parse ( ) {
80- const pre = this . json [ 'pre-commit' ] || this . json . precommit
83+ const pre = this . configFile || this . json [ 'pre-commit' ] || this . json . precommit
8184 const config = ! Array . isArray ( pre ) && typeof pre === 'object' ? pre : { } ;
8285
8386 [ 'silent' , 'colors' , 'template' ] . forEach ( function each ( flag ) {
@@ -180,6 +183,21 @@ Hook.prototype.initialize = function initialize () {
180183 this . status = this . status . stdout . toString ( ) . trim ( )
181184 this . root = this . root . stdout . toString ( ) . trim ( )
182185
186+ if ( fs . existsSync ( path . join ( this . root , '.pre-commit.json' ) ) ) {
187+ let configFile
188+
189+ try {
190+ const rawText = fs . readFileSync ( path . join ( this . root , '.pre-commit.json' ) , 'utf-8' ) . toString ( )
191+ configFile = JSON . parse ( rawText )
192+ } catch ( e ) {
193+ return this . log ( this . format ( Hook . log . preCommitConfig , e . message ) , 1 )
194+ }
195+
196+ if ( typeof configFile === 'object' && ! Array . isArray ( configFile ) ) {
197+ this . configFile = configFile
198+ }
199+ }
200+
183201 try {
184202 this . json = require ( path . join ( this . root , 'package.json' ) )
185203 this . parse ( )
@@ -282,6 +300,14 @@ Hook.log = {
282300 'Skipping the pre-commit hook.'
283301 ] . join ( '\n' ) ,
284302
303+ preCommitConfig : [
304+ 'Received an error while parsing or locating the `.pre-commit.json` file:' ,
305+ '' ,
306+ ' %s' ,
307+ '' ,
308+ 'Skipping the pre-commit hook.'
309+ ] . join ( '\n' ) ,
310+
285311 run : [
286312 'We have nothing pre-commit hooks to run. Either you\'re missing the `scripts`' ,
287313 'in your `package.json` or have configured pre-commit to run nothing.' ,
0 commit comments