Skip to content

Commit 2537144

Browse files
committed
Add afterUpdate handler
1 parent c7ab15a commit 2537144

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/commands/fix/agent-fix.mts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, promises as fs } from 'node:fs'
1+
import { existsSync } from 'node:fs'
22
import path from 'node:path'
33

44
import semver from 'semver'
@@ -45,7 +45,6 @@ import {
4545
} from '../../shadow/npm/arborist-helpers.mts'
4646
import { removeNodeModules } from '../../utils/fs.mts'
4747
import { globWorkspace } from '../../utils/glob.mts'
48-
import { readLockfile } from '../../utils/lockfile.mts'
4948
import { getPurlObject } from '../../utils/purl.mts'
5049
import { applyRange } from '../../utils/semver.mts'
5150
import { getCveInfoFromAlertsMap } from '../../utils/socket-package-alert.mts'
@@ -104,12 +103,13 @@ export async function agentFix(
104103
alertsMap: AlertsByPurl,
105104
installer: Installer,
106105
{
107-
beforeInstall = noopHandler,
108-
// eslint-disable-next-line sort-destructure-keys/sort-destructure-keys
109106
afterInstall = noopHandler,
107+
afterUpdate = noopHandler,
108+
beforeInstall = noopHandler,
110109
revertInstall = noopHandler,
111110
}: {
112111
beforeInstall?: InstallPhaseHandler | undefined
112+
afterUpdate?: InstallPhaseHandler | undefined
113113
afterInstall?: InstallPhaseHandler | undefined
114114
revertInstall?: InstallPhaseHandler | undefined
115115
},
@@ -382,6 +382,16 @@ export async function agentFix(
382382
// eslint-disable-next-line no-await-in-loop
383383
await editablePkgJson.save({ ignoreWhitespace: true })
384384

385+
// eslint-disable-next-line no-await-in-loop
386+
await afterUpdate(
387+
editablePkgJson,
388+
packument,
389+
oldVersion,
390+
newVersion,
391+
vulnerableVersionRange,
392+
fixConfig,
393+
)
394+
385395
// eslint-disable-next-line no-await-in-loop
386396
const unstagedCResult = await gitUnstagedModifiedFiles(cwd)
387397
const moddedFilepaths = unstagedCResult.ok
@@ -407,14 +417,6 @@ export async function agentFix(
407417
continue infosLoop
408418
}
409419

410-
// eslint-disable-next-line no-await-in-loop
411-
const pkgJsonSrc = await fs.readFile(
412-
editablePkgJson.filename!,
413-
'utf8',
414-
)
415-
// eslint-disable-next-line no-await-in-loop
416-
const lockSrc = await readLockfile(pkgEnvDetails.lockPath)
417-
418420
if (!hasAnnouncedWorkspace) {
419421
hasAnnouncedWorkspace = true
420422
workspaceLogCallCount = logger.logCallCount
@@ -463,11 +465,6 @@ export async function agentFix(
463465

464466
// Check repoInfo to make TypeScript happy.
465467
if (!errored && fixEnv.isCi && fixEnv.repoInfo) {
466-
// Rewrite files in case the install reverted them.
467-
// eslint-disable-next-line no-await-in-loop
468-
await fs.writeFile(editablePkgJson.filename!, pkgJsonSrc, 'utf8')
469-
// eslint-disable-next-line no-await-in-loop
470-
await fs.writeFile(pkgEnvDetails.lockPath, lockSrc!, 'utf8')
471468
try {
472469
if (
473470
// eslint-disable-next-line no-await-in-loop

src/commands/fix/npm-fix.mts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function npmFix(
8484
alertsMap,
8585
install,
8686
{
87-
async beforeInstall(editablePkgJson, packument, oldVersion, newVersion) {
87+
async beforeInstall(editablePkgJson) {
8888
revertData = {
8989
...(editablePkgJson.content.dependencies && {
9090
dependencies: { ...editablePkgJson.content.dependencies },
@@ -98,16 +98,21 @@ export async function npmFix(
9898
peerDependencies: { ...editablePkgJson.content.peerDependencies },
9999
}),
100100
} as PackageJson
101-
102-
const arb = new Arborist({
103-
path: pkgEnvDetails.pkgPath,
104-
...flatConfig,
105-
})
106-
const idealTree = await arb.buildIdealTree()
107-
const node = findPackageNode(idealTree, packument.name, oldVersion)
108-
if (node) {
109-
updateNode(node, newVersion, packument.versions[newVersion]!)
110-
await arb.reify()
101+
},
102+
async afterUpdate(editablePkgJson, packument, oldVersion, newVersion) {
103+
const isWorkspaceRoot =
104+
editablePkgJson.filename === pkgEnvDetails.editablePkgJson.filename
105+
if (isWorkspaceRoot) {
106+
const arb = new Arborist({
107+
path: pkgEnvDetails.pkgPath,
108+
...flatConfig,
109+
})
110+
const idealTree = await arb.buildIdealTree()
111+
const node = findPackageNode(idealTree, packument.name, oldVersion)
112+
if (node) {
113+
updateNode(node, newVersion, packument.versions[newVersion]!)
114+
await arb.reify()
115+
}
111116
}
112117
},
113118
async revertInstall(editablePkgJson) {

0 commit comments

Comments
 (0)