1
1
import path from 'node:path'
2
2
3
+ import Config from '@npmcli/config'
4
+ import {
5
+ definitions ,
6
+ flatten ,
7
+ shorthands ,
8
+ // @ts -ignore
9
+ } from '@npmcli/config/lib/definitions'
3
10
import semver from 'semver'
4
11
5
12
import { getManifestData } from '@socketsecurity/registry'
@@ -68,6 +75,7 @@ type InstallOptions = {
68
75
}
69
76
70
77
async function install (
78
+ pkgEnvDetails : EnvDetails ,
71
79
arb : ArboristInstance ,
72
80
options : InstallOptions ,
73
81
) : Promise < NodeClass | null > {
@@ -76,11 +84,26 @@ async function install(
76
84
...options ,
77
85
} as InstallOptions
78
86
try {
79
- const newArb = new Arborist ( { path : cwd } )
87
+ const config = new Config ( {
88
+ argv : [ ] ,
89
+ cwd,
90
+ definitions,
91
+ flatten,
92
+ npmPath : pkgEnvDetails . agentExecPath ,
93
+ shorthands,
94
+ } )
95
+ await config . load ( )
96
+
97
+ const legacyPeerDeps = config . get ( 'legacy-peer-deps' )
98
+ const newArb = new Arborist ( {
99
+ legacyPeerDeps,
100
+ path : cwd ,
101
+ } )
80
102
newArb . idealTree = await arb . buildIdealTree ( )
81
- const actualTree = await newArb . reify ( )
82
- arb . actualTree = actualTree
83
- return actualTree
103
+ await newArb . reify ( )
104
+ arb . actualTree = null
105
+ await arb . loadActual ( )
106
+ return arb . actualTree
84
107
} catch { }
85
108
return null
86
109
}
@@ -116,13 +139,15 @@ export async function npmFix(
116
139
117
140
let count = 0
118
141
119
- const arb = new Arborist ( {
142
+ let arb = new Arborist ( {
120
143
path : rootPath ,
121
144
...SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES ,
122
145
} )
123
146
// Calling arb.reify() creates the arb.diff object, nulls-out arb.idealTree,
124
147
// and populates arb.actualTree.
125
- let actualTree = await arb . reify ( )
148
+ await arb . reify ( )
149
+ await arb . loadActual ( )
150
+ let actualTree = arb . actualTree !
126
151
127
152
let alertsMap
128
153
try {
@@ -231,6 +256,13 @@ export async function npmFix(
231
256
j < length_j ;
232
257
j += 1
233
258
) {
259
+ arb = new Arborist ( {
260
+ path : rootPath ,
261
+ ...SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES ,
262
+ } )
263
+ // eslint-disable-next-line no-await-in-loop
264
+ await arb . loadActual ( )
265
+ actualTree = arb . actualTree !
234
266
const isLastPkgJsonPath = j === length_j - 1
235
267
const pkgJsonPath = pkgJsonPaths [ j ] !
236
268
const pkgPath = path . dirname ( pkgJsonPath )
@@ -262,7 +294,7 @@ export async function npmFix(
262
294
const editablePkgJson = await readPackageJson ( pkgJsonPath , {
263
295
editable : true ,
264
296
} )
265
- const fixedVersions = new Set < string > ( )
297
+ const seenVersions = new Set < string > ( )
266
298
267
299
let hasAnnouncedWorkspace = false
268
300
let workspaceLogCallCount = logger . logCallCount
@@ -301,7 +333,7 @@ export async function npmFix(
301
333
)
302
334
continue infosLoop
303
335
}
304
- if ( fixedVersions . has ( newVersion ) ) {
336
+ if ( seenVersions . has ( newVersion ) ) {
305
337
continue infosLoop
306
338
}
307
339
if ( semver . gte ( oldVersion , newVersion ) ) {
@@ -371,7 +403,7 @@ export async function npmFix(
371
403
let errored = false
372
404
try {
373
405
// eslint-disable-next-line no-await-in-loop
374
- const maybeActualTree = await install ( arb , { cwd } )
406
+ const maybeActualTree = await install ( pkgEnvDetails , arb , { cwd } )
375
407
if ( maybeActualTree ) {
376
408
actualTree = maybeActualTree
377
409
if ( test ) {
@@ -380,7 +412,7 @@ export async function npmFix(
380
412
await runScript ( testScript , [ ] , { spinner, stdio : 'ignore' } )
381
413
}
382
414
spinner ?. success ( `Fixed ${ name } in ${ workspace } .` )
383
- fixedVersions . add ( newVersion )
415
+ seenVersions . add ( newVersion )
384
416
} else {
385
417
errored = true
386
418
}
@@ -459,13 +491,15 @@ export async function npmFix(
459
491
// eslint-disable-next-line no-await-in-loop
460
492
await gitResetAndClean ( ciEnv . baseBranch , cwd )
461
493
// eslint-disable-next-line no-await-in-loop
462
- const maybeActualTree = await install ( arb , { cwd } )
463
- if ( ! maybeActualTree ) {
464
- // Exit early if install fails.
465
- return handleInstallFail ( )
494
+ const maybeActualTree = await install ( pkgEnvDetails , arb , {
495
+ cwd,
496
+ } )
497
+ if ( maybeActualTree ) {
498
+ actualTree = maybeActualTree
499
+ continue infosLoop
466
500
}
467
- actualTree = maybeActualTree
468
- continue infosLoop
501
+ // Exit early if install fails.
502
+ return handleInstallFail ( )
469
503
}
470
504
471
505
// eslint-disable-next-line no-await-in-loop
@@ -529,7 +563,7 @@ export async function npmFix(
529
563
// eslint-disable-next-line no-await-in-loop
530
564
await gitResetAndClean ( ciEnv . baseBranch , cwd )
531
565
// eslint-disable-next-line no-await-in-loop
532
- const maybeActualTree = await install ( arb , { cwd } )
566
+ const maybeActualTree = await install ( pkgEnvDetails , arb , { cwd } )
533
567
spinner ?. stop ( )
534
568
if ( maybeActualTree ) {
535
569
actualTree = maybeActualTree
@@ -547,13 +581,14 @@ export async function npmFix(
547
581
editablePkgJson . save ( { ignoreWhitespace : true } ) ,
548
582
] )
549
583
// eslint-disable-next-line no-await-in-loop
550
- const maybeActualTree = await install ( arb , { cwd } )
584
+ const maybeActualTree = await install ( pkgEnvDetails , arb , { cwd } )
551
585
spinner ?. stop ( )
552
- if ( ! maybeActualTree ) {
586
+ if ( maybeActualTree ) {
587
+ actualTree = maybeActualTree
588
+ } else {
553
589
// Exit early if install fails.
554
590
return handleInstallFail ( )
555
591
}
556
- actualTree = maybeActualTree
557
592
}
558
593
logger . fail ( `Update failed for ${ oldId } in ${ workspace } .` , error )
559
594
}
@@ -579,5 +614,6 @@ export async function npmFix(
579
614
580
615
spinner ?. stop ( )
581
616
582
- return { ok : true , data : { fixed : true } } // true? did we actually change anything?
617
+ // Or, did we change anything?
618
+ return { ok : true , data : { fixed : true } }
583
619
}
0 commit comments