Skip to content

Commit 5363c9f

Browse files
committed
update run-script snapshot tests
1 parent 0387c9d commit 5363c9f

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

lib/commands/run-script.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const { log, output } = require('proc-log')
1+
const { output } = require('proc-log')
22
const pkgJson = require('@npmcli/package-json')
33
const BaseCommand = require('../base-cmd.js')
4+
const { outputError } = require('../utils/output-error.js')
5+
const { getError } = require('../utils/error-message.js')
46

57
// TODO this is missing things like prepare, prepublishOnly, and dependencies
68
const cmdList = [
@@ -55,21 +57,33 @@ class RunScript extends BaseCommand {
5557
async execWorkspaces (args) {
5658
await this.setWorkspaces()
5759

58-
for (const [name, path] of this.workspaces.entries()) {
60+
const ws = [...this.workspaces.entries()]
61+
for (const [name, path] of ws) {
62+
const last = name === ws.at(-1)[0]
5963
if (!args.length) {
60-
await this.#list(path, { workspace: name })
64+
await this.#list(path, { workspace: name, newline: !last })
6165
continue
6266
}
6367

6468
const pkg = await pkgJson.normalize(path).then(p => p.content)
6569
try {
6670
await this.#run(args, { path, pkg })
6771
} catch (err) {
68-
log.error(`Lifecycle script \`${args[0]}\` failed with error:`)
69-
log.error(err)
70-
log.error(` in workspace: ${pkg._id || pkg.name}`)
71-
log.error(` at location: ${path}`)
72-
process.exitCode = 1
72+
// Set command to null so that we get the full error parsing
73+
// run-script will supress error messages due to isShellout
74+
const error = getError(err, { npm: this.npm, command: null })
75+
outputError({
76+
...error,
77+
error: [
78+
['', `Lifecycle script \`${args[0]}\` failed with error:`],
79+
['workspace', pkg._id || pkg.name],
80+
...error.error,
81+
],
82+
})
83+
if (!last) {
84+
output.error('')
85+
}
86+
process.exitCode = error.exitCode
7387
}
7488
}
7589
}
@@ -133,7 +147,7 @@ class RunScript extends BaseCommand {
133147
}
134148
}
135149

136-
async #list (path, { workspace } = {}) {
150+
async #list (path, { workspace, newline } = {}) {
137151
const { scripts = {}, name, _id } = await pkgJson.normalize(path).then(p => p.content)
138152
const scriptEntries = Object.entries(scripts)
139153

@@ -189,7 +203,9 @@ class RunScript extends BaseCommand {
189203
}
190204
}
191205

192-
output.standard('')
206+
if (newline) {
207+
output.standard('')
208+
}
193209
}
194210
}
195211

tap-snapshots/test/lib/commands/run-script.js.test.cjs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ postenv:echo after the env
3131

3232
exports[`test/lib/commands/run-script.js TAP list scripts warn json > json report 1`] = `
3333
{
34-
test: 'exit 2',
35-
start: 'node server.js',
36-
stop: 'node kill-server.js',
37-
preenv: 'echo before the env',
38-
postenv: 'echo after the env'
34+
"test": "exit 2",
35+
"start": "node server.js",
36+
"stop": "node kill-server.js",
37+
"preenv": "echo before the env",
38+
"postenv": "echo after the env"
3939
}
4040
`
4141

@@ -53,20 +53,34 @@ Scripts available in x@1.2.3 via \`npm run-script\`:
5353

5454
exports[`test/lib/commands/run-script.js TAP workspaces failed workspace run with succeeded runs > should log error msgs for each workspace script 1`] = `
5555
Lifecycle script \`glorp\` failed with error:
56-
Error: ERR
57-
in workspace: a@1.0.0
58-
at location: {CWD}/prefix/packages/a
56+
workspace a@1.0.0
57+
code ERR
58+
ERR
5959
`
6060

6161
exports[`test/lib/commands/run-script.js TAP workspaces list all scripts --json > must match snapshot 1`] = `
62-
{ a: { glorp: 'echo a doing the glerp glop' } }
63-
{ b: { glorp: 'echo b doing the glerp glop' } }
6462
{
65-
c: { test: 'exit 0', posttest: 'echo posttest', lorem: 'echo c lorem' }
63+
"a": {
64+
"glorp": "echo a doing the glerp glop"
65+
},
66+
"b": {
67+
"glorp": "echo b doing the glerp glop"
68+
},
69+
"c": {
70+
"test": "exit 0",
71+
"posttest": "echo posttest",
72+
"lorem": "echo c lorem"
73+
},
74+
"d": {
75+
"test": "exit 0",
76+
"posttest": "echo posttest"
77+
},
78+
"e": {
79+
"test": "exit 0",
80+
"start": "echo start something"
81+
},
82+
"noscripts": {}
6683
}
67-
{ d: { test: 'exit 0', posttest: 'echo posttest' } }
68-
{ e: { test: 'exit 0', start: 'echo start something' } }
69-
{ noscripts: {} }
7084
`
7185

7286
exports[`test/lib/commands/run-script.js TAP workspaces list all scripts --parseable > must match snapshot 1`] = `
@@ -195,69 +209,60 @@ Scripts available in a@1.0.0 via \`npm run-script\`:
195209

196210
exports[`test/lib/commands/run-script.js TAP workspaces missing scripts in all workspaces > should log error msgs for each workspace script 1`] = `
197211
Lifecycle script \`missing-script\` failed with error:
198-
Error: Missing script: "missing-script"
199-
212+
workspace a@1.0.0
213+
Missing script: "missing-script"
214+
npm error
200215
To see a list of scripts, run:
201216
npm run
202-
in workspace: a@1.0.0
203-
at location: {CWD}/prefix/packages/a
204217
Lifecycle script \`missing-script\` failed with error:
205-
Error: Missing script: "missing-script"
206-
218+
workspace b@2.0.0
219+
Missing script: "missing-script"
220+
npm error
207221
To see a list of scripts, run:
208222
npm run
209-
in workspace: b@2.0.0
210-
at location: {CWD}/prefix/packages/b
211223
Lifecycle script \`missing-script\` failed with error:
212-
Error: Missing script: "missing-script"
213-
224+
workspace c@1.0.0
225+
Missing script: "missing-script"
226+
npm error
214227
To see a list of scripts, run:
215228
npm run
216-
in workspace: c@1.0.0
217-
at location: {CWD}/prefix/packages/c
218229
Lifecycle script \`missing-script\` failed with error:
219-
Error: Missing script: "missing-script"
220-
230+
workspace d@1.0.0
231+
Missing script: "missing-script"
232+
npm error
221233
To see a list of scripts, run:
222234
npm run
223-
in workspace: d@1.0.0
224-
at location: {CWD}/prefix/packages/d
225235
Lifecycle script \`missing-script\` failed with error:
226-
Error: Missing script: "missing-script"
227-
236+
workspace e
237+
Missing script: "missing-script"
238+
npm error
228239
To see a list of scripts, run:
229240
npm run
230-
in workspace: e
231-
at location: {CWD}/prefix/packages/e
232241
Lifecycle script \`missing-script\` failed with error:
233-
Error: Missing script: "missing-script"
234-
242+
workspace noscripts@1.0.0
243+
Missing script: "missing-script"
244+
npm error
235245
To see a list of scripts, run:
236246
npm run
237-
in workspace: noscripts@1.0.0
238-
at location: {CWD}/prefix/packages/noscripts
239247
`
240248

241249
exports[`test/lib/commands/run-script.js TAP workspaces missing scripts in some workspaces > should log error msgs for each workspace script 1`] = `
242250
Lifecycle script \`test\` failed with error:
243-
Error: Missing script: "test"
244-
251+
workspace a@1.0.0
252+
Missing script: "test"
253+
npm error
245254
To see a list of scripts, run:
246255
npm run
247-
in workspace: a@1.0.0
248-
at location: {CWD}/prefix/packages/a
249256
Lifecycle script \`test\` failed with error:
250-
Error: Missing script: "test"
251-
257+
workspace b@2.0.0
258+
Missing script: "test"
259+
npm error
252260
To see a list of scripts, run:
253261
npm run
254-
in workspace: b@2.0.0
255-
at location: {CWD}/prefix/packages/b
256262
`
257263

258264
exports[`test/lib/commands/run-script.js TAP workspaces single failed workspace run > should log error msgs for each workspace script 1`] = `
259265
Lifecycle script \`test\` failed with error:
260-
Error: err
261-
in workspace: c@1.0.0
262-
at location: {CWD}/prefix/packages/c
266+
workspace c@1.0.0
267+
err
263268
`

0 commit comments

Comments
 (0)