1- 'use strict' ;
1+ 'use strict'
22
3- var spawn = require ( 'cross-spawn' )
4- , which = require ( 'which' )
5- , path = require ( 'path' )
6- , util = require ( 'util' )
7- , tty = require ( 'tty' ) ;
3+ const spawn = require ( 'cross-spawn' )
4+ const which = require ( 'which' )
5+ const path = require ( 'path' )
6+ const util = require ( 'util' )
7+ const tty = require ( 'tty' )
88
99/**
1010 * Representation of a hook runner.
@@ -14,20 +14,20 @@ var spawn = require('cross-spawn')
1414 * @param {Object } options Optional configuration, primarily used for testing.
1515 * @api public
1616 */
17- function Hook ( fn , options ) {
18- if ( ! this ) return new Hook ( fn , options ) ;
19- options = options || { } ;
20-
21- this . options = options ; // Used for testing only. Ignore this. Don't touch.
22- this . config = { } ; // pre-commit configuration from the `package.json`.
23- this . json = { } ; // Actual content of the `package.json`.
24- this . npm = '' ; // The location of the `npm` binary.
25- this . git = '' ; // The location of the `git` binary.
26- this . root = '' ; // The root location of the .git folder.
27- this . status = '' ; // Contents of the `git status`.
28- this . exit = fn ; // Exit function.
29-
30- this . initialize ( ) ;
17+ function Hook ( fn , options ) {
18+ if ( ! this ) return new Hook ( fn , options )
19+ options = options || { }
20+
21+ this . options = options // Used for testing only. Ignore this. Don't touch.
22+ this . config = { } // pre-commit configuration from the `package.json`.
23+ this . json = { } // Actual content of the `package.json`.
24+ this . npm = '' // The location of the `npm` binary.
25+ this . git = '' // The location of the `git` binary.
26+ this . root = '' // The root location of the .git folder.
27+ this . status = '' // Contents of the `git status`.
28+ this . exit = fn // Exit function.
29+
30+ this . initialize ( )
3131}
3232
3333/**
@@ -38,10 +38,10 @@ function Hook(fn, options) {
3838 * @public
3939 */
4040Object . defineProperty ( Hook . prototype , 'silent' , {
41- get : function silent ( ) {
42- return ! ! this . config . silent ;
41+ get : function silent ( ) {
42+ return ! ! this . config . silent
4343 }
44- } ) ;
44+ } )
4545
4646/**
4747 * Boolean indicating if we're allowed and capable of outputting colors into the
@@ -51,10 +51,10 @@ Object.defineProperty(Hook.prototype, 'silent', {
5151 * @public
5252 */
5353Object . defineProperty ( Hook . prototype , 'colors' , {
54- get : function colors ( ) {
55- return this . config . colors !== false && tty . isatty ( process . stdout . fd ) ;
54+ get : function colors ( ) {
55+ return this . config . colors !== false && tty . isatty ( process . stdout . fd )
5656 }
57- } ) ;
57+ } )
5858
5959/**
6060 * Execute a binary.
@@ -64,50 +64,50 @@ Object.defineProperty(Hook.prototype, 'colors', {
6464 * @returns {Object }
6565 * @api private
6666 */
67- Hook . prototype . exec = function exec ( bin , args ) {
67+ Hook . prototype . exec = function exec ( bin , args ) {
6868 return spawn . sync ( bin , args , {
6969 stdio : 'pipe'
70- } ) ;
71- } ;
70+ } )
71+ }
7272
7373/**
7474 * Parse the package.json so we can create an normalize it's contents to
7575 * a usable configuration structure.
7676 *
7777 * @api private
7878 */
79- Hook . prototype . parse = function parse ( ) {
80- var pre = this . json [ 'pre-commit' ] || this . json . precommit
81- , config = ! Array . isArray ( pre ) && 'object' === typeof pre ? pre : { } ;
79+ Hook . prototype . parse = function parse ( ) {
80+ const pre = this . json [ 'pre-commit' ] || this . json . precommit
81+ const config = ! Array . isArray ( pre ) && typeof pre === 'object' ? pre : { } ;
8282
83- [ 'silent' , 'colors' , 'template' ] . forEach ( function each ( flag ) {
84- var value ;
83+ [ 'silent' , 'colors' , 'template' ] . forEach ( function each ( flag ) {
84+ let value
8585
86- if ( flag in config ) value = config [ flag ] ;
87- else if ( 'precommit.' + flag in this . json ) value = this . json [ 'precommit.' + flag ] ;
88- else if ( 'pre-commit.' + flag in this . json ) value = this . json [ 'pre-commit.' + flag ] ;
89- else return ;
86+ if ( flag in config ) value = config [ flag ]
87+ else if ( 'precommit.' + flag in this . json ) value = this . json [ 'precommit.' + flag ]
88+ else if ( 'pre-commit.' + flag in this . json ) value = this . json [ 'pre-commit.' + flag ]
89+ else return
9090
91- config [ flag ] = value ;
92- } , this ) ;
91+ config [ flag ] = value
92+ } , this )
9393
9494 //
9595 // The scripts we need to run can be set under the `run` property.
9696 //
97- config . run = config . run || pre ;
97+ config . run = config . run || pre
9898
99- if ( 'string' === typeof config . run ) config . run = config . run . split ( / [ , ] + / ) ;
99+ if ( typeof config . run === 'string' ) config . run = config . run . split ( / [ , ] + / )
100100 if (
101- ! Array . isArray ( config . run )
102- && this . json . scripts
103- && this . json . scripts . test
104- && this . json . scripts . test !== 'echo "Error: no test specified" && exit 1'
101+ ! Array . isArray ( config . run ) &&
102+ this . json . scripts &&
103+ this . json . scripts . test &&
104+ this . json . scripts . test !== 'echo "Error: no test specified" && exit 1'
105105 ) {
106- config . run = [ 'test' ] ;
106+ config . run = [ 'test' ]
107107 }
108108
109- this . config = config ;
110- } ;
109+ this . config = config
110+ }
111111
112112/**
113113 * Write messages to the terminal, for feedback purposes.
@@ -116,80 +116,81 @@ Hook.prototype.parse = function parse() {
116116 * @param {Number } exit Exit code for the process.exit.
117117 * @api public
118118 */
119- Hook . prototype . log = function log ( lines , exit ) {
120- if ( ! Array . isArray ( lines ) ) lines = lines . split ( '\n' ) ;
121- if ( 'number' !== typeof exit ) exit = 1 ;
122-
123- var prefix = this . colors
124- ? '\u001b[38;5;166mpre-commit:\u001b[39;49m '
125- : 'pre-commit: ' ;
126-
127- lines . push ( '' ) ; // Whitespace at the end of the log.
128- lines . unshift ( '' ) ; // Whitespace at the beginning.
129-
130- lines = lines . map ( function map ( line ) {
131- return prefix + line ;
132- } ) ;
133-
134- if ( ! this . silent ) lines . forEach ( function output ( line ) {
135- if ( exit ) console . error ( line ) ;
136- else console . log ( line ) ;
137- } ) ;
119+ Hook . prototype . log = function log ( lines , exit ) {
120+ if ( ! Array . isArray ( lines ) ) lines = lines . split ( '\n' )
121+ if ( typeof exit !== 'number' ) exit = 1
122+
123+ const prefix = this . colors
124+ ? '\u001b[38;5;166mpre-commit:\u001b[39;49m '
125+ : 'pre-commit: '
126+
127+ lines . push ( '' ) // Whitespace at the end of the log.
128+ lines . unshift ( '' ) // Whitespace at the beginning.
129+
130+ lines = lines . map ( function map ( line ) {
131+ return prefix + line
132+ } )
133+
134+ if ( ! this . silent ) {
135+ lines . forEach ( function output ( line ) {
136+ if ( exit ) console . error ( line )
137+ else console . log ( line )
138+ } )
139+ }
138140
139- this . exit ( exit , lines ) ;
140- return exit === 0 ;
141- } ;
141+ this . exit ( exit , lines )
142+ return exit === 0
143+ }
142144
143145/**
144146 * Initialize all the values of the constructor to see if we can run as an
145147 * pre-commit hook.
146148 *
147149 * @api private
148150 */
149- Hook . prototype . initialize = function initialize ( ) {
150- [ 'git' , 'npm' ] . forEach ( function each ( binary ) {
151- try { this [ binary ] = which . sync ( binary ) ; }
152- catch ( e ) { }
153- } , this ) ;
151+ Hook . prototype . initialize = function initialize ( ) {
152+ [ 'git' , 'npm' ] . forEach ( function each ( binary ) {
153+ try { this [ binary ] = which . sync ( binary ) } catch ( e ) { }
154+ } , this )
154155
155156 //
156157 // in GUI clients node and npm are not in the PATH so get node binary PATH,
157158 // add it to the PATH list and try again.
158159 //
159160 if ( ! this . npm ) {
160161 try {
161- process . env . PATH += path . delimiter + path . dirname ( process . env . _ ) ;
162- this . npm = which . sync ( 'npm' ) ;
162+ process . env . PATH += path . delimiter + path . dirname ( process . env . _ )
163+ this . npm = which . sync ( 'npm' )
163164 } catch ( e ) {
164- return this . log ( this . format ( Hook . log . binary , 'npm' ) , 0 ) ;
165+ return this . log ( this . format ( Hook . log . binary , 'npm' ) , 0 )
165166 }
166167 }
167168
168169 //
169170 // Also bail out if we cannot find the git binary.
170171 //
171- if ( ! this . git ) return this . log ( this . format ( Hook . log . binary , 'git' ) , 0 ) ;
172+ if ( ! this . git ) return this . log ( this . format ( Hook . log . binary , 'git' ) , 0 )
172173
173- this . root = this . exec ( this . git , [ 'rev-parse' , '--show-toplevel' ] ) ;
174- this . status = this . exec ( this . git , [ 'status' , '--porcelain' ] ) ;
174+ this . root = this . exec ( this . git , [ 'rev-parse' , '--show-toplevel' ] )
175+ this . status = this . exec ( this . git , [ 'status' , '--porcelain' ] )
175176
176- if ( this . status . code ) return this . log ( Hook . log . status , 0 ) ;
177- if ( this . root . code ) return this . log ( Hook . log . root , 0 ) ;
177+ if ( this . status . code ) return this . log ( Hook . log . status , 0 )
178+ if ( this . root . code ) return this . log ( Hook . log . root , 0 )
178179
179- this . status = this . status . stdout . toString ( ) . trim ( ) ;
180- this . root = this . root . stdout . toString ( ) . trim ( ) ;
180+ this . status = this . status . stdout . toString ( ) . trim ( )
181+ this . root = this . root . stdout . toString ( ) . trim ( )
181182
182183 try {
183- this . json = require ( path . join ( this . root , 'package.json' ) ) ;
184- this . parse ( ) ;
185- } catch ( e ) { return this . log ( this . format ( Hook . log . json , e . message ) , 0 ) ; }
184+ this . json = require ( path . join ( this . root , 'package.json' ) )
185+ this . parse ( )
186+ } catch ( e ) { return this . log ( this . format ( Hook . log . json , e . message ) , 0 ) }
186187
187188 //
188189 // We can only check for changes after we've parsed the package.json as it
189190 // contains information if we need to suppress the empty message or not.
190191 //
191192 if ( ! this . status . length && ! this . options . ignorestatus ) {
192- return this . log ( Hook . log . empty , 0 ) ;
193+ return this . log ( Hook . log . empty , 0 )
193194 }
194195
195196 //
@@ -198,24 +199,24 @@ Hook.prototype.initialize = function initialize() {
198199 // execute.
199200 //
200201 if ( this . config . template ) {
201- this . exec ( this . git , [ 'config' , 'commit.template' , this . config . template ] ) ;
202+ this . exec ( this . git , [ 'config' , 'commit.template' , this . config . template ] )
202203 }
203204
204- if ( ! this . config . run ) return this . log ( Hook . log . run , 0 ) ;
205- } ;
205+ if ( ! this . config . run ) return this . log ( Hook . log . run , 0 )
206+ }
206207
207208/**
208209 * Run the specified hooks.
209210 *
210211 * @api public
211212 */
212- Hook . prototype . run = function runner ( ) {
213- var hooked = this ;
213+ Hook . prototype . run = function runner ( ) {
214+ const hooked = this ;
214215
215- ( function again ( scripts ) {
216- if ( ! scripts . length ) return hooked . exit ( 0 ) ;
216+ ( function again ( scripts ) {
217+ if ( ! scripts . length ) return hooked . exit ( 0 )
217218
218- var script = scripts . shift ( ) ;
219+ const script = scripts . shift ( )
219220
220221 //
221222 // There's a reason on why we're using an async `spawn` here instead of the
@@ -229,13 +230,13 @@ Hook.prototype.run = function runner() {
229230 env : process . env ,
230231 cwd : hooked . root ,
231232 stdio : [ 0 , 1 , 2 ]
232- } ) . once ( 'close' , function closed ( code ) {
233- if ( code ) return hooked . log ( hooked . format ( Hook . log . failure , script , code ) ) ;
233+ } ) . once ( 'close' , function closed ( code ) {
234+ if ( code ) return hooked . log ( hooked . format ( Hook . log . failure , script , code ) )
234235
235- again ( scripts ) ;
236- } ) ;
237- } ) ( hooked . config . run . slice ( 0 ) ) ;
238- } ;
236+ again ( scripts )
237+ } )
238+ } ) ( hooked . config . run . slice ( 0 ) )
239+ }
239240
240241/**
241242 * Expose some of our internal tools so plugins can also re-use them for their
@@ -244,7 +245,7 @@ Hook.prototype.run = function runner() {
244245 * @type {Function }
245246 * @public
246247 */
247- Hook . prototype . format = util . format ;
248+ Hook . prototype . format = util . format
248249
249250/**
250251 * The various of error and status messages that we can output.
@@ -296,20 +297,20 @@ Hook.log = {
296297 '' ,
297298 'This is ill-advised since the commit is broken.'
298299 ] . join ( '\n' )
299- } ;
300+ }
300301
301302//
302303// Expose the Hook instance so we can use it for testing purposes.
303304//
304- module . exports = Hook ;
305+ module . exports = Hook
305306
306307//
307308// Run directly if we're required executed directly through the CLI
308309//
309- if ( module !== require . main ) return ;
310-
311- var hook = new Hook ( function cli ( code ) {
312- process . exit ( code ) ;
313- } ) ;
310+ if ( module === require . main ) {
311+ const hook = new Hook ( function cli ( code ) {
312+ process . exit ( code )
313+ } )
314314
315- hook . run ( ) ;
315+ hook . run ( )
316+ }
0 commit comments