@@ -16,9 +16,7 @@ import {
1616 getSocketBranchName ,
1717 getSocketCommitMessage ,
1818 gitCheckoutBaseBranchIfAvailable ,
19- gitCreateAndPushBranchIfNeeded ,
20- gitHardReset ,
21- isInGitRepo
19+ gitCreateAndPushBranchIfNeeded
2220} from './git'
2321import {
2422 doesPullRequestExistForBranch ,
@@ -121,39 +119,35 @@ export async function pnpmFix(
121119
122120 // Lazily access constants.ENV[CI].
123121 const isCi = constants . ENV [ CI ]
122+ const workspacePkgJsonPaths = await globWorkspace (
123+ pkgEnvDetails . agent ,
124+ rootPath
125+ )
124126
125- const { 0 : isRepo , 1 : workspacePkgJsonPaths } = await Promise . all ( [
126- isInGitRepo ( cwd ) ,
127- globWorkspace ( pkgEnvDetails . agent , rootPath )
128- ] )
127+ const baseBranch = isCi ? getBaseGitBranch ( ) : ''
128+
129+ const { owner, repo } = isCi
130+ ? getGitHubEnvRepoInfo ( )
131+ : { owner : '' , repo : '' }
129132
130133 const pkgJsonPaths = [
131134 ...workspacePkgJsonPaths ,
132135 // Process the workspace root last since it will add an override to package.json.
133136 pkgEnvDetails . editablePkgJson . filename !
134137 ]
135138
136- let actualTree
139+ let actualTree = await getActualTree ( cwd )
137140
138141 for ( const { 0 : name , 1 : infos } of infoByPkg ) {
139142 if ( getManifestData ( NPM , name ) ) {
140143 spinner ?. info ( `Skipping ${ name } . Socket Optimize package exists.` )
141144 continue
142145 }
143- // eslint-disable-next-line no-await-in-loop
144- await Promise . all ( [
145- removeNodeModules ( cwd ) ,
146- ...( isRepo ? [ gitHardReset ( cwd ) ] : [ ] )
147- ] )
148- // eslint-disable-next-line no-await-in-loop
149- actualTree = await install ( pkgEnvDetails , { spinner } )
150-
151146 const oldVersions = arrayUnique (
152147 findPackageNodes ( actualTree , name )
153- . map ( n => n . version )
148+ . map ( n => n . target ?. version ?? n . version )
154149 . filter ( Boolean )
155150 )
156- debugLog ( name , 'oldVersions' , oldVersions )
157151 const packument =
158152 oldVersions . length && infos . length
159153 ? // eslint-disable-next-line no-await-in-loop
@@ -179,14 +173,6 @@ export async function pnpmFix(
179173 firstPatchedVersionIdentifier,
180174 vulnerableVersionRange
181175 } of infos ) {
182- // eslint-disable-next-line no-await-in-loop
183- await Promise . all ( [
184- removeNodeModules ( cwd ) ,
185- ...( isRepo ? [ gitHardReset ( cwd ) ] : [ ] )
186- ] )
187- // eslint-disable-next-line no-await-in-loop
188- actualTree = await install ( pkgEnvDetails , { spinner } )
189-
190176 const node = findPackageNode ( actualTree , name , oldVersion )
191177 if ( ! node ) {
192178 debugLog (
@@ -292,20 +278,11 @@ export async function pnpmFix(
292278 const branch = isCi
293279 ? getSocketBranchName ( oldPurl , newVersion , workspaceName )
294280 : ''
295- const baseBranch = isCi ? getBaseGitBranch ( ) : ''
296- const { owner, repo } = isCi
297- ? getGitHubEnvRepoInfo ( )
298- : { owner : '' , repo : '' }
299281 const shouldOpenPr = isCi
300282 ? // eslint-disable-next-line no-await-in-loop
301283 ! ( await doesPullRequestExistForBranch ( owner , repo , branch ) )
302284 : false
303285
304- if ( isCi ) {
305- // eslint-disable-next-line no-await-in-loop
306- await gitCheckoutBaseBranchIfAvailable ( baseBranch , cwd )
307- }
308-
309286 if ( updateData ) {
310287 editablePkgJson . update ( updateData )
311288 }
@@ -397,18 +374,19 @@ export async function pnpmFix(
397374 spinner ?. error ( `Reverting ${ newSpec } ${ workspaceDetails } ` , error )
398375 }
399376 }
377+
400378 editablePkgJson . update ( revertData )
401379 // eslint-disable-next-line no-await-in-loop
402380 await Promise . all ( [
403381 removeNodeModules ( cwd ) ,
404- ...( isRepo
405- ? [ gitHardReset ( cwd ) ]
406- : installed
407- ? [ editablePkgJson . save ( ) ]
408- : [ ] )
382+ ...( isCi
383+ ? [ gitCheckoutBaseBranchIfAvailable ( baseBranch , cwd ) ]
384+ : [ ] ) ,
385+ ...( installed && ! isCi ? [ editablePkgJson . save ( ) ] : [ ] )
409386 ] )
410387 // eslint-disable-next-line no-await-in-loop
411388 actualTree = await install ( pkgEnvDetails , { spinner } )
389+
412390 if ( errored ) {
413391 if ( ! failedSpecs . has ( newSpecKey ) ) {
414392 failedSpecs . add ( newSpecKey )
0 commit comments