11import { getManifestData } from '@socketsecurity/registry'
22import { runScript } from '@socketsecurity/registry/lib/npm'
3- import { fetchPackagePackument } from '@socketsecurity/registry/lib/packages'
3+ import {
4+ fetchPackagePackument ,
5+ readPackageJson
6+ } from '@socketsecurity/registry/lib/packages'
47import { Spinner } from '@socketsecurity/registry/lib/spinner'
58
69import constants from '../../constants'
@@ -15,13 +18,24 @@ import {
1518 getPackagesAlerts ,
1619 updateNode
1720} from '../../shadow/arborist/lib/arborist/reify'
21+ // import { detect } from '../../utils/package-manager-detector'
22+
23+ import type { SafeNode } from '../../shadow/arborist/lib/node'
1824
1925const { NPM } = constants
2026
27+ function isTopLevel ( tree : SafeNode , node : SafeNode ) : boolean {
28+ return tree . children . get ( node . name ) === node
29+ }
30+
2131export async function runFix ( ) {
2232 const spinner = new Spinner ( ) . start ( )
33+ const cwd = process . cwd ( )
34+ const editablePkgJson = await readPackageJson ( cwd , { editable : true } )
35+ // const agentDetails = await detect()
36+
2337 const arb = new SafeArborist ( {
24- path : process . cwd ( ) ,
38+ path : cwd ,
2539 ...SAFE_ARBORIST_REIFY_OPTIONS_OVERRIDES
2640 } )
2741 await arb . reify ( )
@@ -80,6 +94,24 @@ export async function runFix() {
8094 await runScript ( 'test' , [ ] , { stdio : 'pipe' } )
8195 spinner . info ( `Patched ${ name } ${ oldVersion } -> ${ node . version } ` )
8296 spinner . start ( )
97+ if ( isTopLevel ( tree , node ) ) {
98+ for ( const depField of [
99+ 'dependencies' ,
100+ 'optionalDependencies' ,
101+ 'peerDependencies'
102+ ] ) {
103+ const oldVersion = (
104+ editablePkgJson . content [ depField ] as any
105+ ) ?. [ name ]
106+ if ( oldVersion ) {
107+ const decorator = / ^ [ ~ ^ ] / . exec ( oldVersion ) ?. [ 0 ] ?? ''
108+ ; ( editablePkgJson as any ) . content [ depField ] [ name ] =
109+ `${ decorator } ${ node . version } `
110+ }
111+ }
112+ }
113+ // eslint-disable-next-line no-await-in-loop
114+ await editablePkgJson . save ( )
83115 } catch {
84116 spinner . error ( `Reverting ${ name } to ${ oldVersion } ` )
85117 spinner . start ( )
@@ -95,18 +127,7 @@ export async function runFix() {
95127 }
96128 }
97129 }
98-
99- // await arb.reify({
100- // audit: false,
101- // dryRun: false,
102- // fund: false,
103- // ignoreScripts: false,
104- // progress: false,
105- // save: true,
106- // saveBundle: false,
107- // silent: true
108- // })
109- const arb2 = new Arborist ( )
130+ const arb2 = new Arborist ( { path : cwd } )
110131 arb2 . idealTree = arb . idealTree
111132 await arb2 . reify ( )
112133 spinner . stop ( )
0 commit comments