Skip to content

Commit 96f9dbe

Browse files
committed
Use OVERRIDES and RESOLUTIONS constants
1 parent 1be02db commit 96f9dbe

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

src/commands/optimize.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ const {
4545
BUN,
4646
LOCK_EXT,
4747
NPM,
48+
OVERRIDES,
4849
PNPM,
50+
RESOLUTIONS,
4951
SOCKET_CLI_UPDATE_OVERRIDES_IN_PACKAGE_LOCK_FILE,
5052
VLT,
5153
YARN_BERRY,
@@ -56,11 +58,9 @@ const {
5658
} = constants
5759

5860
const COMMAND_TITLE = 'Socket Optimize'
59-
const OVERRIDES_FIELD_NAME = 'overrides'
6061
const NPM_OVERRIDE_PR_URL = 'https://github.com/npm/cli/pull/7025'
6162
const PNPM_FIELD_NAME = PNPM
6263
const PNPM_WORKSPACE = `${PNPM}-workspace`
63-
const RESOLUTIONS_FIELD_NAME = 'resolutions'
6464

6565
const manifestNpmOverrides = getManifestData(NPM)!
6666

@@ -75,35 +75,35 @@ type GetOverridesResult = {
7575

7676
const getOverridesDataByAgent: Record<Agent, GetOverrides> = {
7777
[BUN](pkgJson: PackageJson) {
78-
const overrides = (pkgJson as any)?.resolutions ?? {}
78+
const overrides = (pkgJson as any)?.[RESOLUTIONS] ?? {}
7979
return { type: YARN_BERRY, overrides }
8080
},
8181
// npm overrides documentation:
8282
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
8383
[NPM](pkgJson: PackageJson) {
84-
const overrides = (pkgJson as any)?.overrides ?? {}
84+
const overrides = (pkgJson as any)?.[OVERRIDES] ?? {}
8585
return { type: NPM, overrides }
8686
},
8787
// pnpm overrides documentation:
8888
// https://pnpm.io/package_json#pnpmoverrides
8989
[PNPM](pkgJson: PackageJson) {
90-
const overrides = (pkgJson as any)?.pnpm?.overrides ?? {}
90+
const overrides = (pkgJson as any)?.pnpm?.[OVERRIDES] ?? {}
9191
return { type: PNPM, overrides }
9292
},
9393
[VLT](pkgJson: PackageJson) {
94-
const overrides = (pkgJson as any)?.overrides ?? {}
94+
const overrides = (pkgJson as any)?.[OVERRIDES] ?? {}
9595
return { type: VLT, overrides }
9696
},
9797
// Yarn resolutions documentation:
9898
// https://yarnpkg.com/configuration/manifest#resolutions
9999
[YARN_BERRY](pkgJson: PackageJson) {
100-
const overrides = (pkgJson as any)?.resolutions ?? {}
100+
const overrides = (pkgJson as any)?.[RESOLUTIONS] ?? {}
101101
return { type: YARN_BERRY, overrides }
102102
},
103103
// Yarn resolutions documentation:
104104
// https://classic.yarnpkg.com/en/docs/selective-version-resolutions
105105
[YARN_CLASSIC](pkgJson: PackageJson) {
106-
const overrides = (pkgJson as any)?.resolutions ?? {}
106+
const overrides = (pkgJson as any)?.[RESOLUTIONS] ?? {}
107107
return { type: YARN_CLASSIC, overrides }
108108
}
109109
}
@@ -237,10 +237,7 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
237237
: { [field]: undefined })
238238
)
239239
}
240-
} else if (
241-
field === OVERRIDES_FIELD_NAME ||
242-
field === RESOLUTIONS_FIELD_NAME
243-
) {
240+
} else if (field === OVERRIDES || field === RESOLUTIONS) {
244241
// Properties with undefined values are omitted when saved as JSON.
245242
editablePkgJson.update(<typeof pkgJson>{
246243
[field]: hasKeys(value) ? value : undefined
@@ -251,9 +248,9 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
251248
return
252249
}
253250
if (
254-
(field === OVERRIDES_FIELD_NAME ||
251+
(field === OVERRIDES ||
255252
field === PNPM_FIELD_NAME ||
256-
field === RESOLUTIONS_FIELD_NAME) &&
253+
field === RESOLUTIONS) &&
257254
!hasKeys(value)
258255
) {
259256
return
@@ -264,24 +261,21 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
264261
const entries = Object.entries(pkgJson)
265262
let insertIndex = -1
266263
let isPlacingHigher = false
267-
if (field === OVERRIDES_FIELD_NAME) {
268-
insertIndex = getLowestEntryIndex(entries, [RESOLUTIONS_FIELD_NAME])
264+
if (field === OVERRIDES) {
265+
insertIndex = getLowestEntryIndex(entries, [RESOLUTIONS])
269266
if (insertIndex === -1) {
270267
isPlacingHigher = true
271268
insertIndex = getHighestEntryIndex(entries, [...depFields, PNPM])
272269
}
273-
} else if (field === RESOLUTIONS_FIELD_NAME) {
270+
} else if (field === RESOLUTIONS) {
274271
isPlacingHigher = true
275272
insertIndex = getHighestEntryIndex(entries, [
276273
...depFields,
277-
OVERRIDES_FIELD_NAME,
274+
OVERRIDES,
278275
PNPM
279276
])
280277
} else if (field === PNPM_FIELD_NAME) {
281-
insertIndex = getLowestEntryIndex(entries, [
282-
OVERRIDES_FIELD_NAME,
283-
RESOLUTIONS_FIELD_NAME
284-
])
278+
insertIndex = getLowestEntryIndex(entries, [OVERRIDES, RESOLUTIONS])
285279
if (insertIndex === -1) {
286280
isPlacingHigher = true
287281
insertIndex = getHighestEntryIndex(entries, depFields)
@@ -313,18 +307,14 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
313307
editablePkgJson: EditablePackageJson,
314308
overrides: Overrides
315309
) {
316-
updatePkgJson(editablePkgJson, OVERRIDES_FIELD_NAME, overrides)
310+
updatePkgJson(editablePkgJson, OVERRIDES, overrides)
317311
}
318312

319313
function updateResolutions(
320314
editablePkgJson: EditablePackageJson,
321315
overrides: Overrides
322316
) {
323-
updatePkgJson(
324-
editablePkgJson,
325-
RESOLUTIONS_FIELD_NAME,
326-
<PnpmOrYarnOverrides>overrides
327-
)
317+
updatePkgJson(editablePkgJson, RESOLUTIONS, <PnpmOrYarnOverrides>overrides)
328318
}
329319

330320
return {

0 commit comments

Comments
 (0)