Skip to content

Commit

Permalink
feat(libnpmexec)!: no longer accept output function
Browse files Browse the repository at this point in the history
BREAKING CHANGE: libnpmexec now emits an output event on process
instead of invoking the output function passed in
  • Loading branch information
lukekarrys committed Apr 16, 2024
1 parent b8f8b41 commit 39e4da0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
1 change: 0 additions & 1 deletion workspaces/libnpmexec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ await libexec({
- `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.
- `locationMsg`: Overrides "at location" message when entering interactive mode **String**
- `globalBin`: Location to the global space bin folder, same as: `$(npm bin -g)` **String**, defaults to empty string.
- `output`: A function to print output to **Function**
- `packages`: A list of packages to be used (possibly fetch from the registry) **Array<String>**, defaults to `[]`
- `path`: Location to where to read local project info (`package.json`) **String**, defaults to `.`
- `runPath`: Location to where to execute the script **String**, defaults to `.`
Expand Down
2 changes: 0 additions & 2 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const exec = async (opts) => {
locationMsg = undefined,
globalBin = '',
globalPath,
output,
// dereference values because we manipulate it later
packages: [...packages] = [],
path = '.',
Expand All @@ -98,7 +97,6 @@ const exec = async (opts) => {
call,
flatOptions,
locationMsg,
output,
path,
binPaths,
runPath,
Expand Down
5 changes: 2 additions & 3 deletions workspaces/libnpmexec/lib/run-script.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const ciInfo = require('ci-info')
const runScript = require('@npmcli/run-script')
const readPackageJson = require('read-package-json-fast')
const { log } = require('proc-log')
const { log, output } = require('proc-log')
const noTTY = require('./no-tty.js')

const run = async ({
args,
call,
flatOptions,
locationMsg,
output = () => {},
path,
binPaths,
runPath,
Expand Down Expand Up @@ -37,7 +36,7 @@ const run = async ({

const { chalk } = flatOptions

output(`${
output.standard(`${
chalk.reset('\nEntering npm script environment')
}${
chalk.reset(locationMsg || ` at location:\n${chalk.dim(runPath)}`)
Expand Down
61 changes: 30 additions & 31 deletions workspaces/libnpmexec/test/run-script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
const t = require('tap')

const mockRunScript = async (t, mocks, { level = 0 } = {}) => {
const runScript = t.mock('../lib/run-script.js', mocks)
const mockedRunScript = t.mock('../lib/run-script.js', mocks)
const { Chalk } = await import('chalk')
return (opts) => runScript({

const outputs = []
const handleOutput = (_level, msg) => {
if (_level === 'standard') {
outputs.push(msg)
}
}
process.on('output', handleOutput)
t.teardown(() => process.off('output', handleOutput))

const logs = []
const handleLog = (_level, title, msg) => {
logs.push(`${_level} ${title} ${msg}`)
}
process.on('log', handleLog)
t.teardown(() => process.off('log', handleLog))

const runScript = (opts) => mockedRunScript({
args: [],
call: '',
path: '',
Expand All @@ -14,6 +31,7 @@ const mockRunScript = async (t, mocks, { level = 0 } = {}) => {
...opts,
flatOptions: { chalk: new Chalk({ level }) },
})
return { runScript, outputs, logs }
}

t.test('no package.json', async t => {
Expand All @@ -24,7 +42,7 @@ t.test('no package.json', async t => {
name: 'pkg',
}),
})
const runScript = await mockRunScript(t, {
const { runScript } = await mockRunScript(t, {
'ci-info': { isCI: false },
'@npmcli/run-script': async () => {
t.ok('should call run-script')
Expand All @@ -38,49 +56,41 @@ t.test('no package.json', async t => {
t.test('colorized interactive mode msg', async t => {
t.plan(2)

const runScript = await mockRunScript(t, {
const { runScript, outputs } = await mockRunScript(t, {
'ci-info': { isCI: false },
'@npmcli/run-script': async () => {
t.ok('should call run-script')
},
'../lib/no-tty.js': () => false,
}, { level: 3 })

const OUTPUT = []
await runScript({
output: msg => {
OUTPUT.push(msg)
},
runPath: '/foo/',
})
t.matchSnapshot(OUTPUT.join('\n'), 'should print colorized output')
t.matchSnapshot(outputs.join('\n'), 'should print colorized output')
})

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

const runScript = await mockRunScript(t, {
const { runScript, outputs } = await mockRunScript(t, {
'ci-info': { isCI: false },
'@npmcli/run-script': async () => {
t.ok('should call run-script')
},
'../lib/no-tty.js': () => false,
})

const OUTPUT = []
await runScript({
output: msg => {
OUTPUT.push(msg)
},
runPath: '/foo/',
})
t.matchSnapshot(OUTPUT.join('\n'), 'should print non-colorized output')
t.matchSnapshot(outputs.join('\n'), 'should print non-colorized output')
})

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

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

t.test('ci env', async t => {
t.plan(2)

const runScript = await mockRunScript(t, {
const { runScript, logs } = await mockRunScript(t, {
'ci-info': { isCI: true },
'@npmcli/run-script': async () => {
throw new Error('should not call run-script')
},
'../lib/no-tty.js': () => false,
'proc-log': {
log: {
warn (title, msg) {
t.equal(title, 'exec', 'should have expected title')
t.equal(
msg,
'Interactive mode disabled in CI environment',
'should have expected ci environment message'
)
},
},
},

})

await runScript()

t.equal(logs[0], 'warn exec Interactive mode disabled in CI environment')
})

0 comments on commit 39e4da0

Please sign in to comment.