Skip to content

Commit

Permalink
feat!: output using proc-log
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The existing banner is now emitted using `proc-log`
instead of `console.log`.  It is always emitted. Consuming libraries can
decide under which situations to show the banner.
  • Loading branch information
wraithgar committed Apr 12, 2024
1 parent 69610b9 commit fb8f731
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 58 deletions.
29 changes: 11 additions & 18 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ const packageEnvs = require('./package-envs.js')
const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp')
const signalManager = require('./signal-manager.js')
const isServerPackage = require('./is-server-package.js')

// you wouldn't like me when I'm angry...
const bruce = (id, event, cmd, args) => {
let banner = id
? `\n> ${id} ${event}\n`
: `\n> ${event}\n`
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')}`
if (args.length) {
banner += ` ${args.join(' ')}`
}
banner += '\n'
return banner
}
const { output } = require('proc-log')

const runScriptPkg = async options => {
const {
Expand All @@ -29,8 +17,6 @@ const runScriptPkg = async options => {
pkg,
args = [],
stdioString,
// note: only used when stdio:inherit
banner = true,
// how long to wait for a process.kill signal
// only exposed here so that we can make the test go a bit faster.
signalTimeout = 500,
Expand Down Expand Up @@ -59,10 +45,17 @@ const runScriptPkg = async options => {
return { code: 0, signal: null }
}

if (stdio === 'inherit' && banner !== false) {
// we're dumping to the parent's stdout, so print the banner
console.log(bruce(pkg._id, event, cmd, args))
let banner
if (pkg._id) {
banner = `\n> ${pkg._id} ${event}\n`
} else {
banner = `\n> ${event}\n`
}
if (args.length) {
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')} ${args.join(' ')}\n`
}
// consuming library decides if this is displayed or not
output.standard(banner)

const [spawnShell, spawnArgs, spawnOpts] = makeSpawnArgs({
event,
Expand Down
55 changes: 15 additions & 40 deletions test/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ const isWindows = process.platform === 'win32'
const emptyDir = t.testdir({})

const pkill = process.kill
const consoleLog = console.log

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

t.test('run-script-pkg', async t => {
await t.test('do the banner when stdio is inherited, handle line breaks', async t => {
const logs = mockConsole(t)
await t.test('output with no args and a pkgid', async t => {
spawk.spawn('sh', a => a.includes('bar\nbaz\n'))
await runScript({
event: 'foo',
Expand All @@ -33,34 +34,11 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(logs, [['\n> [email protected] foo\n> bar\n> baz\n']])
t.strictSame(output, [['\n> [email protected] foo\n']])
t.ok(spawk.done())
})

await t.test('do not show banner when stdio is inherited, if suppressed', async t => {
const logs = mockConsole(t)
spawk.spawn('sh', a => a.includes('bar'))
await runScript({
event: 'foo',
path: emptyDir,
scriptShell: 'sh',
env: {
environ: 'value',
},
stdio: 'inherit',
cmd: 'bar',
pkg: {
_id: '[email protected]',
scripts: {},
},
banner: false,
})
t.strictSame(logs, [])
t.ok(spawk.done())
})

await t.test('do the banner with no pkgid', async t => {
const logs = mockConsole(t)
await t.test('output with args and no pkgid', async t => {
spawk.spawn('sh', a => a.includes('bar baz buzz'))
await runScript({
event: 'foo',
Expand All @@ -76,12 +54,11 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(logs, [['\n> foo\n> bar baz buzz\n']])
t.strictSame(output, [['\n> foo\n> bar baz buzz\n']])
t.ok(spawk.done())
})

await t.test('pkg has foo script', async t => {
const logs = mockConsole(t)
spawk.spawn('sh', a => a.includes('bar'))
await runScript({
event: 'foo',
Expand All @@ -98,12 +75,11 @@ t.test('run-script-pkg', async t => {
},
},
})
t.strictSame(logs, [])
t.strictSame(output, [['\n> [email protected] foo\n']])
t.ok(spawk.done())
})

await t.test('pkg has foo script, with args', async t => {
const logs = mockConsole(t)
spawk.spawn('sh', a => a.includes('bar a b c'))
await runScript({
event: 'foo',
Expand All @@ -122,7 +98,7 @@ t.test('run-script-pkg', async t => {
args: ['a', 'b', 'c'],
binPaths: false,
})
t.strictSame(logs, [])
t.strictSame(output, [['\n> [email protected] foo\n> bar a b c\n']])
t.ok(spawk.done())
})

Expand All @@ -131,7 +107,6 @@ t.test('run-script-pkg', async t => {
'binding.gyp': 'exists',
})

const logs = mockConsole(t)
spawk.spawn('sh', a => a.includes('node-gyp rebuild'))
await runScript({
event: 'install',
Expand All @@ -146,7 +121,7 @@ t.test('run-script-pkg', async t => {
scripts: {},
},
})
t.strictSame(logs, [])
t.strictSame(output, [['\n> [email protected] install\n']])
t.ok(spawk.done())
})

Expand Down

0 comments on commit fb8f731

Please sign in to comment.