Skip to content

Commit fb8f731

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 fb8f731

File tree

2 files changed

+26
-58
lines changed

2 files changed

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

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

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

+15-40
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('output with 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']])
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('output with 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

8361
await t.test('pkg has foo script', async t => {
84-
const logs = mockConsole(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, [['\n> [email protected] foo\n']])
10279
t.ok(spawk.done())
10380
})
10481

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

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

134-
const logs = mockConsole(t)
135110
spawk.spawn('sh', a => a.includes('node-gyp rebuild'))
136111
await runScript({
137112
event: 'install',
@@ -146,7 +121,7 @@ t.test('run-script-pkg', async t => {
146121
scripts: {},
147122
},
148123
})
149-
t.strictSame(logs, [])
124+
t.strictSame(output, [['\n> [email protected] install\n']])
150125
t.ok(spawk.done())
151126
})
152127

0 commit comments

Comments
 (0)