Skip to content

Commit 7144bc1

Browse files
committed
Cleanup agent specific actions
1 parent 16eec24 commit 7144bc1

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/commands/optimize.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const getOverridesDataByAgent: Record<Agent, GetOverrides> = {
9090
type AgentLockIncludesFn = (lockSrc: string, name: string) => boolean
9191

9292
const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
93-
const yarn = (lockSrc: string, name: string) => {
93+
function yarnLockIncludes(lockSrc: string, name: string) {
9494
const escapedName = escapeRegExp(name)
9595
return new RegExp(
9696
// Detects the package name in the following cases:
@@ -102,8 +102,9 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
102102
'm'
103103
).test(lockSrc)
104104
}
105+
105106
return {
106-
bun: yarn,
107+
bun: yarnLockIncludes,
107108
npm(lockSrc: string, name: string) {
108109
// Detects the package name in the following cases:
109110
// "name":
@@ -126,8 +127,8 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
126127
// "name"
127128
return lockSrc.includes(`"${name}"`)
128129
},
129-
'yarn/berry': yarn,
130-
'yarn/classic': yarn
130+
'yarn/berry': yarnLockIncludes,
131+
'yarn/classic': yarnLockIncludes
131132
}
132133
})()
133134

@@ -142,6 +143,7 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
142143
[OVERRIDES_FIELD_NAME]: overrides
143144
})
144145
}
146+
145147
function updateResolutions(
146148
pkgJson: EditablePackageJson,
147149
overrides: Overrides
@@ -150,6 +152,7 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
150152
[RESOLUTIONS_FIELD_NAME]: <PnpmOrYarnOverrides>overrides
151153
})
152154
}
155+
153156
return {
154157
bun: updateResolutions,
155158
npm: updateOverrides,
@@ -201,6 +204,17 @@ const lsByAgent = (() => {
201204
return JSON.stringify([...names], null, 2)
202205
}
203206

207+
function parseableToQueryStdout(stdout: string) {
208+
if (stdout === '') {
209+
return ''
210+
}
211+
// Convert the parseable stdout into a json array of unique names.
212+
// The matchAll regexp looks for a forward (posix) or backward (win32) slash
213+
// and matches one or more non-slashes until the newline.
214+
const names = new Set(stdout.matchAll(/(?<=[/\\])[^/\\]+(?=\n)/g))
215+
return JSON.stringify([...names], null, 2)
216+
}
217+
204218
async function npmQuery(npmExecPath: string, cwd: string): Promise<string> {
205219
let stdout = ''
206220
try {
@@ -238,19 +252,17 @@ const lsByAgent = (() => {
238252
return result
239253
}
240254
}
255+
let stdout = ''
241256
try {
242-
const { stdout } = await spawn(
243-
agentExecPath,
244-
['ls', '--parseable', '--prod', '--depth', 'Infinity'],
245-
{ cwd }
246-
)
247-
// Convert the parseable stdout into a json array of unique names.
248-
// The matchAll regexp looks for forward or backward slash followed by
249-
// one or more non-slashes until the newline.
250-
const names = new Set(stdout.matchAll(/(?<=[/\\])[^/\\]+(?=\n)/g))
251-
return JSON.stringify([...names], null, 2)
257+
stdout = (
258+
await spawn(
259+
agentExecPath,
260+
['ls', '--parseable', '--prod', '--depth', 'Infinity'],
261+
{ cwd }
262+
)
263+
).stdout
252264
} catch {}
253-
return ''
265+
return parseableToQueryStdout(stdout)
254266
},
255267
async vlt(agentExecPath: string, cwd: string) {
256268
let stdout = ''
@@ -294,14 +306,24 @@ const lsByAgent = (() => {
294306

295307
type AgentDepsIncludesFn = (stdout: string, name: string) => boolean
296308

297-
const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = {
298-
bun: (stdout: string, name: string) => stdout.includes(` ${name}@`),
299-
npm: (stdout: string, name: string) => stdout.includes(`"${name}"`),
300-
pnpm: (stdout: string, name: string) => stdout.includes(`"${name}"`),
301-
vlt: (stdout: string, name: string) => stdout.includes(` ${name}@`),
302-
'yarn/berry': (stdout: string, name: string) => stdout.includes(` ${name}@`),
303-
'yarn/classic': (stdout: string, name: string) => stdout.includes(` ${name}@`)
304-
}
309+
const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = (() => {
310+
function matchHumanStdout(stdout: string, name: string) {
311+
return stdout.includes(` ${name}@`)
312+
}
313+
314+
function matchQueryStdout(stdout: string, name: string) {
315+
return stdout.includes(`"${name}"`)
316+
}
317+
318+
return {
319+
bun: matchHumanStdout,
320+
npm: matchQueryStdout,
321+
pnpm: matchQueryStdout,
322+
vlt: matchQueryStdout,
323+
'yarn/berry': matchHumanStdout,
324+
'yarn/classic': matchHumanStdout
325+
}
326+
})()
305327

306328
function getDependencyEntries(pkgJson: PackageJsonContent) {
307329
const {

0 commit comments

Comments
 (0)