@@ -51,70 +51,71 @@ class Query extends BaseCommand {
51
51
'expect-results' ,
52
52
]
53
53
54
- get parsedResponse ( ) {
55
- return JSON . stringify ( this . #response, null , 2 )
54
+ constructor ( ...args ) {
55
+ super ( ...args )
56
+ this . npm . config . set ( 'json' , true )
56
57
}
57
58
58
59
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' )
61
61
const Arborist = require ( '@npmcli/arborist' )
62
- const opts = {
62
+ const arb = new Arborist ( {
63
63
...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
+ } )
68
68
let tree
69
- if ( this . npm . config . get ( 'package-lock-only' ) ) {
69
+ if ( packageLock ) {
70
70
try {
71
71
tree = await arb . loadVirtual ( )
72
72
} catch ( err ) {
73
73
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
+ )
76
77
}
77
78
} else {
78
- tree = await arb . loadActual ( opts )
79
+ tree = await arb . loadActual ( )
79
80
}
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( )
85
83
}
86
84
87
85
async execWorkspaces ( args ) {
88
86
await this . setWorkspaces ( )
89
87
const Arborist = require ( '@npmcli/arborist' )
90
- const opts = {
88
+ const arb = new Arborist ( {
91
89
...this . npm . flatOptions ,
92
90
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 ] )
93
101
}
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 ( ) {
107
106
this . checkExpected ( this . #response. length )
108
- output . standard ( this . parsedResponse )
107
+ output . buffer ( this . #response )
109
108
}
110
109
111
110
// builds a normalized inventory
112
- buildResponse ( items ) {
111
+ async #queryTree ( tree , arg ) {
112
+ const items = await tree . querySelectorAll ( arg , this . npm . flatOptions )
113
113
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 ) ) {
115
116
const item = new QuerySelectorItem ( node )
116
117
this . #response. push ( item )
117
- if ( node . target . location ) {
118
+ if ( location ) {
118
119
this . #seen. add ( item . location )
119
120
}
120
121
}
0 commit comments