From fb8f7319c4874c1a8e1b6df0c39e5c9fc87c74aa Mon Sep 17 00:00:00 2001 From: Gar Date: Fri, 12 Apr 2024 11:56:01 -0700 Subject: [PATCH] feat!: output using proc-log 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. --- lib/run-script-pkg.js | 29 +++++++++------------- test/run-script-pkg.js | 55 ++++++++++++------------------------------ 2 files changed, 26 insertions(+), 58 deletions(-) diff --git a/lib/run-script-pkg.js b/lib/run-script-pkg.js index ea33db5..133f105 100644 --- a/lib/run-script-pkg.js +++ b/lib/run-script-pkg.js @@ -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 { @@ -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, @@ -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, diff --git a/test/run-script-pkg.js b/test/run-script-pkg.js index 9ff5f92..c5798bb 100644 --- a/test/run-script-pkg.js +++ b/test/run-script-pkg.js @@ -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', @@ -33,34 +34,11 @@ t.test('run-script-pkg', async t => { scripts: {}, }, }) - t.strictSame(logs, [['\n> foo@1.2.3 foo\n> bar\n> baz\n']]) + t.strictSame(output, [['\n> foo@1.2.3 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: 'foo@1.2.3', - 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', @@ -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', @@ -98,12 +75,11 @@ t.test('run-script-pkg', async t => { }, }, }) - t.strictSame(logs, []) + t.strictSame(output, [['\n> foo@1.2.3 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', @@ -122,7 +98,7 @@ t.test('run-script-pkg', async t => { args: ['a', 'b', 'c'], binPaths: false, }) - t.strictSame(logs, []) + t.strictSame(output, [['\n> foo@1.2.3 foo\n> bar a b c\n']]) t.ok(spawk.done()) }) @@ -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', @@ -146,7 +121,7 @@ t.test('run-script-pkg', async t => { scripts: {}, }, }) - t.strictSame(logs, []) + t.strictSame(output, [['\n> foo@1.2.3 install\n']]) t.ok(spawk.done()) })