Skip to content

Commit 82873d0

Browse files
committed
Tweak gitHardReset use
1 parent 354c908 commit 82873d0

File tree

2 files changed

+31
-54
lines changed

2 files changed

+31
-54
lines changed

src/commands/fix/npm-fix.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
getSocketCommitMessage,
1616
gitCheckoutBaseBranchIfAvailable,
1717
gitCreateAndPushBranchIfNeeded,
18-
gitHardReset,
19-
isInGitRepo
18+
gitHardReset
2019
} from './git'
2120
import {
2221
doesPullRequestExistForBranch,
@@ -114,11 +113,10 @@ export async function npmFix(
114113

115114
// Lazily access constants.ENV[CI].
116115
const isCi = constants.ENV[CI]
117-
118-
const { 0: isRepo, 1: workspacePkgJsonPaths } = await Promise.all([
119-
isInGitRepo(cwd),
120-
globWorkspace(pkgEnvDetails.agent, rootPath)
121-
])
116+
const workspacePkgJsonPaths = await globWorkspace(
117+
pkgEnvDetails.agent,
118+
rootPath
119+
)
122120

123121
const pkgJsonPaths = [
124122
...workspacePkgJsonPaths,
@@ -139,7 +137,7 @@ export async function npmFix(
139137

140138
const oldVersions = arrayUnique(
141139
findPackageNodes(arb.idealTree!, name)
142-
.map(n => n.version)
140+
.map(n => n.target?.version ?? n.version)
143141
.filter(Boolean)
144142
)
145143
const packument =
@@ -335,13 +333,14 @@ export async function npmFix(
335333
// eslint-disable-next-line no-await-in-loop
336334
await Promise.all([
337335
removeNodeModules(cwd),
338-
...(isRepo ? [gitHardReset(cwd)] : []),
339-
...(saved && !isRepo ? [editablePkgJson.save()] : [])
336+
...(isCi
337+
? [gitCheckoutBaseBranchIfAvailable(baseBranch, cwd)]
338+
: []),
339+
...(saved && !isCi ? [editablePkgJson.save()] : [])
340340
])
341-
if (!isRepo && installed) {
342-
// eslint-disable-next-line no-await-in-loop
343-
await install(revertTree, { cwd })
344-
}
341+
// eslint-disable-next-line no-await-in-loop
342+
await install(revertTree, { cwd })
343+
345344
if (errored) {
346345
if (!failedSpecs.has(newSpecKey)) {
347346
failedSpecs.add(newSpecKey)

src/commands/fix/pnpm-fix.ts

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import {
1616
getSocketBranchName,
1717
getSocketCommitMessage,
1818
gitCheckoutBaseBranchIfAvailable,
19-
gitCreateAndPushBranchIfNeeded,
20-
gitHardReset,
21-
isInGitRepo
19+
gitCreateAndPushBranchIfNeeded
2220
} from './git'
2321
import {
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

Comments
 (0)