Skip to content

Commit 5f9cbcf

Browse files
committed
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.
1 parent 021758b commit 5f9cbcf

File tree

3 files changed

+47
-71
lines changed

3 files changed

+47
-71
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ runScript({
5757
// print the package id and script, and the command to be run, like:
5858
// > [email protected] postinstall
5959
// > make all-the-things
60-
// Defaults true when stdio:'inherit', otherwise suppressed
61-
banner: true,
6260
})
6361
.then(({ code, signal, stdout, stderr, pkgid, path, event, script }) => {
6462
// do something with the results
@@ -99,6 +97,11 @@ terminal, then it is up to the user to end it, of course.
9997
- `event` Lifecycle event being run
10098
- `script` Command being run
10199

100+
If stdio is `inherit` this package will emit a banner with the package
101+
name and version, event name, and script command to be run, and send it
102+
to [`proc-log.output.standard`](https://npm.im/proc-log). Consuming
103+
libraries can decide whether or not to display this.
104+
102105
### Options
103106

104107
- `path` Required. The path to the package having its script run.
@@ -124,10 +127,6 @@ terminal, then it is up to the user to end it, of course.
124127
- `stdioString` Optional, passed directly to `@npmcli/promise-spawn` which
125128
defaults it to `true`. Return string values for `stderr` and `stdout` rather
126129
than Buffers.
127-
- `banner` Optional, defaults to `true`. If the `stdio` option is set to
128-
`'inherit'`, then print a banner with the package name and version, event
129-
name, and script command to be run. Set explicitly to `false` to disable
130-
for inherited stdio.
131130

132131
Note that this does _not_ run pre-event and post-event scripts. The
133132
caller has to manage that process themselves.

lib/run-script-pkg.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp'
55
const signalManager = require('./signal-manager.js')
66
const isServerPackage = require('./is-server-package.js')
77

8-
// you wouldn't like me when I'm angry...
9-
const bruce = (id, event, cmd, args) => {
10-
let banner = id
11-
? `\n> ${id} ${event}\n`
12-
: `\n> ${event}\n`
13-
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')}`
14-
if (args.length) {
15-
banner += ` ${args.join(' ')}`
16-
}
17-
banner += '\n'
18-
return banner
19-
}
20-
218
const runScriptPkg = async options => {
229
const {
2310
event,
@@ -29,8 +16,6 @@ const runScriptPkg = async options => {
2916
pkg,
3017
args = [],
3118
stdioString,
32-
// note: only used when stdio:inherit
33-
banner = true,
3419
// how long to wait for a process.kill signal
3520
// only exposed here so that we can make the test go a bit faster.
3621
signalTimeout = 500,
@@ -59,9 +44,20 @@ const runScriptPkg = async options => {
5944
return { code: 0, signal: null }
6045
}
6146

62-
if (stdio === 'inherit' && banner !== false) {
63-
// we're dumping to the parent's stdout, so print the banner
64-
console.log(bruce(pkg._id, event, cmd, args))
47+
if (stdio === 'inherit') {
48+
let banner
49+
if (pkg._id) {
50+
banner = `\n> ${pkg._id} ${event}\n`
51+
} else {
52+
banner = `\n> ${event}\n`
53+
}
54+
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')}`
55+
if (args.length) {
56+
banner += ` ${args.join(' ')}`
57+
}
58+
banner += '\n'
59+
const { output } = require('proc-log')
60+
output.standard(banner)
6561
}
6662

6763
const [spawnShell, spawnArgs, spawnOpts] = makeSpawnArgs({

test/run-script-pkg.js

+28-47
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ const isWindows = process.platform === 'win32'
66
const emptyDir = t.testdir({})
77

88
const pkill = process.kill
9-
const consoleLog = console.log
109

11-
const mockConsole = t => {
12-
const logs = []
13-
console.log = (...args) => logs.push(args)
14-
t.teardown(() => console.log = consoleLog)
15-
return logs
10+
const output = []
11+
const appendOutput = (level, ...args) => {
12+
if (level === 'standard') {
13+
output.push([...args])
14+
}
1615
}
16+
process.on('output', appendOutput)
17+
t.afterEach(() => output.length = 0)
18+
t.teardown(() => process.removeListener('output', appendOutput))
1719

1820
t.test('run-script-pkg', async t => {
19-
await t.test('do the banner when stdio is inherited, handle line breaks', async t => {
20-
const logs = mockConsole(t)
21+
await t.test('stdio inherit no args and a pkgid', async t => {
2122
spawk.spawn('sh', a => a.includes('bar\nbaz\n'))
2223
await runScript({
2324
event: 'foo',
@@ -33,34 +34,11 @@ t.test('run-script-pkg', async t => {
3334
scripts: {},
3435
},
3536
})
36-
t.strictSame(logs, [['\n> [email protected] foo\n> bar\n> baz\n']])
37+
t.strictSame(output, [['\n> [email protected] foo\n> bar\n> baz\n']])
3738
t.ok(spawk.done())
3839
})
3940

40-
await t.test('do not show banner when stdio is inherited, if suppressed', async t => {
41-
const logs = mockConsole(t)
42-
spawk.spawn('sh', a => a.includes('bar'))
43-
await runScript({
44-
event: 'foo',
45-
path: emptyDir,
46-
scriptShell: 'sh',
47-
env: {
48-
environ: 'value',
49-
},
50-
stdio: 'inherit',
51-
cmd: 'bar',
52-
pkg: {
53-
54-
scripts: {},
55-
},
56-
banner: false,
57-
})
58-
t.strictSame(logs, [])
59-
t.ok(spawk.done())
60-
})
61-
62-
await t.test('do the banner with no pkgid', async t => {
63-
const logs = mockConsole(t)
41+
await t.test('stdio inherit args and no pkgid', async t => {
6442
spawk.spawn('sh', a => a.includes('bar baz buzz'))
6543
await runScript({
6644
event: 'foo',
@@ -76,12 +54,11 @@ t.test('run-script-pkg', async t => {
7654
scripts: {},
7755
},
7856
})
79-
t.strictSame(logs, [['\n> foo\n> bar baz buzz\n']])
57+
t.strictSame(output, [['\n> foo\n> bar baz buzz\n']])
8058
t.ok(spawk.done())
8159
})
8260

83-
await t.test('pkg has foo script', async t => {
84-
const logs = mockConsole(t)
61+
await t.test('pkg has foo script, with stdio pipe', async t => {
8562
spawk.spawn('sh', a => a.includes('bar'))
8663
await runScript({
8764
event: 'foo',
@@ -98,12 +75,11 @@ t.test('run-script-pkg', async t => {
9875
},
9976
},
10077
})
101-
t.strictSame(logs, [])
78+
t.strictSame(output, [])
10279
t.ok(spawk.done())
10380
})
10481

105-
await t.test('pkg has foo script, with args', async t => {
106-
const logs = mockConsole(t)
82+
await t.test('pkg has foo script, with stdio pipe and args', async t => {
10783
spawk.spawn('sh', a => a.includes('bar a b c'))
10884
await runScript({
10985
event: 'foo',
@@ -122,16 +98,16 @@ t.test('run-script-pkg', async t => {
12298
args: ['a', 'b', 'c'],
12399
binPaths: false,
124100
})
125-
t.strictSame(logs, [])
101+
t.strictSame(output, [])
126102
t.ok(spawk.done())
127103
})
128104

129-
await t.test('pkg has no install or preinstall script, node-gyp files present', async t => {
105+
/* eslint-disable-next-line max-len */
106+
await t.test('pkg has no install or preinstall script, node-gyp files present, stdio pipe', async t => {
130107
const testdir = t.testdir({
131108
'binding.gyp': 'exists',
132109
})
133110

134-
const logs = mockConsole(t)
135111
spawk.spawn('sh', a => a.includes('node-gyp rebuild'))
136112
await runScript({
137113
event: 'install',
@@ -146,11 +122,11 @@ t.test('run-script-pkg', async t => {
146122
scripts: {},
147123
},
148124
})
149-
t.strictSame(logs, [])
125+
t.strictSame(output, [])
150126
t.ok(spawk.done())
151127
})
152128

153-
t.test('pkg has no install or preinstall script, but gypfile:false', async t => {
129+
t.test('pkg has no install or preinstall script, but gypfile:false, stdio pipe', async t => {
154130
const testdir = t.testdir({
155131
'binding.gyp': 'exists',
156132
})
@@ -170,6 +146,7 @@ t.test('run-script-pkg', async t => {
170146
},
171147
},
172148
})
149+
t.strictSame(output, [])
173150
t.strictSame(res, { code: 0, signal: null })
174151
})
175152

@@ -190,7 +167,7 @@ t.test('run-script-pkg', async t => {
190167
t.ok(interceptor.calledWith.stdio[0].writableEnded, 'stdin was ended properly')
191168
})
192169

193-
await t.test('kill process when foreground process ends with signal', async t => {
170+
await t.test('kill process when foreground process ends with signal, stdio inherit', async t => {
194171
t.teardown(() => {
195172
process.kill = pkill
196173
})
@@ -219,14 +196,15 @@ t.test('run-script-pkg', async t => {
219196
},
220197
},
221198
}))
199+
t.strictSame(output, [['\n> [email protected] sleep\n> sleep 1000000\n']])
222200
t.ok(spawk.done())
223201
if (!isWindows) {
224202
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
225203
t.equal(pid, process.pid, 'process.kill got expected pid')
226204
}
227205
})
228206

229-
await t.test('kill process when foreground process ends with signal', async t => {
207+
await t.test('kill process when foreground process ends with signal, stdio inherit', async t => {
230208
t.teardown(() => {
231209
process.kill = pkill
232210
})
@@ -255,14 +233,15 @@ t.test('run-script-pkg', async t => {
255233
},
256234
},
257235
}))
236+
t.strictSame(output, [['\n> [email protected] sleep\n> sleep 1000000\n']])
258237
t.ok(spawk.done())
259238
if (!isWindows) {
260239
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
261240
t.equal(pid, process.pid, 'process.kill got expected pid')
262241
}
263242
})
264243

265-
t.test('rejects if process.kill fails to end process', async t => {
244+
t.test('rejects if process.kill fails to end process, stdio inherit', async t => {
266245
t.teardown(() => {
267246
process.kill = pkill
268247
})
@@ -290,6 +269,7 @@ t.test('run-script-pkg', async t => {
290269
},
291270
},
292271
}))
272+
t.strictSame(output, [['\n> [email protected] sleep\n> sleep 1000000\n']])
293273
t.ok(spawk.done())
294274
if (!isWindows) {
295275
t.equal(signal, 'SIGFOO', 'process.kill got expected signal')
@@ -314,6 +294,7 @@ t.test('run-script-pkg', async t => {
314294
},
315295
},
316296
}))
297+
t.strictSame(output, [])
317298
t.ok(spawk.done())
318299
})
319300
})

0 commit comments

Comments
 (0)