1
- import { $ } from "execa" ;
1
+ import { $ , ExecaError } from "execa" ;
2
2
import { join } from "node:path" ;
3
3
import { readJSONFileSync } from "./fileSystem" ;
4
4
import { logger } from "./logger" ;
@@ -269,8 +269,7 @@ class PNPMCommands implements PackageManagerCommands {
269
269
packageNames : string [ ] ,
270
270
options : PackageManagerOptions
271
271
) : Promise < Record < string , string > > {
272
- const { stdout } = await $ ( { cwd : options . cwd } ) `${ this . cmd } list ${ packageNames } -r --json` ;
273
- const result = JSON . parse ( stdout ) as PnpmList ;
272
+ const result = await this . #listDependencies( packageNames , options ) ;
274
273
275
274
logger . debug ( `Resolving ${ packageNames . join ( " " ) } version using ${ this . name } ` ) ;
276
275
@@ -289,6 +288,21 @@ class PNPMCommands implements PackageManagerCommands {
289
288
290
289
return results ;
291
290
}
291
+
292
+ async #listDependencies( packageNames : string [ ] , options : PackageManagerOptions ) {
293
+ const childProcess = await $ ( {
294
+ cwd : options . cwd ,
295
+ reject : false ,
296
+ } ) `${ this . cmd } list ${ packageNames } -r --json` ;
297
+
298
+ if ( childProcess . failed ) {
299
+ logger . debug ( "Failed to list dependencies, using stdout anyway..." , {
300
+ error : childProcess . stderr ,
301
+ } ) ;
302
+ }
303
+
304
+ return JSON . parse ( childProcess . stdout ) as PnpmList ;
305
+ }
292
306
}
293
307
294
308
type NpmDependency = {
@@ -331,8 +345,7 @@ class NPMCommands implements PackageManagerCommands {
331
345
packageNames : string [ ] ,
332
346
options : PackageManagerOptions
333
347
) : Promise < Record < string , string > > {
334
- const { stdout } = await $ ( { cwd : options . cwd } ) `${ this . cmd } list ${ packageNames } --json` ;
335
- const output = JSON . parse ( stdout ) as NpmListOutput ;
348
+ const output = await this . #listDependencies( packageNames , options ) ;
336
349
337
350
logger . debug ( `Resolving ${ packageNames . join ( " " ) } version using ${ this . name } ` , { output } ) ;
338
351
@@ -349,6 +362,21 @@ class NPMCommands implements PackageManagerCommands {
349
362
return results ;
350
363
}
351
364
365
+ async #listDependencies( packageNames : string [ ] , options : PackageManagerOptions ) {
366
+ const childProcess = await $ ( {
367
+ cwd : options . cwd ,
368
+ reject : false ,
369
+ } ) `${ this . cmd } list ${ packageNames } --json` ;
370
+
371
+ if ( childProcess . failed ) {
372
+ logger . debug ( "Failed to list dependencies, using stdout anyway..." , {
373
+ error : childProcess . stderr ,
374
+ } ) ;
375
+ }
376
+
377
+ return JSON . parse ( childProcess . stdout ) as NpmListOutput ;
378
+ }
379
+
352
380
#recursivelySearchDependencies(
353
381
dependencies : Record < string , NpmDependency > ,
354
382
packageName : string
@@ -404,7 +432,7 @@ class YarnCommands implements PackageManagerCommands {
404
432
packageNames : string [ ] ,
405
433
options : PackageManagerOptions
406
434
) : Promise < Record < string , string > > {
407
- const { stdout } = await $ ( { cwd : options . cwd } ) ` ${ this . cmd } info ${ packageNames } --json` ;
435
+ const stdout = await this . #listDependencies ( packageNames , options ) ;
408
436
409
437
const lines = stdout . split ( "\n" ) ;
410
438
@@ -425,6 +453,21 @@ class YarnCommands implements PackageManagerCommands {
425
453
return results ;
426
454
}
427
455
456
+ async #listDependencies( packageNames : string [ ] , options : PackageManagerOptions ) {
457
+ const childProcess = await $ ( {
458
+ cwd : options . cwd ,
459
+ reject : false ,
460
+ } ) `${ this . cmd } info ${ packageNames } --json` ;
461
+
462
+ if ( childProcess . failed ) {
463
+ logger . debug ( "Failed to list dependencies, using stdout anyway..." , {
464
+ error : childProcess . stderr ,
465
+ } ) ;
466
+ }
467
+
468
+ return childProcess . stdout ;
469
+ }
470
+
428
471
// The "value" when doing yarn info is formatted like this:
429
472
// "package-name@npm:version" or "package-name@workspace:version"
430
473
// This function will parse the value into just the package name.
0 commit comments