Skip to content

Commit d5f204c

Browse files
authored
Merge branch 'npm:latest' into mm/fix-cache-add
2 parents e805fd0 + 9e6686b commit d5f204c

File tree

26 files changed

+152
-158
lines changed

26 files changed

+152
-158
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/ls.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,14 @@ class LS extends ArboristWorkspaceCmd {
177177
const [rootError] = tree.errors.filter(e =>
178178
e.code === 'EJSONPARSE' && e.path === resolve(path, 'package.json'))
179179

180-
output.buffer(
181-
json ? jsonOutput({ path, problems, result, rootError, seenItems }) :
182-
parseable ? parseableOutput({ seenNodes, global, long }) :
183-
humanOutput({ chalk, result, seenItems, unicode })
184-
)
180+
if (json) {
181+
output.buffer(jsonOutput({ path, problems, result, rootError, seenItems }))
182+
} else {
183+
output.standard(parseable
184+
? parseableOutput({ seenNodes, global, long })
185+
: humanOutput({ chalk, result, seenItems, unicode })
186+
)
187+
}
185188

186189
// if filtering items, should exit with error code on no results
187190
if (result && !result[_include] && args.length) {

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/query.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,70 +51,71 @@ class Query extends BaseCommand {
5151
'expect-results',
5252
]
5353

54-
get parsedResponse () {
55-
return JSON.stringify(this.#response, null, 2)
54+
constructor (...args) {
55+
super(...args)
56+
this.npm.config.set('json', true)
5657
}
5758

5859
async exec (args) {
59-
// one dir up from wherever node_modules lives
60-
const where = resolve(this.npm.dir, '..')
60+
const packageLock = this.npm.config.get('package-lock-only')
6161
const Arborist = require('@npmcli/arborist')
62-
const opts = {
62+
const arb = new Arborist({
6363
...this.npm.flatOptions,
64-
path: where,
65-
forceActual: true,
66-
}
67-
const arb = new Arborist(opts)
64+
// one dir up from wherever node_modules lives
65+
path: resolve(this.npm.dir, '..'),
66+
forceActual: !packageLock,
67+
})
6868
let tree
69-
if (this.npm.config.get('package-lock-only')) {
69+
if (packageLock) {
7070
try {
7171
tree = await arb.loadVirtual()
7272
} catch (err) {
7373
log.verbose('loadVirtual', err.stack)
74-
/* eslint-disable-next-line max-len */
75-
throw this.usageError('A package lock or shrinkwrap file is required in package-lock-only mode')
74+
throw this.usageError(
75+
'A package lock or shrinkwrap file is required in package-lock-only mode'
76+
)
7677
}
7778
} else {
78-
tree = await arb.loadActual(opts)
79+
tree = await arb.loadActual()
7980
}
80-
const items = await tree.querySelectorAll(args[0], this.npm.flatOptions)
81-
this.buildResponse(items)
82-
83-
this.checkExpected(this.#response.length)
84-
output.standard(this.parsedResponse)
81+
await this.#queryTree(tree, args[0])
82+
this.#output()
8583
}
8684

8785
async execWorkspaces (args) {
8886
await this.setWorkspaces()
8987
const Arborist = require('@npmcli/arborist')
90-
const opts = {
88+
const arb = new Arborist({
9189
...this.npm.flatOptions,
9290
path: this.npm.prefix,
91+
})
92+
// FIXME: Workspace support in query does not work as expected so this does not
93+
// do the same package-lock-only check as this.exec().
94+
// https://github.com/npm/cli/pull/6732#issuecomment-1708804921
95+
const tree = await arb.loadActual()
96+
for (const path of this.workspacePaths) {
97+
const wsTree = path === tree.root.path
98+
? tree // --includes-workspace-root
99+
: await tree.querySelectorAll(`.workspace:path(${path})`).then(r => r[0].target)
100+
await this.#queryTree(wsTree, args[0])
93101
}
94-
const arb = new Arborist(opts)
95-
const tree = await arb.loadActual(opts)
96-
for (const workspacePath of this.workspacePaths) {
97-
let items
98-
if (workspacePath === tree.root.path) {
99-
// include-workspace-root
100-
items = await tree.querySelectorAll(args[0])
101-
} else {
102-
const [workspace] = await tree.querySelectorAll(`.workspace:path(${workspacePath})`)
103-
items = await workspace.target.querySelectorAll(args[0], this.npm.flatOptions)
104-
}
105-
this.buildResponse(items)
106-
}
102+
this.#output()
103+
}
104+
105+
#output () {
107106
this.checkExpected(this.#response.length)
108-
output.standard(this.parsedResponse)
107+
output.buffer(this.#response)
109108
}
110109

111110
// builds a normalized inventory
112-
buildResponse (items) {
111+
async #queryTree (tree, arg) {
112+
const items = await tree.querySelectorAll(arg, this.npm.flatOptions)
113113
for (const node of items) {
114-
if (!node.target.location || !this.#seen.has(node.target.location)) {
114+
const { location } = node.target
115+
if (!location || !this.#seen.has(location)) {
115116
const item = new QuerySelectorItem(node)
116117
this.#response.push(item)
117-
if (node.target.location) {
118+
if (location) {
118119
this.#seen.add(item.location)
119120
}
120121
}

0 commit comments

Comments
 (0)