Skip to content

Commit ff5788a

Browse files
committed
Use methods instead of maps for agent specific actions
1 parent 3aa82b9 commit ff5788a

File tree

5 files changed

+176
-98
lines changed

5 files changed

+176
-98
lines changed

src/commands/optimize/add-overrides.mts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@ import { fetchPackageManifest } from '@socketsecurity/registry/lib/packages'
88
import { pEach } from '@socketsecurity/registry/lib/promises'
99
import { Spinner } from '@socketsecurity/registry/lib/spinner'
1010

11-
import { depsIncludesByAgent } from './deps-includes-by-agent.mts'
11+
import { lsStdoutIncludes } from './deps-includes-by-agent.mts'
1212
import { getDependencyEntries } from './get-dependency-entries.mts'
1313
import {
1414
getOverridesData,
1515
getOverridesDataNpm,
1616
getOverridesDataYarnClassic,
1717
} from './get-overrides-by-agent.mts'
18-
import { lockfileIncludesByAgent } from './lockfile-includes-by-agent.mts'
19-
import { lsByAgent } from './ls-by-agent.mts'
18+
import { lockSrcIncludes } from './lockfile-includes-by-agent.mts'
19+
import { listPackages } from './ls-by-agent.mts'
2020
import { CMD_NAME } from './shared.mts'
21-
import { updateManifestByAgent } from './update-manifest-by-agent.mts'
21+
import { updateManifest } from './update-manifest-by-agent.mts'
2222
import constants from '../../constants.mts'
2323
import { cmdPrefixMessage } from '../../utils/cmd.mts'
2424
import { globWorkspace } from '../../utils/glob.mts'
2525
import { npa } from '../../utils/npm-package-arg.mts'
2626
import { getMajor } from '../../utils/semver.mts'
2727

2828
import type { GetOverridesResult } from './get-overrides-by-agent.mts'
29-
import type { AgentLockIncludesFn } from './lockfile-includes-by-agent.mts'
3029
import type { AliasResult } from '../../utils/npm-package-arg.mts'
3130
import type { EnvDetails } from '../../utils/package-environment.mts'
3231
import type { Logger } from '@socketsecurity/registry/lib/logger'
@@ -118,6 +117,7 @@ export async function addOverrides(
118117
),
119118
)
120119

120+
const addingText = `Adding overrides to ${workspace}...`
121121
let loggedAddingText = false
122122

123123
// Chunk package names to process them in parallel 3 at a time.
@@ -158,32 +158,30 @@ export async function addOverrides(
158158
state.addedInWorkspaces.add(workspace)
159159
}
160160
if (!loggedAddingText) {
161-
spinner?.setText(`Adding overrides to ${workspace}...`)
161+
spinner?.setText(addingText)
162162
loggedAddingText = true
163163
}
164164
}
165165
depAliasMap.set(origPkgName, thisSpec)
166166
}
167167
}
168168
if (isWorkspaceRoot) {
169-
// The AgentDepsIncludesFn and AgentLockIncludesFn types overlap in their
170-
// first two parameters. AgentLockIncludesFn accepts an optional third
171-
// parameter which AgentDepsIncludesFn will ignore so we cast thingScanner
172-
// as an AgentLockIncludesFn type.
169+
// The lockSrcIncludes and lsStdoutIncludes functions overlap in their
170+
// first two parameters. lockSrcIncludes accepts an optional third parameter
171+
// which lsStdoutIncludes will ignore.
173172
const thingScanner = (
174-
isLockScanned
175-
? lockfileIncludesByAgent.get(agent)
176-
: depsIncludesByAgent.get(agent)
177-
) as AgentLockIncludesFn
173+
isLockScanned ? lockSrcIncludes : lsStdoutIncludes
174+
) as typeof lockSrcIncludes
175+
178176
const thingToScan = isLockScanned
179177
? lockSrc
180-
: await lsByAgent.get(agent)!(pkgEnvDetails, pkgPath, { npmExecPath })
178+
: await listPackages(pkgEnvDetails, { cwd: pkgPath, npmExecPath })
181179
// Chunk package names to process them in parallel 3 at a time.
182180
await pEach(overridesDataObjects, 3, async ({ overrides, type }) => {
183181
const overrideExists = hasOwn(overrides, origPkgName)
184182
if (
185183
overrideExists ||
186-
thingScanner(thingToScan, origPkgName, lockName)
184+
thingScanner(pkgEnvDetails, thingToScan, origPkgName, lockName)
187185
) {
188186
const oldSpec = overrideExists ? overrides[origPkgName]! : undefined
189187
const origDepAlias = depAliasMap.get(origPkgName)
@@ -231,7 +229,7 @@ export async function addOverrides(
231229
const addedOrUpdated = overrideExists ? 'updated' : 'added'
232230
state[addedOrUpdated].add(sockRegPkgName)
233231
if (!loggedAddingText) {
234-
spinner?.setText(`Adding overrides to ${workspace}...`)
232+
spinner?.setText(addingText)
235233
loggedAddingText = true
236234
}
237235
}
@@ -280,8 +278,9 @@ export async function addOverrides(
280278
)
281279
if (isWorkspaceRoot) {
282280
for (const { overrides, type } of overridesDataObjects) {
283-
updateManifestByAgent.get(type)!(
284-
pkgEnvDetails,
281+
updateManifest(
282+
type,
283+
pkgEnvDetails.editablePkgJson,
285284
toSortedObject(overrides),
286285
)
287286
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import constants from '../../constants.mts'
22

3-
import type { Agent } from '../../utils/package-environment.mts'
4-
5-
type AgentDepsIncludesFn = (stdout: string, name: string) => boolean
3+
import type { EnvDetails } from '../../utils/package-environment.mts'
64

75
const { BUN, NPM, PNPM, VLT, YARN_BERRY, YARN_CLASSIC } = constants
86

9-
function matchLsCmdViewHumanStdout(stdout: string, name: string) {
7+
export function matchLsCmdViewHumanStdout(stdout: string, name: string) {
108
return stdout.includes(` ${name}@`)
119
}
1210

13-
function matchQueryCmdStdout(stdout: string, name: string) {
11+
export function matchQueryCmdStdout(stdout: string, name: string) {
1412
return stdout.includes(`"${name}"`)
1513
}
1614

17-
export const depsIncludesByAgent = new Map<Agent, AgentDepsIncludesFn>([
18-
[BUN, matchLsCmdViewHumanStdout],
19-
[NPM, matchQueryCmdStdout],
20-
[PNPM, matchQueryCmdStdout],
21-
[VLT, matchQueryCmdStdout],
22-
[YARN_BERRY, matchLsCmdViewHumanStdout],
23-
[YARN_CLASSIC, matchLsCmdViewHumanStdout],
24-
])
15+
export function lsStdoutIncludes(
16+
pkgEnvDetails: EnvDetails,
17+
stdout: string,
18+
name: string,
19+
): boolean {
20+
switch (pkgEnvDetails.agent) {
21+
case BUN:
22+
case YARN_BERRY:
23+
case YARN_CLASSIC:
24+
return matchLsCmdViewHumanStdout(stdout, name)
25+
case PNPM:
26+
case VLT:
27+
case NPM:
28+
default:
29+
return matchQueryCmdStdout(stdout, name)
30+
}
31+
}

src/commands/optimize/lockfile-includes-by-agent.mts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@ import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
22

33
import constants from '../../constants.mts'
44

5-
import type { Agent } from '../../utils/package-environment.mts'
6-
7-
export type AgentLockIncludesFn = (
8-
lockSrc: string,
9-
name: string,
10-
ext?: string | undefined,
11-
) => boolean
5+
import type { EnvDetails } from '../../utils/package-environment.mts'
126

137
const { BUN, LOCK_EXT, NPM, PNPM, VLT, YARN_BERRY, YARN_CLASSIC } = constants
148

15-
function includesNpm(lockSrc: string, name: string) {
9+
export function npmLockSrcIncludes(lockSrc: string, name: string) {
1610
// Detects the package name in the following cases:
1711
// "name":
1812
return lockSrc.includes(`"${name}":`)
1913
}
2014

21-
function includesBun(lockSrc: string, name: string, lockName?: string) {
15+
export function bunLockSrcIncludes(
16+
lockSrc: string,
17+
name: string,
18+
lockName?: string | undefined,
19+
) {
2220
// This is a bit counterintuitive. When lockName ends with a .lockb
2321
// we treat it as a yarn.lock. When lockName ends with a .lock we
2422
// treat it as a package-lock.json. The bun.lock format is not identical
2523
// package-lock.json, however it close enough for npmLockIncludes to work.
2624
const lockfileScanner = lockName?.endsWith(LOCK_EXT)
27-
? includesNpm
28-
: includesYarn
25+
? npmLockSrcIncludes
26+
: yarnLockSrcIncludes
2927
return lockfileScanner(lockSrc, name)
3028
}
3129

32-
function includesPnpm(lockSrc: string, name: string) {
30+
export function pnpmLockSrcIncludes(lockSrc: string, name: string) {
3331
const escapedName = escapeRegExp(name)
3432
return new RegExp(
3533
// Detects the package name.
@@ -44,13 +42,13 @@ function includesPnpm(lockSrc: string, name: string) {
4442
).test(lockSrc)
4543
}
4644

47-
function includesVlt(lockSrc: string, name: string) {
45+
export function vltLockSrcIncludes(lockSrc: string, name: string) {
4846
// Detects the package name in the following cases:
4947
// "name"
5048
return lockSrc.includes(`"${name}"`)
5149
}
5250

53-
function includesYarn(lockSrc: string, name: string) {
51+
export function yarnLockSrcIncludes(lockSrc: string, name: string) {
5452
const escapedName = escapeRegExp(name)
5553
return new RegExp(
5654
// Detects the package name in the following cases:
@@ -63,11 +61,25 @@ function includesYarn(lockSrc: string, name: string) {
6361
).test(lockSrc)
6462
}
6563

66-
export const lockfileIncludesByAgent = new Map<Agent, AgentLockIncludesFn>([
67-
[BUN, includesBun],
68-
[NPM, includesNpm],
69-
[PNPM, includesPnpm],
70-
[VLT, includesVlt],
71-
[YARN_BERRY, includesYarn],
72-
[YARN_CLASSIC, includesYarn],
73-
])
64+
export function lockSrcIncludes(
65+
pkgEnvDetails: EnvDetails,
66+
lockSrc: string,
67+
name: string,
68+
lockName?: string | undefined,
69+
): boolean {
70+
switch (pkgEnvDetails.agent) {
71+
case BUN:
72+
return bunLockSrcIncludes(lockSrc, name, lockName)
73+
case PNPM:
74+
return pnpmLockSrcIncludes(lockSrc, name)
75+
case VLT:
76+
return vltLockSrcIncludes(lockSrc, name)
77+
case YARN_BERRY:
78+
return yarnLockSrcIncludes(lockSrc, name)
79+
case YARN_CLASSIC:
80+
return yarnLockSrcIncludes(lockSrc, name)
81+
case NPM:
82+
default:
83+
return npmLockSrcIncludes(lockSrc, name)
84+
}
85+
}

src/commands/optimize/ls-by-agent.mts

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { spawn } from '@socketsecurity/registry/lib/spawn'
22

33
import constants from '../../constants.mts'
44

5-
import type { Agent, EnvDetails } from '../../utils/package-environment.mts'
5+
import type { EnvDetails } from '../../utils/package-environment.mts'
66

77
const { BUN, NPM, PNPM, VLT, YARN_BERRY, YARN_CLASSIC } = constants
88

@@ -58,7 +58,14 @@ async function npmQuery(npmExecPath: string, cwd: string): Promise<string> {
5858
return cleanupQueryStdout(stdout)
5959
}
6060

61-
async function lsBun(pkgEnvDetails: EnvDetails, cwd: string): Promise<string> {
61+
export async function lsBun(
62+
pkgEnvDetails: EnvDetails,
63+
options?: AgentListDepsOptions | undefined,
64+
): Promise<string> {
65+
const { cwd = process.cwd() } = {
66+
__proto__: null,
67+
...options,
68+
} as AgentListDepsOptions
6269
try {
6370
// Bun does not support filtering by production packages yet.
6471
// https://github.com/oven-sh/bun/issues/8283
@@ -73,16 +80,25 @@ async function lsBun(pkgEnvDetails: EnvDetails, cwd: string): Promise<string> {
7380
return ''
7481
}
7582

76-
async function lsNpm(pkgEnvDetails: EnvDetails, cwd: string): Promise<string> {
83+
export async function lsNpm(
84+
pkgEnvDetails: EnvDetails,
85+
options?: AgentListDepsOptions | undefined,
86+
): Promise<string> {
87+
const { cwd = process.cwd() } = {
88+
__proto__: null,
89+
...options,
90+
} as AgentListDepsOptions
7791
return await npmQuery(pkgEnvDetails.agentExecPath, cwd)
7892
}
7993

80-
async function lsPnpm(
94+
export async function lsPnpm(
8195
pkgEnvDetails: EnvDetails,
82-
cwd: string,
8396
options?: AgentListDepsOptions | undefined,
8497
): Promise<string> {
85-
const npmExecPath = options?.npmExecPath
98+
const { cwd = process.cwd(), npmExecPath } = {
99+
__proto__: null,
100+
...options,
101+
} as AgentListDepsOptions
86102
if (npmExecPath && npmExecPath !== NPM) {
87103
const result = await npmQuery(npmExecPath, cwd)
88104
if (result) {
@@ -108,7 +124,14 @@ async function lsPnpm(
108124
return parsableToQueryStdout(stdout)
109125
}
110126

111-
async function lsVlt(pkgEnvDetails: EnvDetails, cwd: string): Promise<string> {
127+
export async function lsVlt(
128+
pkgEnvDetails: EnvDetails,
129+
options?: AgentListDepsOptions | undefined,
130+
): Promise<string> {
131+
const { cwd = process.cwd() } = {
132+
__proto__: null,
133+
...options,
134+
} as AgentListDepsOptions
112135
let stdout = ''
113136
try {
114137
// See https://docs.vlt.sh/cli/commands/list#options.
@@ -127,10 +150,14 @@ async function lsVlt(pkgEnvDetails: EnvDetails, cwd: string): Promise<string> {
127150
return cleanupQueryStdout(stdout)
128151
}
129152

130-
async function lsYarnBerry(
153+
export async function lsYarnBerry(
131154
pkgEnvDetails: EnvDetails,
132-
cwd: string,
155+
options?: AgentListDepsOptions | undefined,
133156
): Promise<string> {
157+
const { cwd = process.cwd() } = {
158+
__proto__: null,
159+
...options,
160+
} as AgentListDepsOptions
134161
try {
135162
// Yarn Berry does not support filtering by production packages yet.
136163
// https://github.com/yarnpkg/berry/issues/5117
@@ -149,10 +176,14 @@ async function lsYarnBerry(
149176
return ''
150177
}
151178

152-
async function lsYarnClassic(
179+
export async function lsYarnClassic(
153180
pkgEnvDetails: EnvDetails,
154-
cwd: string,
181+
options?: AgentListDepsOptions | undefined,
155182
): Promise<string> {
183+
const { cwd = process.cwd() } = {
184+
__proto__: null,
185+
...options,
186+
} as AgentListDepsOptions
156187
try {
157188
// However, Yarn Classic does support it.
158189
// https://github.com/yarnpkg/yarn/releases/tag/v1.0.0
@@ -169,19 +200,28 @@ async function lsYarnClassic(
169200
return ''
170201
}
171202

172-
export type AgentListDepsOptions = { npmExecPath?: string }
203+
export type AgentListDepsOptions = {
204+
cwd?: string | undefined
205+
npmExecPath?: string | undefined
206+
}
173207

174-
export type AgentListDepsFn = (
208+
export async function listPackages(
175209
pkgEnvDetails: EnvDetails,
176-
cwd: string,
177210
options?: AgentListDepsOptions | undefined,
178-
) => Promise<string>
179-
180-
export const lsByAgent = new Map<Agent, AgentListDepsFn>([
181-
[BUN, lsBun],
182-
[NPM, lsNpm],
183-
[PNPM, lsPnpm],
184-
[VLT, lsVlt],
185-
[YARN_BERRY, lsYarnBerry],
186-
[YARN_CLASSIC, lsYarnClassic],
187-
])
211+
): Promise<string> {
212+
switch (pkgEnvDetails.agent) {
213+
case BUN:
214+
return await lsBun(pkgEnvDetails, options)
215+
case PNPM:
216+
return await lsPnpm(pkgEnvDetails, options)
217+
case VLT:
218+
return await lsVlt(pkgEnvDetails, options)
219+
case YARN_BERRY:
220+
return await lsYarnBerry(pkgEnvDetails, options)
221+
case YARN_CLASSIC:
222+
return await lsYarnClassic(pkgEnvDetails, options)
223+
case NPM:
224+
default:
225+
return await lsNpm(pkgEnvDetails, options)
226+
}
227+
}

0 commit comments

Comments
 (0)