@@ -46,20 +46,34 @@ function resolve(opts, done) {
46
46
} ) ;
47
47
}
48
48
49
-
50
49
function check ( options , mode , done ) {
51
50
if ( mode === 'extra' ) {
52
- console . log ( ' ' + chalk . gray ( figures . pointerSmall ,
53
- 'Checking for dependencies in package.json not used in code'
54
- + figures . ellipsis ) ) ;
51
+ console . log (
52
+ ' ' +
53
+ chalk . gray (
54
+ figures . pointerSmall ,
55
+ 'Checking for dependencies in package.json not used in code' +
56
+ figures . ellipsis
57
+ )
58
+ ) ;
55
59
} else if ( mode === 'extra-dev' ) {
56
- console . log ( ' ' + chalk . gray ( figures . pointerSmall ,
57
- 'Checking for devDependencies in package.json not used in code'
58
- + figures . ellipsis ) ) ;
60
+ console . log (
61
+ ' ' +
62
+ chalk . gray (
63
+ figures . pointerSmall ,
64
+ 'Checking for devDependencies in package.json not used in code' +
65
+ figures . ellipsis
66
+ )
67
+ ) ;
59
68
} else {
60
- console . log ( ' ' + chalk . gray ( figures . pointerSmall ,
61
- 'Checking for dependencies used in code but not added to package.json'
62
- + figures . ellipsis ) ) ;
69
+ console . log (
70
+ ' ' +
71
+ chalk . gray (
72
+ figures . pointerSmall ,
73
+ 'Checking for dependencies used in code but not added to package.json' +
74
+ figures . ellipsis
75
+ )
76
+ ) ;
63
77
}
64
78
65
79
var pkg = require ( path . join ( options . dir , 'package.json' ) ) ;
@@ -104,91 +118,124 @@ function check(options, mode, done) {
104
118
} ) ;
105
119
}
106
120
107
- dc ( opts , function ( err , data ) {
108
- if ( err ) {
109
- return done ( err ) ;
110
- }
111
- pkg = data . package ;
112
-
113
- var deps = data . used ;
114
- var results ;
115
- var errMsg ;
116
- var corrector ;
117
-
118
- if ( mode === 'extra' ) {
119
- // Are all dependencies in package.json are used in the code?
120
- results = filterIgnored ( dc . extra ( pkg , deps , {
121
- excludeDev : true
122
- } ) ) . filter ( function ( name ) {
123
- return pkg . devDependencies [ name ] !== undefined ;
124
- } ) ;
121
+ dc ( opts )
122
+ . then ( function ( data ) {
123
+ pkg = data . package ;
125
124
126
- errMsg = results . length + ' dependencies in package.json are not used in code' ;
127
- corrector = 'npm uninstall --save ' + results . join ( ' ' ) + ';' ;
128
- } else if ( mode === 'extra-dev' ) {
129
- results = filterIgnored ( dc . extra ( pkg , deps , {
130
- excludeDev : false
131
- } ) ) . filter ( function ( name ) {
132
- return pkg . devDependencies [ name ] !== undefined ;
133
- } ) ;
125
+ var deps = data . used ;
126
+ var results ;
127
+ var errMsg ;
128
+ var corrector ;
134
129
135
- errMsg = results . length + ' devDependencies in package.json could not be detected as used in code' ;
136
- corrector = 'npm uninstall --save-dev ' + results . join ( ' ' ) + ';' ;
137
- } else {
138
- // Are we missing any dependencies in package.json?
139
- results = filterIgnored ( dc . missing ( pkg , deps ) ) ;
140
- errMsg = results . length + ' dependencies|devDependencies missing from package.json' ;
141
- corrector = results . map ( function ( name ) {
142
- return 'npm install --save ' + name + ';' ;
143
- } ) . join ( '\n' ) ;
144
- }
145
-
146
- if ( results . length === 0 ) {
147
130
if ( mode === 'extra' ) {
148
- console . log ( ' ' + chalk . green ( figures . tick ) ,
149
- ' No extra dependencies in package.json' ) ;
131
+ // Are all dependencies in package.json are used in the code?
132
+ results = filterIgnored (
133
+ dc . extra ( pkg , deps , {
134
+ excludeDev : true
135
+ } )
136
+ ) . filter ( function ( name ) {
137
+ return pkg . devDependencies [ name ] !== undefined ;
138
+ } ) ;
139
+
140
+ errMsg =
141
+ results . length + ' dependencies in package.json are not used in code' ;
142
+ corrector = 'npm uninstall --save ' + results . join ( ' ' ) + ';' ;
150
143
} else if ( mode === 'extra-dev' ) {
151
- console . log ( ' ' + chalk . green ( figures . tick ) ,
152
- ' No extra devDependencies in package.json' ) ;
144
+ results = filterIgnored (
145
+ dc . extra ( pkg , deps , {
146
+ excludeDev : false
147
+ } )
148
+ ) . filter ( function ( name ) {
149
+ return pkg . devDependencies [ name ] !== undefined ;
150
+ } ) ;
151
+
152
+ errMsg =
153
+ results . length +
154
+ ' devDependencies in package.json could not be detected as used in code' ;
155
+ corrector = 'npm uninstall --save-dev ' + results . join ( ' ' ) + ';' ;
153
156
} else {
154
- console . log ( ' ' + chalk . green ( figures . tick ) ,
155
- ' No missing dependencies in package.json' ) ;
157
+ // Are we missing any dependencies in package.json?
158
+ results = filterIgnored ( dc . missing ( pkg , deps ) ) ;
159
+ errMsg =
160
+ results . length +
161
+ ' dependencies|devDependencies missing from package.json' ;
162
+ corrector = results
163
+ . map ( function ( name ) {
164
+ return 'npm install --save ' + name + ';' ;
165
+ } )
166
+ . join ( '\n' ) ;
156
167
}
157
- return done ( ) ;
158
- }
159
168
160
- if ( mode === 'extra-dev' ) {
161
- var msg = [
162
- chalk . gray ( ' There are modules listed as devDependencies in package.json we\n' ) ,
163
- chalk . gray ( ' could not detect are being used in your code.\n\n' ) ,
164
- chalk . gray ( ' Advanced users should considering updating the `dependency-check`\n' ) ,
165
- chalk . gray ( ' configuration in package.json to add additional entrypoints to scan for usage.\n' ) ,
166
- chalk . gray ( ' We suggest running the following command to clean-up:\n' ) ,
167
- chalk . white . bold ( ' npm install --save ' + results . join ( ' ' ) + ';\n\n' ) ,
168
- chalk . gray ( '\n\nPlease see the configuration docs for more info:\n' ) ,
169
- chalk . blue ( 'https://github.com/mongodb-js/precommit#configuration' )
170
- ] . join ( '' ) ;
171
-
172
- var title = format ( '%d potentially unused devDependencies' , results . length ) ;
173
- options . result . warnings . push ( {
174
- title : title ,
175
- message : msg
176
- } ) ;
169
+ if ( results . length === 0 ) {
170
+ if ( mode === 'extra' ) {
171
+ console . log (
172
+ ' ' + chalk . green ( figures . tick ) ,
173
+ ' No extra dependencies in package.json'
174
+ ) ;
175
+ } else if ( mode === 'extra-dev' ) {
176
+ console . log (
177
+ ' ' + chalk . green ( figures . tick ) ,
178
+ ' No extra devDependencies in package.json'
179
+ ) ;
180
+ } else {
181
+ console . log (
182
+ ' ' + chalk . green ( figures . tick ) ,
183
+ ' No missing dependencies in package.json'
184
+ ) ;
185
+ }
186
+ return done ( ) ;
187
+ }
177
188
178
- console . log ( ' ' + chalk . yellow ( figures . warning ) , ' ' + title ) ;
179
- return done ( ) ;
180
- }
189
+ if ( mode === 'extra-dev' ) {
190
+ var msg = [
191
+ chalk . gray (
192
+ ' There are modules listed as devDependencies in package.json we\n'
193
+ ) ,
194
+ chalk . gray ( ' could not detect are being used in your code.\n\n' ) ,
195
+ chalk . gray (
196
+ ' Advanced users should considering updating the `dependency-check`\n'
197
+ ) ,
198
+ chalk . gray (
199
+ ' configuration in package.json to add additional entrypoints to scan for usage.\n'
200
+ ) ,
201
+ chalk . gray (
202
+ ' We suggest running the following command to clean-up:\n'
203
+ ) ,
204
+ chalk . white . bold (
205
+ ' npm install --save ' + results . join ( ' ' ) + ';\n\n'
206
+ ) ,
207
+ chalk . gray ( '\n\nPlease see the configuration docs for more info:\n' ) ,
208
+ chalk . blue ( 'https://github.com/mongodb-js/precommit#configuration' )
209
+ ] . join ( '' ) ;
210
+
211
+ var title = format (
212
+ '%d potentially unused devDependencies' ,
213
+ results . length
214
+ ) ;
215
+ options . result . warnings . push ( {
216
+ title : title ,
217
+ message : msg
218
+ } ) ;
181
219
182
- console . log ( ' ' + chalk . red ( figures . cross ) , ' ' + errMsg ) ;
220
+ console . log ( ' ' + chalk . yellow ( figures . warning ) , ' ' + title ) ;
221
+ return done ( ) ;
222
+ }
183
223
184
- errMsg += chalk . gray ( '\nYou can correct this error by running:' ) ;
185
- errMsg += '\n ' + chalk . bold . white ( corrector ) ;
186
- errMsg += chalk . gray ( '\n\nPlease see the configuration docs for more info:\n' ) ;
187
- errMsg += chalk . blue ( 'https://github.com/mongodb-js/precommit#configuration' ) ;
224
+ console . log ( ' ' + chalk . red ( figures . cross ) , ' ' + errMsg ) ;
188
225
189
- options . result . errors . push ( new Error ( errMsg ) ) ;
190
- return done ( ) ;
191
- } ) ;
226
+ errMsg += chalk . gray ( '\nYou can correct this error by running:' ) ;
227
+ errMsg += '\n ' + chalk . bold . white ( corrector ) ;
228
+ errMsg += chalk . gray (
229
+ '\n\nPlease see the configuration docs for more info:\n'
230
+ ) ;
231
+ errMsg += chalk . blue (
232
+ 'https://github.com/mongodb-js/precommit#configuration'
233
+ ) ;
234
+
235
+ options . result . errors . push ( new Error ( errMsg ) ) ;
236
+ return done ( ) ;
237
+ } )
238
+ . catch ( done ) ;
192
239
}
193
240
194
241
var lint = function ( opts , done ) {
@@ -203,9 +250,17 @@ var lint = function(opts, done) {
203
250
* at the files the user actually changed.
204
251
*/
205
252
debug ( 'linting files' , opts . files ) ;
206
- console . log ( ' ' + chalk . gray ( figures . pointerSmall ,
207
- format ( 'Running eslint on %d files%s' , opts . files . length ,
208
- figures . ellipsis ) ) ) ;
253
+ console . log (
254
+ ' ' +
255
+ chalk . gray (
256
+ figures . pointerSmall ,
257
+ format (
258
+ 'Running eslint on %d files%s' ,
259
+ opts . files . length ,
260
+ figures . ellipsis
261
+ )
262
+ )
263
+ ) ;
209
264
210
265
var report = cli . executeOnFiles ( opts . files ) ;
211
266
var formatter = cli . getFormatter ( ) ;
@@ -217,30 +272,34 @@ var lint = function(opts, done) {
217
272
218
273
debug ( 'eslint result' , JSON . stringify ( report . results , null , 2 ) ) ;
219
274
if ( report . errorCount > 0 ) {
220
- var msg = format (
221
- 'Please fix the %d error(s) below.' ,
222
- report . errorCount ) ;
275
+ var msg = format ( 'Please fix the %d error(s) below.' , report . errorCount ) ;
223
276
msg += '\n\n' + formatter ( report . results ) ;
224
277
var err = new Error ( msg ) ;
225
278
opts . result . errors . push ( err ) ;
226
- console . log ( ' ' + chalk . red . bold ( figures . cross ) ,
227
- report . errorCount , ' eslint errors detected' ) ;
279
+ console . log (
280
+ ' ' + chalk . red . bold ( figures . cross ) ,
281
+ report . errorCount ,
282
+ ' eslint errors detected'
283
+ ) ;
228
284
} else if ( report . warningCount > 0 ) {
229
285
var title = format ( '%s eslint warnings detected' , report . warningCount ) ;
230
286
console . log ( ' ' + chalk . yellow . bold ( figures . warning ) , ' ' + title ) ;
231
287
232
288
opts . result . warnings . push ( {
233
289
title : title ,
234
290
message : [
235
- chalk . gray ( format ( ' While eslint detected 0 potential errors,'
236
- + ' you may want to consider addressing these %s warnings:' , report . warningCount ) )
237
- + '\n\n' ,
291
+ chalk . gray (
292
+ format (
293
+ ' While eslint detected 0 potential errors,' +
294
+ ' you may want to consider addressing these %s warnings:' ,
295
+ report . warningCount
296
+ )
297
+ ) + '\n\n' ,
238
298
formatter ( report . results )
239
299
] . join ( '' )
240
300
} ) ;
241
301
} else {
242
- console . log ( ' ' + chalk . green ( figures . tick ) ,
243
- ' No errors found by eslint' ) ;
302
+ console . log ( ' ' + chalk . green ( figures . tick ) , ' No errors found by eslint' ) ;
244
303
}
245
304
done ( ) ;
246
305
} ;
@@ -284,9 +343,7 @@ module.exports = function(opts, done) {
284
343
lint . bind ( null , opts )
285
344
] ;
286
345
287
- var tasks = [
288
- resolve . bind ( null , opts )
289
- ] ;
346
+ var tasks = [ resolve . bind ( null , opts ) ] ;
290
347
tasks . push . apply ( tasks , checks ) ;
291
348
292
349
async . series ( tasks , function ( err ) {
@@ -295,8 +352,13 @@ module.exports = function(opts, done) {
295
352
}
296
353
297
354
if ( opts . result . errors . length > 0 ) {
298
- var error = new Error ( format ( '%d of %d check(s) failed:\n' ,
299
- opts . result . errors . length , checks . length ) ) ;
355
+ var error = new Error (
356
+ format (
357
+ '%d of %d check(s) failed:\n' ,
358
+ opts . result . errors . length ,
359
+ checks . length
360
+ )
361
+ ) ;
300
362
301
363
opts . result . errors . map ( function ( e ) {
302
364
e . message . split ( '\n' ) . map ( function ( line , i ) {
0 commit comments