Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/make-spawn-args.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint camelcase: "off" */
const setPATH = require('./set-path.js')
const { resolve } = require('path')

let npm_config_node_gyp
let npmConfigNodeGyp

const makeSpawnArgs = options => {
const {
Expand All @@ -20,13 +19,13 @@ const makeSpawnArgs = options => {

if (nodeGyp) {
// npm already pulled this from env and passes it in to options
npm_config_node_gyp = nodeGyp
npmConfigNodeGyp = nodeGyp
} else if (env.npm_config_node_gyp) {
// legacy mode for standalone user
npm_config_node_gyp = env.npm_config_node_gyp
npmConfigNodeGyp = env.npm_config_node_gyp
} else {
// default
npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
npmConfigNodeGyp = require.resolve('node-gyp/bin/node-gyp.js')
}

const spawnEnv = setPATH(path, binPaths, {
Expand All @@ -36,7 +35,7 @@ const makeSpawnArgs = options => {
npm_package_json: resolve(path, 'package.json'),
npm_lifecycle_event: event,
npm_lifecycle_script: cmd,
npm_config_node_gyp,
npm_config_node_gyp: npmConfigNodeGyp,
})

const spawnOpts = {
Expand Down
14 changes: 4 additions & 10 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,13 @@ const runScriptPkg = async options => {

let inputEnd = () => {}
if (stdio === 'inherit') {
let banner
const { log, input } = require('proc-log')
if (pkg._id) {
banner = `\n> ${pkg._id} ${event}\n`
log.notice('run', `${pkg._id} ${event}`)
} else {
banner = `\n> ${event}\n`
log.notice('run', event)
}
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')}`
if (args.length) {
banner += ` ${args.join(' ')}`
}
banner += '\n'
const { output, input } = require('proc-log')
output.standard(banner)
log.notice('run', `${cmd.trim()} ${args?.join(' ')}`.trim())
inputEnd = input.start()
}

Expand Down
51 changes: 32 additions & 19 deletions test/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ const emptyDir = t.testdir({})

const pkill = process.kill

const output = []
const appendOutput = (level, ...args) => {
if (level === 'standard') {
output.push([...args])
}
const logs = []
const logHandler = (level, ...args) => {
logs.push([level, ...args])
}
process.on('output', appendOutput)
t.afterEach(() => output.length = 0)
t.teardown(() => process.removeListener('output', appendOutput))

t.test('run-script-pkg', async t => {
process.on('log', logHandler)
t.afterEach(() => logs.length = 0)
t.teardown(() => process.removeListener('log', logHandler))

await t.test('stdio inherit no args and a pkgid', async t => {
spawk.spawn('sh', a => a.includes('bar\nbaz\n'))
await runScript({
Expand All @@ -34,7 +33,10 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(output, [['\n> foo@1.2.3 foo\n> bar\n> baz\n']])
t.strictSame(logs, [
['notice', 'run', 'foo@1.2.3 foo'],
['notice', 'run', 'bar\nbaz'],
])
t.ok(spawk.done())
})

Expand All @@ -54,7 +56,10 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(output, [['\n> foo\n> bar baz buzz\n']])
t.strictSame(logs, [
['notice', 'run', 'foo'],
['notice', 'run', 'bar baz buzz'],
])
t.ok(spawk.done())
})

Expand All @@ -75,7 +80,7 @@ t.test('run-script-pkg', async t => {
},
},
})
t.strictSame(output, [])
t.strictSame(logs, [])
t.ok(spawk.done())
})

Expand All @@ -98,11 +103,10 @@ t.test('run-script-pkg', async t => {
args: ['a', 'b', 'c'],
binPaths: false,
})
t.strictSame(output, [])
t.strictSame(logs, [])
t.ok(spawk.done())
})

/* eslint-disable-next-line max-len */
await t.test('pkg has no install or preinstall script, node-gyp files present, stdio pipe', async t => {
const testdir = t.testdir({
'binding.gyp': 'exists',
Expand All @@ -122,7 +126,7 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(output, [])
t.strictSame(logs, [])
t.ok(spawk.done())
})

Expand All @@ -146,7 +150,7 @@ t.test('run-script-pkg', async t => {
},
},
})
t.strictSame(output, [])
t.strictSame(logs, [])
t.strictSame(res, { code: 0, signal: null })
})

Expand Down Expand Up @@ -196,7 +200,10 @@ t.test('run-script-pkg', async t => {
},
},
}))
t.strictSame(output, [['\n> husky@1.2.3 sleep\n> sleep 1000000\n']])
t.strictSame(logs, [
['notice', 'run', 'husky@1.2.3 sleep'],
['notice', 'run', 'sleep 1000000'],
])
t.ok(spawk.done())
if (!isWindows) {
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
Expand Down Expand Up @@ -233,7 +240,10 @@ t.test('run-script-pkg', async t => {
},
},
}))
t.strictSame(output, [['\n> husky@1.2.3 sleep\n> sleep 1000000\n']])
t.strictSame(logs, [
['notice', 'run', 'husky@1.2.3 sleep'],
['notice', 'run', 'sleep 1000000'],
])
t.ok(spawk.done())
if (!isWindows) {
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
Expand Down Expand Up @@ -269,7 +279,10 @@ t.test('run-script-pkg', async t => {
},
},
}))
t.strictSame(output, [['\n> husky@1.2.3 sleep\n> sleep 1000000\n']])
t.strictSame(logs, [
['notice', 'run', 'husky@1.2.3 sleep'],
['notice', 'run', 'sleep 1000000'],
])
t.ok(spawk.done())
if (!isWindows) {
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
Expand All @@ -294,7 +307,7 @@ t.test('run-script-pkg', async t => {
},
},
}))
t.strictSame(output, [])
t.strictSame(logs, [])
t.ok(spawk.done())
})
})
1 change: 0 additions & 1 deletion test/validate-options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable max-len */
const t = require('tap')
const runScript = require('..')

Expand Down
Loading