Skip to content

Commit 39e4da0

Browse files
committed
feat(libnpmexec)!: no longer accept output function
BREAKING CHANGE: libnpmexec now emits an output event on process instead of invoking the output function passed in
1 parent b8f8b41 commit 39e4da0

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

workspaces/libnpmexec/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ await libexec({
3535
- `localBin`: Location to the `node_modules/.bin` folder of the local project to start scanning for bin files **String**, defaults to `./node_modules/.bin`. **libexec** will walk up the directory structure looking for `node_modules/.bin` folders in parent folders that might satisfy the current `arg` and will use that bin if found.
3636
- `locationMsg`: Overrides "at location" message when entering interactive mode **String**
3737
- `globalBin`: Location to the global space bin folder, same as: `$(npm bin -g)` **String**, defaults to empty string.
38-
- `output`: A function to print output to **Function**
3938
- `packages`: A list of packages to be used (possibly fetch from the registry) **Array<String>**, defaults to `[]`
4039
- `path`: Location to where to read local project info (`package.json`) **String**, defaults to `.`
4140
- `runPath`: Location to where to execute the script **String**, defaults to `.`

workspaces/libnpmexec/lib/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const exec = async (opts) => {
8383
locationMsg = undefined,
8484
globalBin = '',
8585
globalPath,
86-
output,
8786
// dereference values because we manipulate it later
8887
packages: [...packages] = [],
8988
path = '.',
@@ -98,7 +97,6 @@ const exec = async (opts) => {
9897
call,
9998
flatOptions,
10099
locationMsg,
101-
output,
102100
path,
103101
binPaths,
104102
runPath,

workspaces/libnpmexec/lib/run-script.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const ciInfo = require('ci-info')
22
const runScript = require('@npmcli/run-script')
33
const readPackageJson = require('read-package-json-fast')
4-
const { log } = require('proc-log')
4+
const { log, output } = require('proc-log')
55
const noTTY = require('./no-tty.js')
66

77
const run = async ({
88
args,
99
call,
1010
flatOptions,
1111
locationMsg,
12-
output = () => {},
1312
path,
1413
binPaths,
1514
runPath,
@@ -37,7 +36,7 @@ const run = async ({
3736

3837
const { chalk } = flatOptions
3938

40-
output(`${
39+
output.standard(`${
4140
chalk.reset('\nEntering npm script environment')
4241
}${
4342
chalk.reset(locationMsg || ` at location:\n${chalk.dim(runPath)}`)
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
const t = require('tap')
22

33
const mockRunScript = async (t, mocks, { level = 0 } = {}) => {
4-
const runScript = t.mock('../lib/run-script.js', mocks)
4+
const mockedRunScript = t.mock('../lib/run-script.js', mocks)
55
const { Chalk } = await import('chalk')
6-
return (opts) => runScript({
6+
7+
const outputs = []
8+
const handleOutput = (_level, msg) => {
9+
if (_level === 'standard') {
10+
outputs.push(msg)
11+
}
12+
}
13+
process.on('output', handleOutput)
14+
t.teardown(() => process.off('output', handleOutput))
15+
16+
const logs = []
17+
const handleLog = (_level, title, msg) => {
18+
logs.push(`${_level} ${title} ${msg}`)
19+
}
20+
process.on('log', handleLog)
21+
t.teardown(() => process.off('log', handleLog))
22+
23+
const runScript = (opts) => mockedRunScript({
724
args: [],
825
call: '',
926
path: '',
@@ -14,6 +31,7 @@ const mockRunScript = async (t, mocks, { level = 0 } = {}) => {
1431
...opts,
1532
flatOptions: { chalk: new Chalk({ level }) },
1633
})
34+
return { runScript, outputs, logs }
1735
}
1836

1937
t.test('no package.json', async t => {
@@ -24,7 +42,7 @@ t.test('no package.json', async t => {
2442
name: 'pkg',
2543
}),
2644
})
27-
const runScript = await mockRunScript(t, {
45+
const { runScript } = await mockRunScript(t, {
2846
'ci-info': { isCI: false },
2947
'@npmcli/run-script': async () => {
3048
t.ok('should call run-script')
@@ -38,49 +56,41 @@ t.test('no package.json', async t => {
3856
t.test('colorized interactive mode msg', async t => {
3957
t.plan(2)
4058

41-
const runScript = await mockRunScript(t, {
59+
const { runScript, outputs } = await mockRunScript(t, {
4260
'ci-info': { isCI: false },
4361
'@npmcli/run-script': async () => {
4462
t.ok('should call run-script')
4563
},
4664
'../lib/no-tty.js': () => false,
4765
}, { level: 3 })
4866

49-
const OUTPUT = []
5067
await runScript({
51-
output: msg => {
52-
OUTPUT.push(msg)
53-
},
5468
runPath: '/foo/',
5569
})
56-
t.matchSnapshot(OUTPUT.join('\n'), 'should print colorized output')
70+
t.matchSnapshot(outputs.join('\n'), 'should print colorized output')
5771
})
5872

5973
t.test('no color interactive mode msg', async t => {
6074
t.plan(2)
6175

62-
const runScript = await mockRunScript(t, {
76+
const { runScript, outputs } = await mockRunScript(t, {
6377
'ci-info': { isCI: false },
6478
'@npmcli/run-script': async () => {
6579
t.ok('should call run-script')
6680
},
6781
'../lib/no-tty.js': () => false,
6882
})
6983

70-
const OUTPUT = []
7184
await runScript({
72-
output: msg => {
73-
OUTPUT.push(msg)
74-
},
7585
runPath: '/foo/',
7686
})
77-
t.matchSnapshot(OUTPUT.join('\n'), 'should print non-colorized output')
87+
t.matchSnapshot(outputs.join('\n'), 'should print non-colorized output')
7888
})
7989

8090
t.test('no tty', async t => {
8191
t.plan(1)
8292

83-
const runScript = await mockRunScript(t, {
93+
const { runScript } = await mockRunScript(t, {
8494
'ci-info': { isCI: false },
8595
'@npmcli/run-script': async () => {
8696
t.ok('should call run-script')
@@ -92,27 +102,16 @@ t.test('no tty', async t => {
92102
})
93103

94104
t.test('ci env', async t => {
95-
t.plan(2)
96-
97-
const runScript = await mockRunScript(t, {
105+
const { runScript, logs } = await mockRunScript(t, {
98106
'ci-info': { isCI: true },
99107
'@npmcli/run-script': async () => {
100108
throw new Error('should not call run-script')
101109
},
102110
'../lib/no-tty.js': () => false,
103-
'proc-log': {
104-
log: {
105-
warn (title, msg) {
106-
t.equal(title, 'exec', 'should have expected title')
107-
t.equal(
108-
msg,
109-
'Interactive mode disabled in CI environment',
110-
'should have expected ci environment message'
111-
)
112-
},
113-
},
114-
},
111+
115112
})
116113

117114
await runScript()
115+
116+
t.equal(logs[0], 'warn exec Interactive mode disabled in CI environment')
118117
})

0 commit comments

Comments
 (0)