Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove json.stringify from all commands #7541

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/commands/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Access extends BaseCommand {
outputs[item] = lookup[val] || val
}
if (this.npm.config.get('json')) {
output.standard(JSON.stringify(outputs, null, 2))
output.buffer(outputs)
} else {
for (const item of Object.keys(outputs).sort(localeCompare)) {
if (!limiter || limiter === item) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ ${defData}

publicConf[key] = value
}
output.standard(JSON.stringify(publicConf, null, 2))
output.buffer(publicConf)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Explain extends ArboristWorkspaceCmd {
}

if (this.npm.flatOptions.json) {
output.standard(JSON.stringify(expls, null, 2))
output.buffer(expls)
} else {
output.standard(expls.map(expl => {
return explainNode(expl, Infinity, this.npm.chalk)
Expand Down
6 changes: 1 addition & 5 deletions lib/commands/fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ class Fund extends ArboristWorkspaceCmd {
})

if (this.npm.config.get('json')) {
output.standard(this.printJSON(fundingInfo))
output.buffer(fundingInfo)
} else {
output.standard(this.printHuman(fundingInfo))
}
}

printJSON (fundingInfo) {
return JSON.stringify(fundingInfo, null, 2)
}

printHuman (fundingInfo) {
const unicode = this.npm.config.get('unicode')
const seenUrls = new Map()
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Hook extends BaseCommand {
async add (pkg, uri, secret, opts) {
const hook = await hookApi.add(pkg, uri, secret, opts)
if (opts.json) {
output.standard(JSON.stringify(hook, null, 2))
output.buffer(hook)
} else if (opts.parseable) {
output.standard(Object.keys(hook).join('\t'))
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))
Expand All @@ -53,7 +53,7 @@ class Hook extends BaseCommand {
const hooks = await hookApi.ls({ ...opts, package: pkg })

if (opts.json) {
output.standard(JSON.stringify(hooks, null, 2))
output.buffer(hooks)
} else if (opts.parseable) {
output.standard(Object.keys(hooks[0]).join('\t'))
hooks.forEach(hook => {
Expand All @@ -80,7 +80,7 @@ class Hook extends BaseCommand {
async rm (id, opts) {
const hook = await hookApi.rm(id, opts)
if (opts.json) {
output.standard(JSON.stringify(hook, null, 2))
output.buffer(hook)
} else if (opts.parseable) {
output.standard(Object.keys(hook).join('\t'))
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))
Expand All @@ -92,7 +92,7 @@ class Hook extends BaseCommand {
async update (id, uri, secret, opts) {
const hook = await hookApi.update(id, uri, secret, opts)
if (opts.json) {
output.standard(JSON.stringify(hook, null, 2))
output.buffer(hook)
} else if (opts.parseable) {
output.standard(Object.keys(hook).join('\t'))
output.standard(Object.keys(hook).map(k => hook[k]).join('\t'))
Expand Down
16 changes: 7 additions & 9 deletions lib/commands/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ class Org extends BaseCommand {
org = org.replace(/^[~@]?/, '')
const userCount = Object.keys(roster).length
if (opts.json) {
output.standard(
JSON.stringify({
user,
org,
userCount,
deleted: true,
})
)
output.buffer({
user,
org,
userCount,
deleted: true,
})
} else if (opts.parseable) {
output.standard(['user', 'org', 'userCount', 'deleted'].join('\t'))
output.standard([user, org, userCount, true].join('\t'))
Expand Down Expand Up @@ -135,7 +133,7 @@ class Org extends BaseCommand {
roster = newRoster
}
if (opts.json) {
output.standard(JSON.stringify(roster, null, 2))
output.buffer(roster)
} else if (opts.parseable) {
output.standard(['user', 'role'].join('\t'))
Object.keys(roster).forEach(u => {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Ping extends BaseCommand {
const time = Date.now() - start
log.notice('PONG', `${time}ms`)
if (this.npm.config.get('json')) {
output.standard(JSON.stringify({
output.buffer({
registry: cleanRegistry,
time,
details,
}, null, 2))
})
} else if (Object.keys(details).length) {
log.notice('PONG', JSON.stringify(details, null, 2))
}
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Profile extends BaseCommand {
}

if (this.npm.config.get('json')) {
output.standard(JSON.stringify(info, null, 2))
output.buffer(info)
return
}

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

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

if (this.npm.config.get('json')) {
output.standard(JSON.stringify({ tfa: false }, null, 2))
output.buffer({ tfa: false })
} else if (this.npm.config.get('parseable')) {
output.standard('tfa\tfalse')
} else {
Expand Down
62 changes: 22 additions & 40 deletions lib/commands/sbom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ class SBOM extends BaseCommand {
'workspaces',
]

get #parsedResponse () {
return JSON.stringify(this.#response, null, 2)
}

async exec () {
const sbomFormat = this.npm.config.get('sbom-format')
const packageLockOnly = this.npm.config.get('package-lock-only')
Expand All @@ -35,56 +31,43 @@ class SBOM extends BaseCommand {
throw this.usageError(`Must specify --sbom-format flag with one of: ${SBOM_FORMATS.join(', ')}.`)
}

const Arborist = require('@npmcli/arborist')

const opts = {
...this.npm.flatOptions,
path: this.npm.prefix,
forceActual: true,
}
const Arborist = require('@npmcli/arborist')
const arb = new Arborist(opts)

let tree
if (packageLockOnly) {
try {
tree = await arb.loadVirtual(opts)
} catch (err) {
/* eslint-disable-next-line max-len */
throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode')
}
} else {
tree = await arb.loadActual(opts)
}
const tree = packageLockOnly ? await arb.loadVirtual(opts).catch(() => {
/* eslint-disable-next-line max-len */
throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode')
}) : await arb.loadActual(opts)

// Collect the list of selected workspaces in the project
let wsNodes
if (this.workspaceNames && this.workspaceNames.length) {
wsNodes = arb.workspaceNodes(tree, this.workspaceNames)
}
const wsNodes = this.workspaceNames?.length
? arb.workspaceNodes(tree, this.workspaceNames)
: null

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

const errors = new Set()
for (const node of items) {
detectErrors(node).forEach(error => errors.add(error))
}

if (errors.size > 0) {
throw Object.assign(
new Error([...errors].join('\n')),
{ code: 'ESBOMPROBLEMS' }
)
const errors = items.flatMap(node => detectErrors(node))
if (errors.length) {
throw Object.assign(new Error([...new Set(errors)].join('\n')), {
code: 'ESBOMPROBLEMS',
})
}

// Populate the response with the list of unique nodes (sorted by location)
this.#buildResponse(
items
.sort((a, b) => localeCompare(a.location, b.location))
)
output.standard(this.#parsedResponse)
this.#buildResponse(items.sort((a, b) => localeCompare(a.location, b.location)))

// TODO(BREAKING_CHANGE): all sbom output is in json mode but setting it before
// any of the errors will cause those to be thrown in json mode.
this.npm.config.set('json', true)
output.buffer(this.#response)
}

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

this.#response =
sbomFormat === 'cyclonedx'
? cyclonedxOutput({ npm: this.npm, nodes: items, packageType, packageLockOnly })
: spdxOutput({ npm: this.npm, nodes: items, packageType })
this.#response = sbomFormat === 'cyclonedx'
? cyclonedxOutput({ npm: this.npm, nodes: items, packageType, packageLockOnly })
: spdxOutput({ npm: this.npm, nodes: items, packageType })
}
}

Expand Down
20 changes: 10 additions & 10 deletions lib/commands/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Team extends BaseCommand {
async create (entity, opts) {
await libteam.create(entity, opts)
if (opts.json) {
output.standard(JSON.stringify({
output.buffer({
created: true,
team: entity,
}))
})
} else if (opts.parseable) {
output.standard(`${entity}\tcreated`)
} else if (!this.npm.silent) {
Expand All @@ -82,10 +82,10 @@ class Team extends BaseCommand {
async destroy (entity, opts) {
await libteam.destroy(entity, opts)
if (opts.json) {
output.standard(JSON.stringify({
output.buffer({
deleted: true,
team: entity,
}))
})
} else if (opts.parseable) {
output.standard(`${entity}\tdeleted`)
} else if (!this.npm.silent) {
Expand All @@ -96,11 +96,11 @@ class Team extends BaseCommand {
async add (entity, user, opts) {
await libteam.add(user, entity, opts)
if (opts.json) {
output.standard(JSON.stringify({
output.buffer({
added: true,
team: entity,
user,
}))
})
} else if (opts.parseable) {
output.standard(`${user}\t${entity}\tadded`)
} else if (!this.npm.silent) {
Expand All @@ -111,11 +111,11 @@ class Team extends BaseCommand {
async rm (entity, user, opts) {
await libteam.rm(user, entity, opts)
if (opts.json) {
output.standard(JSON.stringify({
output.buffer({
removed: true,
team: entity,
user,
}))
})
} else if (opts.parseable) {
output.standard(`${user}\t${entity}\tremoved`)
} else if (!this.npm.silent) {
Expand All @@ -126,7 +126,7 @@ class Team extends BaseCommand {
async listUsers (entity, opts) {
const users = (await libteam.lsUsers(entity, opts)).sort()
if (opts.json) {
output.standard(JSON.stringify(users, null, 2))
output.buffer(users)
} else if (opts.parseable) {
output.standard(users.join('\n'))
} else if (!this.npm.silent) {
Expand All @@ -140,7 +140,7 @@ class Team extends BaseCommand {
async listTeams (entity, opts) {
const teams = (await libteam.lsTeams(entity, opts)).sort()
if (opts.json) {
output.standard(JSON.stringify(teams, null, 2))
output.buffer(teams)
} else if (opts.parseable) {
output.standard(teams.join('\n'))
} else if (!this.npm.silent) {
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Token extends BaseCommand {
log.info('token', 'getting list')
const tokens = await listTokens(this.npm.flatOptions)
if (json) {
output.standard(JSON.stringify(tokens, null, 2))
output.buffer(tokens)
return
}
if (parseable) {
Expand Down Expand Up @@ -117,7 +117,7 @@ class Token extends BaseCommand {
})
)
if (json) {
output.standard(JSON.stringify(toRemove))
output.buffer(toRemove)
} else if (parseable) {
output.standard(toRemove.join('\t'))
} else {
Expand All @@ -142,7 +142,7 @@ class Token extends BaseCommand {
delete result.key
delete result.updated
if (json) {
output.standard(JSON.stringify(result))
output.buffer(result)
} else if (parseable) {
Object.keys(result).forEach(k => output.standard(k + '\t' + result[k]))
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Version extends BaseCommand {
}

if (this.npm.config.get('json')) {
output.standard(JSON.stringify(results, null, 2))
output.buffer(results)
} else {
output.standard(results)
}
Expand Down
8 changes: 5 additions & 3 deletions lib/commands/whoami.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class Whoami extends BaseCommand {

async exec () {
const username = await getIdentity(this.npm, { ...this.npm.flatOptions })
output.standard(
this.npm.config.get('json') ? JSON.stringify(username) : username
)
if (this.npm.config.get('json')) {
output.buffer(username)
} else {
output.standard(username)
}
}
}

Expand Down
Loading
Loading