Skip to content

Commit 9b7ad2a

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 69610b9 commit 9b7ad2a

File tree

2 files changed

+47
-59
lines changed

2 files changed

+47
-59
lines changed

Diff for: lib/run-script-pkg.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@ const packageEnvs = require('./package-envs.js')
44
const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp')
55
const signalManager = require('./signal-manager.js')
66
const isServerPackage = require('./is-server-package.js')
7-
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-
}
7+
const { output } = require('proc-log')
208

219
const runScriptPkg = async options => {
2210
const {
@@ -29,8 +17,6 @@ const runScriptPkg = async options => {
2917
pkg,
3018
args = [],
3119
stdioString,
32-
// note: only used when stdio:inherit
33-
banner = true,
3420
// how long to wait for a process.kill signal
3521
// only exposed here so that we can make the test go a bit faster.
3622
signalTimeout = 500,
@@ -59,10 +45,17 @@ const runScriptPkg = async options => {
5945
return { code: 0, signal: null }
6046
}
6147

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))
48+
// consuming library decides if this is displayed or not
49+
output.standard('')
50+
if (pkg._id) {
51+
output.standard(`> ${pkg._id} ${event}`)
52+
} else {
53+
output.standard(`> ${event}`)
54+
}
55+
if (args.length) {
56+
output.standard(`> ${cmd.trim().replace(/\n/g, '\n> ')} ${args.join(' ')}`)
6557
}
58+
output.standard('')
6659

6760
const [spawnShell, spawnArgs, spawnOpts] = makeSpawnArgs({
6861
event,

Diff for: test/run-script-pkg.js

+36-41
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ 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
16-
}
10+
const output = []
11+
process.on('output', (level, ...args) => {
12+
if (level === 'standard') {
13+
output.push([...args])
14+
}
15+
})
16+
t.afterEach(() => output.length = 0)
1717

1818
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)
19+
await t.test('output with no args and a pkgid', async t => {
2120
spawk.spawn('sh', a => a.includes('bar\nbaz\n'))
2221
await runScript({
2322
event: 'foo',
@@ -33,34 +32,15 @@ t.test('run-script-pkg', async t => {
3332
scripts: {},
3433
},
3534
})
36-
t.strictSame(logs, [['\n> [email protected] foo\n> bar\n> baz\n']])
37-
t.ok(spawk.done())
38-
})
39-
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, [])
35+
t.strictSame(output, [
36+
[''],
37+
['> [email protected] foo'],
38+
[''],
39+
])
5940
t.ok(spawk.done())
6041
})
6142

62-
await t.test('do the banner with no pkgid', async t => {
63-
const logs = mockConsole(t)
43+
await t.test('output with args and no pkgid', async t => {
6444
spawk.spawn('sh', a => a.includes('bar baz buzz'))
6545
await runScript({
6646
event: 'foo',
@@ -76,12 +56,16 @@ t.test('run-script-pkg', async t => {
7656
scripts: {},
7757
},
7858
})
79-
t.strictSame(logs, [['\n> foo\n> bar baz buzz\n']])
59+
t.strictSame(output, [
60+
[''],
61+
['> foo'],
62+
['> bar baz buzz'],
63+
[''],
64+
])
8065
t.ok(spawk.done())
8166
})
8267

8368
await t.test('pkg has foo script', async t => {
84-
const logs = mockConsole(t)
8569
spawk.spawn('sh', a => a.includes('bar'))
8670
await runScript({
8771
event: 'foo',
@@ -98,12 +82,15 @@ t.test('run-script-pkg', async t => {
9882
},
9983
},
10084
})
101-
t.strictSame(logs, [])
85+
t.strictSame(output, [
86+
[''],
87+
['> [email protected] foo'],
88+
[''],
89+
])
10290
t.ok(spawk.done())
10391
})
10492

10593
await t.test('pkg has foo script, with args', async t => {
106-
const logs = mockConsole(t)
10794
spawk.spawn('sh', a => a.includes('bar a b c'))
10895
await runScript({
10996
event: 'foo',
@@ -122,7 +109,12 @@ t.test('run-script-pkg', async t => {
122109
args: ['a', 'b', 'c'],
123110
binPaths: false,
124111
})
125-
t.strictSame(logs, [])
112+
t.strictSame(output, [
113+
[''],
114+
['> [email protected] foo'],
115+
['> bar a b c'],
116+
[''],
117+
])
126118
t.ok(spawk.done())
127119
})
128120

@@ -131,7 +123,6 @@ t.test('run-script-pkg', async t => {
131123
'binding.gyp': 'exists',
132124
})
133125

134-
const logs = mockConsole(t)
135126
spawk.spawn('sh', a => a.includes('node-gyp rebuild'))
136127
await runScript({
137128
event: 'install',
@@ -146,7 +137,11 @@ t.test('run-script-pkg', async t => {
146137
scripts: {},
147138
},
148139
})
149-
t.strictSame(logs, [])
140+
t.strictSame(output, [
141+
[''],
142+
['> [email protected] install'],
143+
[''],
144+
])
150145
t.ok(spawk.done())
151146
})
152147

0 commit comments

Comments
 (0)