Skip to content

Commit 61d5771

Browse files
authored
fix: remove json.stringify from all commands (#7541)
This converts all remaining commands/utils to use the display layer for formatting their json output
1 parent 4dfc7d2 commit 61d5771

File tree

22 files changed

+95
-106
lines changed

22 files changed

+95
-106
lines changed

lib/commands/access.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Access extends BaseCommand {
208208
outputs[item] = lookup[val] || val
209209
}
210210
if (this.npm.config.get('json')) {
211-
output.standard(JSON.stringify(outputs, null, 2))
211+
output.buffer(outputs)
212212
} else {
213213
for (const item of Object.keys(outputs).sort(localeCompare)) {
214214
if (!limiter || limiter === item) {

lib/commands/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ ${defData}
403403

404404
publicConf[key] = value
405405
}
406-
output.standard(JSON.stringify(publicConf, null, 2))
406+
output.buffer(publicConf)
407407
}
408408
}
409409

lib/commands/explain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Explain extends ArboristWorkspaceCmd {
7676
}
7777

7878
if (this.npm.flatOptions.json) {
79-
output.standard(JSON.stringify(expls, null, 2))
79+
output.buffer(expls)
8080
} else {
8181
output.standard(expls.map(expl => {
8282
return explainNode(expl, Infinity, this.npm.chalk)

lib/commands/fund.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,12 @@ class Fund extends ArboristWorkspaceCmd {
8585
})
8686

8787
if (this.npm.config.get('json')) {
88-
output.standard(this.printJSON(fundingInfo))
88+
output.buffer(fundingInfo)
8989
} else {
9090
output.standard(this.printHuman(fundingInfo))
9191
}
9292
}
9393

94-
printJSON (fundingInfo) {
95-
return JSON.stringify(fundingInfo, null, 2)
96-
}
97-
9894
printHuman (fundingInfo) {
9995
const unicode = this.npm.config.get('unicode')
10096
const seenUrls = new Map()

lib/commands/hook.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Hook extends BaseCommand {
4040
async add (pkg, uri, secret, opts) {
4141
const hook = await hookApi.add(pkg, uri, secret, opts)
4242
if (opts.json) {
43-
output.standard(JSON.stringify(hook, null, 2))
43+
output.buffer(hook)
4444
} else if (opts.parseable) {
4545
output.standard(Object.keys(hook).join('\t'))
4646
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))
@@ -53,7 +53,7 @@ class Hook extends BaseCommand {
5353
const hooks = await hookApi.ls({ ...opts, package: pkg })
5454

5555
if (opts.json) {
56-
output.standard(JSON.stringify(hooks, null, 2))
56+
output.buffer(hooks)
5757
} else if (opts.parseable) {
5858
output.standard(Object.keys(hooks[0]).join('\t'))
5959
hooks.forEach(hook => {
@@ -80,7 +80,7 @@ class Hook extends BaseCommand {
8080
async rm (id, opts) {
8181
const hook = await hookApi.rm(id, opts)
8282
if (opts.json) {
83-
output.standard(JSON.stringify(hook, null, 2))
83+
output.buffer(hook)
8484
} else if (opts.parseable) {
8585
output.standard(Object.keys(hook).join('\t'))
8686
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))
@@ -92,7 +92,7 @@ class Hook extends BaseCommand {
9292
async update (id, uri, secret, opts) {
9393
const hook = await hookApi.update(id, uri, secret, opts)
9494
if (opts.json) {
95-
output.standard(JSON.stringify(hook, null, 2))
95+
output.buffer(hook)
9696
} else if (opts.parseable) {
9797
output.standard(Object.keys(hook).join('\t'))
9898
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))

lib/commands/org.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,12 @@ class Org extends BaseCommand {
100100
org = org.replace(/^[~@]?/, '')
101101
const userCount = Object.keys(roster).length
102102
if (opts.json) {
103-
output.standard(
104-
JSON.stringify({
105-
user,
106-
org,
107-
userCount,
108-
deleted: true,
109-
})
110-
)
103+
output.buffer({
104+
user,
105+
org,
106+
userCount,
107+
deleted: true,
108+
})
111109
} else if (opts.parseable) {
112110
output.standard(['user', 'org', 'userCount', 'deleted'].join('\t'))
113111
output.standard([user, org, userCount, true].join('\t'))
@@ -135,7 +133,7 @@ class Org extends BaseCommand {
135133
roster = newRoster
136134
}
137135
if (opts.json) {
138-
output.standard(JSON.stringify(roster, null, 2))
136+
output.buffer(roster)
139137
} else if (opts.parseable) {
140138
output.standard(['user', 'role'].join('\t'))
141139
Object.keys(roster).forEach(u => {

lib/commands/ping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class Ping extends BaseCommand {
1616
const time = Date.now() - start
1717
log.notice('PONG', `${time}ms`)
1818
if (this.npm.config.get('json')) {
19-
output.standard(JSON.stringify({
19+
output.buffer({
2020
registry: cleanRegistry,
2121
time,
2222
details,
23-
}, null, 2))
23+
})
2424
} else if (Object.keys(details).length) {
2525
log.notice('PONG', JSON.stringify(details, null, 2))
2626
}

lib/commands/profile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Profile extends BaseCommand {
108108
}
109109

110110
if (this.npm.config.get('json')) {
111-
output.standard(JSON.stringify(info, null, 2))
111+
output.buffer(info)
112112
return
113113
}
114114

@@ -211,7 +211,7 @@ class Profile extends BaseCommand {
211211
const result = await otplease(this.npm, conf, c => set(newUser, c))
212212

213213
if (this.npm.config.get('json')) {
214-
output.standard(JSON.stringify({ [prop]: result[prop] }, null, 2))
214+
output.buffer({ [prop]: result[prop] })
215215
} else if (this.npm.config.get('parseable')) {
216216
output.standard(prop + '\t' + result[prop])
217217
} else if (result[prop] != null) {
@@ -378,7 +378,7 @@ class Profile extends BaseCommand {
378378
await set({ tfa: { password: password, mode: 'disable' } }, conf)
379379

380380
if (this.npm.config.get('json')) {
381-
output.standard(JSON.stringify({ tfa: false }, null, 2))
381+
output.buffer({ tfa: false })
382382
} else if (this.npm.config.get('parseable')) {
383383
output.standard('tfa\tfalse')
384384
} else {

lib/commands/sbom.js

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class SBOM extends BaseCommand {
2222
'workspaces',
2323
]
2424

25-
get #parsedResponse () {
26-
return JSON.stringify(this.#response, null, 2)
27-
}
28-
2925
async exec () {
3026
const sbomFormat = this.npm.config.get('sbom-format')
3127
const packageLockOnly = this.npm.config.get('package-lock-only')
@@ -35,56 +31,43 @@ class SBOM extends BaseCommand {
3531
throw this.usageError(`Must specify --sbom-format flag with one of: ${SBOM_FORMATS.join(', ')}.`)
3632
}
3733

38-
const Arborist = require('@npmcli/arborist')
39-
4034
const opts = {
4135
...this.npm.flatOptions,
4236
path: this.npm.prefix,
4337
forceActual: true,
4438
}
39+
const Arborist = require('@npmcli/arborist')
4540
const arb = new Arborist(opts)
4641

47-
let tree
48-
if (packageLockOnly) {
49-
try {
50-
tree = await arb.loadVirtual(opts)
51-
} catch (err) {
52-
/* eslint-disable-next-line max-len */
53-
throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode')
54-
}
55-
} else {
56-
tree = await arb.loadActual(opts)
57-
}
42+
const tree = packageLockOnly ? await arb.loadVirtual(opts).catch(() => {
43+
/* eslint-disable-next-line max-len */
44+
throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode')
45+
}) : await arb.loadActual(opts)
5846

5947
// Collect the list of selected workspaces in the project
60-
let wsNodes
61-
if (this.workspaceNames && this.workspaceNames.length) {
62-
wsNodes = arb.workspaceNodes(tree, this.workspaceNames)
63-
}
48+
const wsNodes = this.workspaceNames?.length
49+
? arb.workspaceNodes(tree, this.workspaceNames)
50+
: null
6451

6552
// Build the selector and query the tree for the list of nodes
6653
const selector = this.#buildSelector({ wsNodes })
6754
log.info('sbom', `Using dependency selector: ${selector}`)
6855
const items = await tree.querySelectorAll(selector)
6956

70-
const errors = new Set()
71-
for (const node of items) {
72-
detectErrors(node).forEach(error => errors.add(error))
73-
}
74-
75-
if (errors.size > 0) {
76-
throw Object.assign(
77-
new Error([...errors].join('\n')),
78-
{ code: 'ESBOMPROBLEMS' }
79-
)
57+
const errors = items.flatMap(node => detectErrors(node))
58+
if (errors.length) {
59+
throw Object.assign(new Error([...new Set(errors)].join('\n')), {
60+
code: 'ESBOMPROBLEMS',
61+
})
8062
}
8163

8264
// Populate the response with the list of unique nodes (sorted by location)
83-
this.#buildResponse(
84-
items
85-
.sort((a, b) => localeCompare(a.location, b.location))
86-
)
87-
output.standard(this.#parsedResponse)
65+
this.#buildResponse(items.sort((a, b) => localeCompare(a.location, b.location)))
66+
67+
// TODO(BREAKING_CHANGE): all sbom output is in json mode but setting it before
68+
// any of the errors will cause those to be thrown in json mode.
69+
this.npm.config.set('json', true)
70+
output.buffer(this.#response)
8871
}
8972

9073
async execWorkspaces (args) {
@@ -122,10 +105,9 @@ class SBOM extends BaseCommand {
122105
const packageType = this.npm.config.get('sbom-type')
123106
const packageLockOnly = this.npm.config.get('package-lock-only')
124107

125-
this.#response =
126-
sbomFormat === 'cyclonedx'
127-
? cyclonedxOutput({ npm: this.npm, nodes: items, packageType, packageLockOnly })
128-
: spdxOutput({ npm: this.npm, nodes: items, packageType })
108+
this.#response = sbomFormat === 'cyclonedx'
109+
? cyclonedxOutput({ npm: this.npm, nodes: items, packageType, packageLockOnly })
110+
: spdxOutput({ npm: this.npm, nodes: items, packageType })
129111
}
130112
}
131113

lib/commands/team.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ class Team extends BaseCommand {
6868
async create (entity, opts) {
6969
await libteam.create(entity, opts)
7070
if (opts.json) {
71-
output.standard(JSON.stringify({
71+
output.buffer({
7272
created: true,
7373
team: entity,
74-
}))
74+
})
7575
} else if (opts.parseable) {
7676
output.standard(`${entity}\tcreated`)
7777
} else if (!this.npm.silent) {
@@ -82,10 +82,10 @@ class Team extends BaseCommand {
8282
async destroy (entity, opts) {
8383
await libteam.destroy(entity, opts)
8484
if (opts.json) {
85-
output.standard(JSON.stringify({
85+
output.buffer({
8686
deleted: true,
8787
team: entity,
88-
}))
88+
})
8989
} else if (opts.parseable) {
9090
output.standard(`${entity}\tdeleted`)
9191
} else if (!this.npm.silent) {
@@ -96,11 +96,11 @@ class Team extends BaseCommand {
9696
async add (entity, user, opts) {
9797
await libteam.add(user, entity, opts)
9898
if (opts.json) {
99-
output.standard(JSON.stringify({
99+
output.buffer({
100100
added: true,
101101
team: entity,
102102
user,
103-
}))
103+
})
104104
} else if (opts.parseable) {
105105
output.standard(`${user}\t${entity}\tadded`)
106106
} else if (!this.npm.silent) {
@@ -111,11 +111,11 @@ class Team extends BaseCommand {
111111
async rm (entity, user, opts) {
112112
await libteam.rm(user, entity, opts)
113113
if (opts.json) {
114-
output.standard(JSON.stringify({
114+
output.buffer({
115115
removed: true,
116116
team: entity,
117117
user,
118-
}))
118+
})
119119
} else if (opts.parseable) {
120120
output.standard(`${user}\t${entity}\tremoved`)
121121
} else if (!this.npm.silent) {
@@ -126,7 +126,7 @@ class Team extends BaseCommand {
126126
async listUsers (entity, opts) {
127127
const users = (await libteam.lsUsers(entity, opts)).sort()
128128
if (opts.json) {
129-
output.standard(JSON.stringify(users, null, 2))
129+
output.buffer(users)
130130
} else if (opts.parseable) {
131131
output.standard(users.join('\n'))
132132
} else if (!this.npm.silent) {
@@ -140,7 +140,7 @@ class Team extends BaseCommand {
140140
async listTeams (entity, opts) {
141141
const teams = (await libteam.lsTeams(entity, opts)).sort()
142142
if (opts.json) {
143-
output.standard(JSON.stringify(teams, null, 2))
143+
output.buffer(teams)
144144
} else if (opts.parseable) {
145145
output.standard(teams.join('\n'))
146146
} else if (!this.npm.silent) {

0 commit comments

Comments
 (0)