Skip to content

Commit eeef49c

Browse files
committed
refactor: use modern syntax
1 parent b42624a commit eeef49c

File tree

1 file changed

+61
-64
lines changed

1 file changed

+61
-64
lines changed

index.js

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ let PATH = 'PATH'
2323
// windows calls it's path 'Path' usually, but this is not guaranteed.
2424
if (process.platform === 'win32') {
2525
PATH = 'Path'
26-
Object.keys(process.env).forEach(function (e) {
26+
Object.keys(process.env).forEach(e => {
2727
if (e.match(/^PATH$/i)) {
2828
PATH = e
2929
}
3030
})
3131
}
3232

3333
function logid (pkg, stage) {
34-
return pkg._id + '~' + stage + ':'
34+
return `${pkg._id}~${stage}:`
3535
}
3636

3737
function hookStat (dir, stage, cb) {
3838
const hook = path.join(dir, '.hooks', stage)
3939
const cachedStatError = hookStatCache.get(hook)
4040

4141
if (cachedStatError === undefined) {
42-
return fs.stat(hook, function (statError) {
42+
return fs.stat(hook, statError => {
4343
hookStatCache.set(hook, statError)
4444
cb(statError)
4545
})
@@ -61,12 +61,12 @@ function lifecycle (pkg, stage, wd, opts) {
6161
delete pkg.scripts.prepublish
6262
}
6363

64-
hookStat(opts.dir, stage, function (statError) {
64+
hookStat(opts.dir, stage, statError => {
6565
// makeEnv is a slow operation. This guard clause prevents makeEnv being called
6666
// and avoids a ton of unnecessary work, and results in a major perf boost.
6767
if (!pkg.scripts[stage] && statError) return resolve()
6868

69-
validWd(wd || path.resolve(opts.dir, pkg.name), function (er, wd) {
69+
validWd(wd || path.resolve(opts.dir, pkg.name), (er, wd) => {
7070
if (er) return reject(er)
7171

7272
if ((wd.indexOf(opts.dir) !== 0 || _incorrectWorkingDirectory(wd, pkg)) &&
@@ -76,7 +76,7 @@ function lifecycle (pkg, stage, wd, opts) {
7676
}
7777

7878
// set the env variables, then run scripts as a child process.
79-
var env = makeEnv(pkg, opts)
79+
const env = makeEnv(pkg, opts)
8080
env.npm_lifecycle_event = stage
8181
env.npm_node_execpath = env.NODE = env.NODE || process.execPath
8282
env.npm_execpath = require.main.filename
@@ -101,11 +101,11 @@ function _incorrectWorkingDirectory (wd, pkg) {
101101
}
102102

103103
function lifecycle_ (pkg, stage, wd, opts, env, cb) {
104-
var pathArr = []
105-
var p = wd.split(/[\\/]node_modules[\\/]/)
106-
var acc = path.resolve(p.shift())
104+
const pathArr = []
105+
const p = wd.split(/[\\/]node_modules[\\/]/)
106+
let acc = path.resolve(p.shift())
107107

108-
p.forEach(function (pp) {
108+
p.forEach(pp => {
109109
pathArr.unshift(path.join(acc, 'node_modules', '.bin'))
110110
acc = path.join(acc, 'node_modules', pp)
111111
})
@@ -123,7 +123,7 @@ function lifecycle_ (pkg, stage, wd, opts, env, cb) {
123123
if (env[PATH]) pathArr.push(env[PATH])
124124
env[PATH] = pathArr.join(process.platform === 'win32' ? ';' : ':')
125125

126-
var packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)
126+
let packageLifecycle = pkg.scripts && pkg.scripts.hasOwnProperty(stage)
127127

128128
if (opts.ignoreScripts) {
129129
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) {
132132
// define this here so it's available to all scripts.
133133
env.npm_lifecycle_script = pkg.scripts[stage]
134134
} 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`)
136136
}
137137

138138
function done (er) {
@@ -162,10 +162,10 @@ function shouldPrependCurrentNodeDirToPATH (opts) {
162162
if (cfgsetting === false) return false
163163
if (cfgsetting === true) return true
164164

165-
var isDifferentNodeInPath
165+
let isDifferentNodeInPath
166166

167-
var isWindows = process.platform === 'win32'
168-
var foundExecPath
167+
const isWindows = process.platform === 'win32'
168+
let foundExecPath
169169
try {
170170
foundExecPath = which.sync(path.basename(process.execPath), { pathExt: isWindows ? ';' : ':' })
171171
// Apply `fs.realpath()` here to avoid false positives when `node` is a symlinked executable.
@@ -192,9 +192,9 @@ function shouldPrependCurrentNodeDirToPATH (opts) {
192192
}
193193

194194
function validWd (d, cb) {
195-
fs.stat(d, function (er, st) {
195+
fs.stat(d, (er, st) => {
196196
if (er || !st.isDirectory()) {
197-
var p = path.dirname(d)
197+
const p = path.dirname(d)
198198
if (p === d) {
199199
return cb(new Error('Could not find suitable wd'))
200200
}
@@ -206,19 +206,18 @@ function validWd (d, cb) {
206206

207207
function runPackageLifecycle (pkg, stage, env, wd, opts, cb) {
208208
// 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
210210

211-
var note = '\n> ' + pkg._id + ' ' + stage + ' ' + wd +
212-
'\n> ' + cmd + '\n'
211+
const note = `\n> ${pkg._id} ${stage} ${wd}\n> ${cmd}\n`
213212
runCmd(note, cmd, pkg, env, stage, wd, opts, cb)
214213
}
215214

216-
var running = false
217-
var queue = []
215+
let running = false
216+
const queue = []
218217
function dequeue () {
219218
running = false
220219
if (queue.length) {
221-
var r = queue.shift()
220+
const r = queue.shift()
222221
runCmd.apply(null, r)
223222
}
224223
}
@@ -233,9 +232,9 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
233232
running = true
234233
}
235234
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
239238

240239
if (opts.log.level !== 'silent') {
241240
opts.log.clearProgress()
@@ -251,7 +250,7 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
251250
if (unsafe) {
252251
runCmd_(cmd, pkg, env, wd, opts, stage, unsafe, 0, 0, cb)
253252
} else {
254-
uidNumber(user, group, function (er, uid, gid) {
253+
uidNumber(user, group, (er, uid, gid) => {
255254
runCmd_(cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb)
256255
})
257256
}
@@ -264,7 +263,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
264263
process.nextTick(dequeue)
265264
}
266265

267-
var conf = {
266+
const conf = {
268267
cwd: wd,
269268
env: env,
270269
stdio: opts.stdio || [ 0, 1, 2 ]
@@ -275,10 +274,10 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
275274
conf.gid = gid ^ 0
276275
}
277276

278-
var sh = 'sh'
279-
var shFlag = '-c'
277+
let sh = 'sh'
278+
let shFlag = '-c'
280279

281-
var customShell = opts.scriptShell
280+
const customShell = opts.scriptShell
282281

283282
if (customShell) {
284283
sh = customShell
@@ -292,37 +291,36 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
292291
opts.log.verbose('lifecycle', logid(pkg, stage), 'CWD:', wd)
293292
opts.log.silly('lifecycle', logid(pkg, stage), 'Args:', [shFlag, cmd])
294293

295-
var proc = spawn(sh, [shFlag, cmd], conf, opts.log)
294+
const proc = spawn(sh, [shFlag, cmd], conf, opts.log)
296295

297296
proc.on('error', procError)
298-
proc.on('close', function (code, signal) {
297+
proc.on('close', (code, signal) => {
299298
opts.log.silly('lifecycle', logid(pkg, stage), 'Returned: code:', code, ' signal:', signal)
300299
if (signal) {
301300
process.kill(process.pid, signal)
302301
} else if (code) {
303-
var er = new Error('Exit status ' + code)
302+
var er = new Error(`Exit status ${code}`)
304303
er.errno = code
305304
}
306305
procError(er)
307306
})
308-
byline(proc.stdout).on('data', function (data) {
307+
byline(proc.stdout).on('data', data => {
309308
opts.log.verbose('lifecycle', logid(pkg, stage), 'stdout', data.toString())
310309
})
311-
byline(proc.stderr).on('data', function (data) {
310+
byline(proc.stderr).on('data', data => {
312311
opts.log.verbose('lifecycle', logid(pkg, stage), 'stderr', data.toString())
313312
})
314313
process.once('SIGTERM', procKill)
315314
process.once('SIGINT', procInterupt)
316315

317316
function procError (er) {
318317
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}`
322320
if (er.code !== 'EPERM') {
323321
er.code = 'ELIFECYCLE'
324322
}
325-
fs.stat(opts.dir, function (statError, d) {
323+
fs.stat(opts.dir, (statError, d) => {
326324
if (statError && statError.code === 'ENOENT' && opts.dir.split(path.sep).slice(-1)[0] === 'node_modules') {
327325
opts.log.warn('', 'Local package.json exists, but node_modules missing, did you mean to install?')
328326
}
@@ -342,19 +340,18 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
342340
}
343341
function procInterupt () {
344342
proc.kill('SIGINT')
345-
proc.on('exit', function () {
343+
proc.on('exit', () => {
346344
process.exit()
347345
})
348346
process.once('SIGINT', procKill)
349347
}
350348
}
351349

352350
function runHookLifecycle (pkg, stage, env, wd, opts, cb) {
353-
hookStat(opts.dir, stage, function (er) {
351+
hookStat(opts.dir, stage, er => {
354352
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}`
358355
runCmd(note, cmd, pkg, env, stage, wd, opts, cb)
359356
})
360357
}
@@ -384,29 +381,29 @@ function makeEnv (data, opts, prefix, env) {
384381

385382
for (i in data) {
386383
if (i.charAt(0) !== '_') {
387-
var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
384+
const envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
388385
if (i === 'readme') {
389386
continue
390387
}
391388
if (data[i] && typeof data[i] === 'object') {
392389
try {
393390
// quick and dirty detection for cyclical structures
394391
JSON.stringify(data[i])
395-
makeEnv(data[i], opts, envKey + '_', env)
392+
makeEnv(data[i], opts, `${envKey}_`, env)
396393
} catch (ex) {
397394
// usually these are package objects.
398395
// just get the path and basic details.
399-
var d = data[i]
396+
const d = data[i]
400397
makeEnv(
401398
{ name: d.name, version: d.version, path: d.path },
402399
opts,
403-
envKey + '_',
400+
`${envKey}_`,
404401
env
405402
)
406403
}
407404
} else {
408405
env[envKey] = String(data[i])
409-
env[envKey] = env[envKey].indexOf('\n') !== -1
406+
env[envKey] = env[envKey].includes('\n')
410407
? JSON.stringify(env[envKey])
411408
: env[envKey]
412409
}
@@ -416,44 +413,44 @@ function makeEnv (data, opts, prefix, env) {
416413
if (prefix !== 'npm_package_') return env
417414

418415
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}:`
423420

424-
Object.keys(opts.config).forEach(function (i) {
421+
Object.keys(opts.config).forEach(i => {
425422
// in some rare cases (e.g. working with nerf darts), there are segmented
426423
// "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(/:_/)) {
428425
return
429426
}
430-
var value = opts.config[i]
427+
let value = opts.config[i]
431428
if (value instanceof Stream || Array.isArray(value)) return
432429
if (i.match(/umask/)) value = umask.toString(value)
433430
if (!value) value = ''
434-
else if (typeof value === 'number') value = '' + value
431+
else if (typeof value === 'number') value = `${value}`
435432
else if (typeof value !== 'string') value = JSON.stringify(value)
436433

437-
value = value.indexOf('\n') !== -1
434+
value = value.includes('\n')
438435
? JSON.stringify(value)
439436
: value
440437
i = i.replace(/^_+/, '')
441-
var k
438+
let k
442439
if (i.indexOf(namePref) === 0) {
443440
k = i.substr(namePref.length).replace(/[^a-zA-Z0-9_]/g, '_')
444441
pkgConfig[k] = value
445442
} else if (i.indexOf(verPref) === 0) {
446443
k = i.substr(verPref.length).replace(/[^a-zA-Z0-9_]/g, '_')
447444
pkgVerConfig[k] = value
448445
}
449-
var envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
446+
const envKey = (prefix + i).replace(/[^a-zA-Z0-9_]/g, '_')
450447
env[envKey] = value
451448
})
452449

453450
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)
457454
env[envKey] = conf[i]
458455
}
459456
})

0 commit comments

Comments
 (0)