Skip to content

Commit b1db070

Browse files
authored
fix(refactor): use output.buffer and set explicit json mode in query (#7534)
1 parent 53cda32 commit b1db070

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

lib/commands/query.js

+37-36
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)