Skip to content

Commit b1f8f3d

Browse files
committed
fix(refactor): use output.buffer for easier json output in run-script
1 parent 7fdd1e5 commit b1f8f3d

File tree

4 files changed

+95
-135
lines changed

4 files changed

+95
-135
lines changed

lib/commands/run-script.js

Lines changed: 51 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ const { log, output } = require('proc-log')
22
const pkgJson = require('@npmcli/package-json')
33
const BaseCommand = require('../base-cmd.js')
44

5+
// TODO this is missing things like prepare, prepublishOnly, and dependencies
6+
const cmdList = [
7+
'preinstall', 'install', 'postinstall',
8+
'prepublish', 'publish', 'postpublish',
9+
'prerestart', 'restart', 'postrestart',
10+
'prestart', 'start', 'poststart',
11+
'prestop', 'stop', 'poststop',
12+
'pretest', 'test', 'posttest',
13+
'preuninstall', 'uninstall', 'postuninstall',
14+
'preversion', 'version', 'postversion',
15+
]
16+
517
class RunScript extends BaseCommand {
618
static description = 'Run arbitrary package scripts'
719
static params = [
@@ -34,27 +46,39 @@ class RunScript extends BaseCommand {
3446

3547
async exec (args) {
3648
if (args.length) {
37-
return this.run(args)
49+
await this.#run(args, { path: this.npm.localPrefix })
3850
} else {
39-
return this.list(args)
51+
await this.#list(this.npm.localPrefix)
4052
}
4153
}
4254

4355
async execWorkspaces (args) {
44-
if (args.length) {
45-
return this.runWorkspaces(args)
46-
} else {
47-
return this.listWorkspaces(args)
56+
await this.setWorkspaces()
57+
58+
for (const [name, path] of this.workspaces.entries()) {
59+
if (!args.length) {
60+
await this.#list(path, { workspace: name })
61+
continue
62+
}
63+
64+
const pkg = await pkgJson.normalize(path).then(p => p.content)
65+
try {
66+
await this.#run(args, { path, pkg })
67+
} 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
73+
}
4874
}
4975
}
5076

51-
async run ([event, ...args], { path = this.npm.localPrefix, pkg } = {}) {
77+
async #run ([event, ...args], { path, pkg } = {}) {
5278
const runScript = require('@npmcli/run-script')
5379

54-
if (!pkg) {
55-
const { content } = await pkgJson.normalize(path)
56-
pkg = content
57-
}
80+
pkg ??= await pkgJson.normalize(path).then(p => p.content)
81+
5882
const { scripts = {} } = pkg
5983

6084
if (event === 'restart' && !scripts.restart) {
@@ -109,49 +133,39 @@ class RunScript extends BaseCommand {
109133
}
110134
}
111135

112-
async list (args, path) {
113-
/* eslint-disable-next-line max-len */
114-
const { content: { scripts, name, _id } } = await pkgJson.normalize(path || this.npm.localPrefix)
136+
async #list (path, { workspace } = {}) {
137+
const { scripts = {}, name, _id } = await pkgJson.normalize(path).then(p => p.content)
115138
const pkgid = _id || name
116139

117-
if (!scripts) {
118-
return []
119-
}
120-
121-
const allScripts = Object.keys(scripts)
122140
if (this.npm.silent) {
123-
return allScripts
141+
return
124142
}
125143

126144
if (this.npm.config.get('json')) {
127-
output.standard(JSON.stringify(scripts, null, 2))
128-
return allScripts
145+
output.buffer(workspace ? { [workspace]: scripts } : scripts)
146+
return
129147
}
130148

131149
if (this.npm.config.get('parseable')) {
132-
for (const [script, cmd] of Object.entries(scripts)) {
133-
output.standard(`${script}:${cmd}`)
150+
const msg = Object.entries(scripts)
151+
.map(([script, cmd]) => `${workspace ? `${workspace}:` : ''}${script}:${cmd}`)
152+
.join('\n')
153+
.trim()
154+
if (msg) {
155+
output.standard(msg)
134156
}
157+
return
158+
}
135159

136-
return allScripts
160+
if (!Object.keys(scripts).length) {
161+
return
137162
}
138163

139-
// TODO this is missing things like prepare, prepublishOnly, and dependencies
140-
const cmdList = [
141-
'preinstall', 'install', 'postinstall',
142-
'prepublish', 'publish', 'postpublish',
143-
'prerestart', 'restart', 'postrestart',
144-
'prestart', 'start', 'poststart',
145-
'prestop', 'stop', 'poststop',
146-
'pretest', 'test', 'posttest',
147-
'preuninstall', 'uninstall', 'postuninstall',
148-
'preversion', 'version', 'postversion',
149-
]
150164
const indent = '\n '
151165
const prefix = ' '
152166
const cmds = []
153167
const runScripts = []
154-
for (const script of allScripts) {
168+
for (const script of Object.keys(scripts)) {
155169
const list = cmdList.includes(script) ? cmds : runScripts
156170
list.push(script)
157171
}
@@ -184,59 +198,6 @@ class RunScript extends BaseCommand {
184198
}
185199

186200
output.standard('')
187-
return allScripts
188-
}
189-
190-
async runWorkspaces (args) {
191-
const res = []
192-
await this.setWorkspaces()
193-
194-
for (const workspacePath of this.workspacePaths) {
195-
const { content: pkg } = await pkgJson.normalize(workspacePath)
196-
const runResult = await this.run(args, {
197-
path: workspacePath,
198-
pkg,
199-
}).catch(err => {
200-
log.error(`Lifecycle script \`${args[0]}\` failed with error:`)
201-
log.error(err)
202-
log.error(` in workspace: ${pkg._id || pkg.name}`)
203-
log.error(` at location: ${workspacePath}`)
204-
process.exitCode = 1
205-
})
206-
res.push(runResult)
207-
}
208-
}
209-
210-
async listWorkspaces (args) {
211-
await this.setWorkspaces()
212-
213-
if (this.npm.silent) {
214-
return
215-
}
216-
217-
if (this.npm.config.get('json')) {
218-
const res = {}
219-
for (const workspacePath of this.workspacePaths) {
220-
const { content: { scripts, name } } = await pkgJson.normalize(workspacePath)
221-
res[name] = { ...scripts }
222-
}
223-
output.standard(JSON.stringify(res, null, 2))
224-
return
225-
}
226-
227-
if (this.npm.config.get('parseable')) {
228-
for (const workspacePath of this.workspacePaths) {
229-
const { content: { scripts, name } } = await pkgJson.normalize(workspacePath)
230-
for (const [script, cmd] of Object.entries(scripts || {})) {
231-
output.standard(`${name}:${script}:${cmd}`)
232-
}
233-
}
234-
return
235-
}
236-
237-
for (const workspacePath of this.workspacePaths) {
238-
await this.list(args, workspacePath)
239-
}
240201
}
241202
}
242203

mock-registry/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"template-oss-apply": "template-oss-apply --force",
1212
"lintfix": "npm run lint -- --fix",
1313
"snap": "tap",
14-
"posttest": "npm run lint"
14+
"posttest": "npm run lint",
15+
"exit1": "exit 1"
1516
},
1617
"repository": {
1718
"type": "git",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@
228228
"resetdeps": "node scripts/resetdeps.js",
229229
"rp-pull-request": "node scripts/update-authors.js",
230230
"postlint": "template-oss-check",
231-
"template-oss-apply": "template-oss-apply --force"
231+
"template-oss-apply": "template-oss-apply --force",
232+
"exit1": "exit 1"
232233
},
233234
"tap": {
234235
"test-env": [

0 commit comments

Comments
 (0)