@@ -23,23 +23,23 @@ let PATH = 'PATH'
23
23
// windows calls it's path 'Path' usually, but this is not guaranteed.
24
24
if ( process . platform === 'win32' ) {
25
25
PATH = 'Path'
26
- Object . keys ( process . env ) . forEach ( function ( e ) {
26
+ Object . keys ( process . env ) . forEach ( e => {
27
27
if ( e . match ( / ^ P A T H $ / i) ) {
28
28
PATH = e
29
29
}
30
30
} )
31
31
}
32
32
33
33
function logid ( pkg , stage ) {
34
- return pkg . _id + '~' + stage + ':'
34
+ return ` ${ pkg . _id } ~ ${ stage } :`
35
35
}
36
36
37
37
function hookStat ( dir , stage , cb ) {
38
38
const hook = path . join ( dir , '.hooks' , stage )
39
39
const cachedStatError = hookStatCache . get ( hook )
40
40
41
41
if ( cachedStatError === undefined ) {
42
- return fs . stat ( hook , function ( statError ) {
42
+ return fs . stat ( hook , statError => {
43
43
hookStatCache . set ( hook , statError )
44
44
cb ( statError )
45
45
} )
@@ -61,12 +61,12 @@ function lifecycle (pkg, stage, wd, opts) {
61
61
delete pkg . scripts . prepublish
62
62
}
63
63
64
- hookStat ( opts . dir , stage , function ( statError ) {
64
+ hookStat ( opts . dir , stage , statError => {
65
65
// makeEnv is a slow operation. This guard clause prevents makeEnv being called
66
66
// and avoids a ton of unnecessary work, and results in a major perf boost.
67
67
if ( ! pkg . scripts [ stage ] && statError ) return resolve ( )
68
68
69
- validWd ( wd || path . resolve ( opts . dir , pkg . name ) , function ( er , wd ) {
69
+ validWd ( wd || path . resolve ( opts . dir , pkg . name ) , ( er , wd ) => {
70
70
if ( er ) return reject ( er )
71
71
72
72
if ( ( wd . indexOf ( opts . dir ) !== 0 || _incorrectWorkingDirectory ( wd , pkg ) ) &&
@@ -76,7 +76,7 @@ function lifecycle (pkg, stage, wd, opts) {
76
76
}
77
77
78
78
// set the env variables, then run scripts as a child process.
79
- var env = makeEnv ( pkg , opts )
79
+ const env = makeEnv ( pkg , opts )
80
80
env . npm_lifecycle_event = stage
81
81
env . npm_node_execpath = env . NODE = env . NODE || process . execPath
82
82
env . npm_execpath = require . main . filename
@@ -101,11 +101,11 @@ function _incorrectWorkingDirectory (wd, pkg) {
101
101
}
102
102
103
103
function lifecycle_ ( pkg , stage , wd , opts , env , cb ) {
104
- var pathArr = [ ]
105
- var p = wd . split ( / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / )
106
- var acc = path . resolve ( p . shift ( ) )
104
+ const pathArr = [ ]
105
+ const p = wd . split ( / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / )
106
+ let acc = path . resolve ( p . shift ( ) )
107
107
108
- p . forEach ( function ( pp ) {
108
+ p . forEach ( pp => {
109
109
pathArr . unshift ( path . join ( acc , 'node_modules' , '.bin' ) )
110
110
acc = path . join ( acc , 'node_modules' , pp )
111
111
} )
@@ -123,7 +123,7 @@ function lifecycle_ (pkg, stage, wd, opts, env, cb) {
123
123
if ( env [ PATH ] ) pathArr . push ( env [ PATH ] )
124
124
env [ PATH ] = pathArr . join ( process . platform === 'win32' ? ';' : ':' )
125
125
126
- var packageLifecycle = pkg . scripts && pkg . scripts . hasOwnProperty ( stage )
126
+ let packageLifecycle = pkg . scripts && pkg . scripts . hasOwnProperty ( stage )
127
127
128
128
if ( opts . ignoreScripts ) {
129
129
opts . log . info ( 'lifecycle' , logid ( pkg , stage ) , 'ignored because ignore-scripts is set to true' , pkg . _id )
@@ -132,7 +132,7 @@ function lifecycle_ (pkg, stage, wd, opts, env, cb) {
132
132
// define this here so it's available to all scripts.
133
133
env . npm_lifecycle_script = pkg . scripts [ stage ]
134
134
} else {
135
- opts . log . silly ( 'lifecycle' , logid ( pkg , stage ) , ' no script for ' + stage + ' , continuing' )
135
+ opts . log . silly ( 'lifecycle' , logid ( pkg , stage ) , ` no script for ${ stage } , continuing` )
136
136
}
137
137
138
138
function done ( er ) {
@@ -162,10 +162,10 @@ function shouldPrependCurrentNodeDirToPATH (opts) {
162
162
if ( cfgsetting === false ) return false
163
163
if ( cfgsetting === true ) return true
164
164
165
- var isDifferentNodeInPath
165
+ let isDifferentNodeInPath
166
166
167
- var isWindows = process . platform === 'win32'
168
- var foundExecPath
167
+ const isWindows = process . platform === 'win32'
168
+ let foundExecPath
169
169
try {
170
170
foundExecPath = which . sync ( path . basename ( process . execPath ) , { pathExt : isWindows ? ';' : ':' } )
171
171
// Apply `fs.realpath()` here to avoid false positives when `node` is a symlinked executable.
@@ -192,9 +192,9 @@ function shouldPrependCurrentNodeDirToPATH (opts) {
192
192
}
193
193
194
194
function validWd ( d , cb ) {
195
- fs . stat ( d , function ( er , st ) {
195
+ fs . stat ( d , ( er , st ) => {
196
196
if ( er || ! st . isDirectory ( ) ) {
197
- var p = path . dirname ( d )
197
+ const p = path . dirname ( d )
198
198
if ( p === d ) {
199
199
return cb ( new Error ( 'Could not find suitable wd' ) )
200
200
}
@@ -206,19 +206,18 @@ function validWd (d, cb) {
206
206
207
207
function runPackageLifecycle ( pkg , stage , env , wd , opts , cb ) {
208
208
// run package lifecycle scripts in the package root, or the nearest parent.
209
- var cmd = env . npm_lifecycle_script
209
+ const cmd = env . npm_lifecycle_script
210
210
211
- var note = '\n> ' + pkg . _id + ' ' + stage + ' ' + wd +
212
- '\n> ' + cmd + '\n'
211
+ const note = `\n> ${ pkg . _id } ${ stage } ${ wd } \n> ${ cmd } \n`
213
212
runCmd ( note , cmd , pkg , env , stage , wd , opts , cb )
214
213
}
215
214
216
- var running = false
217
- var queue = [ ]
215
+ let running = false
216
+ const queue = [ ]
218
217
function dequeue ( ) {
219
218
running = false
220
219
if ( queue . length ) {
221
- var r = queue . shift ( )
220
+ const r = queue . shift ( )
222
221
runCmd . apply ( null , r )
223
222
}
224
223
}
@@ -233,9 +232,9 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
233
232
running = true
234
233
}
235
234
opts . log . pause ( )
236
- var unsafe = opts . unsafePerm
237
- var user = unsafe ? null : opts . user
238
- var group = unsafe ? null : opts . group
235
+ let unsafe = opts . unsafePerm
236
+ const user = unsafe ? null : opts . user
237
+ const group = unsafe ? null : opts . group
239
238
240
239
if ( opts . log . level !== 'silent' ) {
241
240
opts . log . clearProgress ( )
@@ -251,7 +250,7 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
251
250
if ( unsafe ) {
252
251
runCmd_ ( cmd , pkg , env , wd , opts , stage , unsafe , 0 , 0 , cb )
253
252
} else {
254
- uidNumber ( user , group , function ( er , uid , gid ) {
253
+ uidNumber ( user , group , ( er , uid , gid ) => {
255
254
runCmd_ ( cmd , pkg , env , wd , opts , stage , unsafe , uid , gid , cb )
256
255
} )
257
256
}
@@ -264,7 +263,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
264
263
process . nextTick ( dequeue )
265
264
}
266
265
267
- var conf = {
266
+ const conf = {
268
267
cwd : wd ,
269
268
env : env ,
270
269
stdio : opts . stdio || [ 0 , 1 , 2 ]
@@ -275,10 +274,10 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
275
274
conf . gid = gid ^ 0
276
275
}
277
276
278
- var sh = 'sh'
279
- var shFlag = '-c'
277
+ let sh = 'sh'
278
+ let shFlag = '-c'
280
279
281
- var customShell = opts . scriptShell
280
+ const customShell = opts . scriptShell
282
281
283
282
if ( customShell ) {
284
283
sh = customShell
@@ -292,37 +291,36 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
292
291
opts . log . verbose ( 'lifecycle' , logid ( pkg , stage ) , 'CWD:' , wd )
293
292
opts . log . silly ( 'lifecycle' , logid ( pkg , stage ) , 'Args:' , [ shFlag , cmd ] )
294
293
295
- var proc = spawn ( sh , [ shFlag , cmd ] , conf , opts . log )
294
+ const proc = spawn ( sh , [ shFlag , cmd ] , conf , opts . log )
296
295
297
296
proc . on ( 'error' , procError )
298
- proc . on ( 'close' , function ( code , signal ) {
297
+ proc . on ( 'close' , ( code , signal ) => {
299
298
opts . log . silly ( 'lifecycle' , logid ( pkg , stage ) , 'Returned: code:' , code , ' signal:' , signal )
300
299
if ( signal ) {
301
300
process . kill ( process . pid , signal )
302
301
} else if ( code ) {
303
- var er = new Error ( ' Exit status ' + code )
302
+ var er = new Error ( ` Exit status ${ code } ` )
304
303
er . errno = code
305
304
}
306
305
procError ( er )
307
306
} )
308
- byline ( proc . stdout ) . on ( 'data' , function ( data ) {
307
+ byline ( proc . stdout ) . on ( 'data' , data => {
309
308
opts . log . verbose ( 'lifecycle' , logid ( pkg , stage ) , 'stdout' , data . toString ( ) )
310
309
} )
311
- byline ( proc . stderr ) . on ( 'data' , function ( data ) {
310
+ byline ( proc . stderr ) . on ( 'data' , data => {
312
311
opts . log . verbose ( 'lifecycle' , logid ( pkg , stage ) , 'stderr' , data . toString ( ) )
313
312
} )
314
313
process . once ( 'SIGTERM' , procKill )
315
314
process . once ( 'SIGINT' , procInterupt )
316
315
317
316
function procError ( er ) {
318
317
if ( er ) {
319
- opts . log . info ( 'lifecycle' , logid ( pkg , stage ) , 'Failed to exec ' + stage + ' script' )
320
- er . message = pkg . _id + ' ' + stage + ': `' + cmd + '`\n' +
321
- er . message
318
+ opts . log . info ( 'lifecycle' , logid ( pkg , stage ) , `Failed to exec ${ stage } script` )
319
+ er . message = `${ pkg . _id } ${ stage } : \`${ cmd } \`\n${ er . message } `
322
320
if ( er . code !== 'EPERM' ) {
323
321
er . code = 'ELIFECYCLE'
324
322
}
325
- fs . stat ( opts . dir , function ( statError , d ) {
323
+ fs . stat ( opts . dir , ( statError , d ) => {
326
324
if ( statError && statError . code === 'ENOENT' && opts . dir . split ( path . sep ) . slice ( - 1 ) [ 0 ] === 'node_modules' ) {
327
325
opts . log . warn ( '' , 'Local package.json exists, but node_modules missing, did you mean to install?' )
328
326
}
@@ -342,19 +340,18 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
342
340
}
343
341
function procInterupt ( ) {
344
342
proc . kill ( 'SIGINT' )
345
- proc . on ( 'exit' , function ( ) {
343
+ proc . on ( 'exit' , ( ) => {
346
344
process . exit ( )
347
345
} )
348
346
process . once ( 'SIGINT' , procKill )
349
347
}
350
348
}
351
349
352
350
function runHookLifecycle ( pkg , stage , env , wd , opts , cb ) {
353
- hookStat ( opts . dir , stage , function ( er ) {
351
+ hookStat ( opts . dir , stage , er => {
354
352
if ( er ) return cb ( )
355
- var cmd = path . join ( opts . dir , '.hooks' , stage )
356
- var note = '\n> ' + pkg . _id + ' ' + stage + ' ' + wd +
357
- '\n> ' + cmd
353
+ const cmd = path . join ( opts . dir , '.hooks' , stage )
354
+ const note = `\n> ${ pkg . _id } ${ stage } ${ wd } \n> ${ cmd } `
358
355
runCmd ( note , cmd , pkg , env , stage , wd , opts , cb )
359
356
} )
360
357
}
@@ -384,29 +381,29 @@ function makeEnv (data, opts, prefix, env) {
384
381
385
382
for ( i in data ) {
386
383
if ( i . charAt ( 0 ) !== '_' ) {
387
- var envKey = ( prefix + i ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
384
+ const envKey = ( prefix + i ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
388
385
if ( i === 'readme' ) {
389
386
continue
390
387
}
391
388
if ( data [ i ] && typeof data [ i ] === 'object' ) {
392
389
try {
393
390
// quick and dirty detection for cyclical structures
394
391
JSON . stringify ( data [ i ] )
395
- makeEnv ( data [ i ] , opts , envKey + '_' , env )
392
+ makeEnv ( data [ i ] , opts , ` ${ envKey } _` , env )
396
393
} catch ( ex ) {
397
394
// usually these are package objects.
398
395
// just get the path and basic details.
399
- var d = data [ i ]
396
+ const d = data [ i ]
400
397
makeEnv (
401
398
{ name : d . name , version : d . version , path : d . path } ,
402
399
opts ,
403
- envKey + '_' ,
400
+ ` ${ envKey } _` ,
404
401
env
405
402
)
406
403
}
407
404
} else {
408
405
env [ envKey ] = String ( data [ i ] )
409
- env [ envKey ] = env [ envKey ] . indexOf ( '\n' ) !== - 1
406
+ env [ envKey ] = env [ envKey ] . includes ( '\n' )
410
407
? JSON . stringify ( env [ envKey ] )
411
408
: env [ envKey ]
412
409
}
@@ -416,44 +413,44 @@ function makeEnv (data, opts, prefix, env) {
416
413
if ( prefix !== 'npm_package_' ) return env
417
414
418
415
prefix = 'npm_config_'
419
- var pkgConfig = { }
420
- var pkgVerConfig = { }
421
- var namePref = data . name + ':'
422
- var verPref = data . name + '@' + data . version + ':'
416
+ const pkgConfig = { }
417
+ const pkgVerConfig = { }
418
+ const namePref = ` ${ data . name } :`
419
+ const verPref = ` ${ data . name } @ ${ data . version } :`
423
420
424
- Object . keys ( opts . config ) . forEach ( function ( i ) {
421
+ Object . keys ( opts . config ) . forEach ( i => {
425
422
// in some rare cases (e.g. working with nerf darts), there are segmented
426
423
// "private" (underscore-prefixed) config names -- don't export
427
- if ( ( i . charAt ( 0 ) === '_' && i . indexOf ( '_' + namePref ) !== 0 ) || i . match ( / : _ / ) ) {
424
+ if ( ( i . charAt ( 0 ) === '_' && i . indexOf ( `_ ${ namePref } ` ) !== 0 ) || i . match ( / : _ / ) ) {
428
425
return
429
426
}
430
- var value = opts . config [ i ]
427
+ let value = opts . config [ i ]
431
428
if ( value instanceof Stream || Array . isArray ( value ) ) return
432
429
if ( i . match ( / u m a s k / ) ) value = umask . toString ( value )
433
430
if ( ! value ) value = ''
434
- else if ( typeof value === 'number' ) value = '' + value
431
+ else if ( typeof value === 'number' ) value = ` ${ value } `
435
432
else if ( typeof value !== 'string' ) value = JSON . stringify ( value )
436
433
437
- value = value . indexOf ( '\n' ) !== - 1
434
+ value = value . includes ( '\n' )
438
435
? JSON . stringify ( value )
439
436
: value
440
437
i = i . replace ( / ^ _ + / , '' )
441
- var k
438
+ let k
442
439
if ( i . indexOf ( namePref ) === 0 ) {
443
440
k = i . substr ( namePref . length ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
444
441
pkgConfig [ k ] = value
445
442
} else if ( i . indexOf ( verPref ) === 0 ) {
446
443
k = i . substr ( verPref . length ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
447
444
pkgVerConfig [ k ] = value
448
445
}
449
- var envKey = ( prefix + i ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
446
+ const envKey = ( prefix + i ) . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_' )
450
447
env [ envKey ] = value
451
448
} )
452
449
453
450
prefix = 'npm_package_config_'
454
- ; [ pkgConfig , pkgVerConfig ] . forEach ( function ( conf ) {
455
- for ( var i in conf ) {
456
- var envKey = ( prefix + i )
451
+ ; [ pkgConfig , pkgVerConfig ] . forEach ( conf => {
452
+ for ( const i in conf ) {
453
+ const envKey = ( prefix + i )
457
454
env [ envKey ] = conf [ i ]
458
455
}
459
456
} )
0 commit comments