@@ -43,7 +43,13 @@ import type { Spinner } from '@socketregistry/yocto-spinner'
4343type PackageJson = Awaited < ReturnType < typeof readPackageJson > >
4444
4545const {
46+ BUN ,
47+ NPM ,
48+ PNPM ,
4649 UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE ,
50+ VLT ,
51+ YARN_BERRY ,
52+ YARN_CLASSIC ,
4753 abortSignal,
4854 execPath,
4955 rootBinPath
@@ -52,11 +58,11 @@ const {
5258const COMMAND_TITLE = 'Socket Optimize'
5359const OVERRIDES_FIELD_NAME = 'overrides'
5460const NPM_OVERRIDE_PR_URL = 'https://github.com/npm/cli/pull/7025'
55- const PNPM_FIELD_NAME = 'pnpm'
56- const PNPM_WORKSPACE = 'pnpm -workspace'
61+ const PNPM_FIELD_NAME = PNPM
62+ const PNPM_WORKSPACE = ` ${ PNPM } -workspace`
5763const RESOLUTIONS_FIELD_NAME = 'resolutions'
5864
59- const manifestNpmOverrides = getManifestData ( 'npm' ) !
65+ const manifestNpmOverrides = getManifestData ( NPM ) !
6066
6167type NpmOverrides = { [ key : string ] : string | StringKeyValueObject }
6268type PnpmOrYarnOverrides = { [ key : string ] : string }
@@ -68,37 +74,37 @@ type GetOverridesResult = {
6874}
6975
7076const getOverridesDataByAgent : Record < Agent , GetOverrides > = {
71- bun ( pkgJson : PackageJson ) {
77+ [ BUN ] ( pkgJson : PackageJson ) {
7278 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
73- return { type : 'yarn/berry' , overrides }
79+ return { type : YARN_BERRY , overrides }
7480 } ,
7581 // npm overrides documentation:
7682 // https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
77- npm ( pkgJson : PackageJson ) {
83+ [ NPM ] ( pkgJson : PackageJson ) {
7884 const overrides = ( pkgJson as any ) ?. overrides ?? { }
79- return { type : 'npm' , overrides }
85+ return { type : NPM , overrides }
8086 } ,
8187 // pnpm overrides documentation:
8288 // https://pnpm.io/package_json#pnpmoverrides
83- pnpm ( pkgJson : PackageJson ) {
89+ [ PNPM ] ( pkgJson : PackageJson ) {
8490 const overrides = ( pkgJson as any ) ?. pnpm ?. overrides ?? { }
85- return { type : 'pnpm' , overrides }
91+ return { type : PNPM , overrides }
8692 } ,
87- vlt ( pkgJson : PackageJson ) {
93+ [ VLT ] ( pkgJson : PackageJson ) {
8894 const overrides = ( pkgJson as any ) ?. overrides ?? { }
89- return { type : 'vlt' , overrides }
95+ return { type : VLT , overrides }
9096 } ,
9197 // Yarn resolutions documentation:
9298 // https://yarnpkg.com/configuration/manifest#resolutions
93- 'yarn/berry' ( pkgJson : PackageJson ) {
99+ [ YARN_BERRY ] ( pkgJson : PackageJson ) {
94100 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
95- return { type : 'yarn/berry' , overrides }
101+ return { type : YARN_BERRY , overrides }
96102 } ,
97103 // Yarn resolutions documentation:
98104 // https://classic.yarnpkg.com/en/docs/selective-version-resolutions
99- 'yarn/classic' ( pkgJson : PackageJson ) {
105+ [ YARN_CLASSIC ] ( pkgJson : PackageJson ) {
100106 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
101- return { type : 'yarn/classic' , overrides }
107+ return { type : YARN_CLASSIC , overrides }
102108 }
103109}
104110
@@ -119,13 +125,13 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
119125 }
120126
121127 return {
122- bun : yarnLockIncludes ,
123- npm ( lockSrc : string , name : string ) {
128+ [ BUN ] : yarnLockIncludes ,
129+ [ NPM ] ( lockSrc : string , name : string ) {
124130 // Detects the package name in the following cases:
125131 // "name":
126132 return lockSrc . includes ( `"${ name } ":` )
127133 } ,
128- pnpm ( lockSrc : string , name : string ) {
134+ [ PNPM ] ( lockSrc : string , name : string ) {
129135 const escapedName = escapeRegExp ( name )
130136 return new RegExp (
131137 // Detects the package name in the following cases:
@@ -137,13 +143,13 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
137143 'm'
138144 ) . test ( lockSrc )
139145 } ,
140- vlt ( lockSrc : string , name : string ) {
146+ [ VLT ] ( lockSrc : string , name : string ) {
141147 // Detects the package name in the following cases:
142148 // "name"
143149 return lockSrc . includes ( `"${ name } "` )
144150 } ,
145- 'yarn/berry' : yarnLockIncludes ,
146- 'yarn/classic' : yarnLockIncludes
151+ [ YARN_BERRY ] : yarnLockIncludes ,
152+ [ YARN_CLASSIC ] : yarnLockIncludes
147153 }
148154} ) ( )
149155
@@ -247,14 +253,14 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
247253 insertIndex = getLowestEntryIndex ( entries , [ 'resolutions' ] )
248254 if ( insertIndex === - 1 ) {
249255 isPlacingHigher = true
250- insertIndex = getHighestEntryIndex ( entries , [ ...depFields , 'pnpm' ] )
256+ insertIndex = getHighestEntryIndex ( entries , [ ...depFields , PNPM ] )
251257 }
252258 } else if ( field === RESOLUTIONS_FIELD_NAME ) {
253259 isPlacingHigher = true
254260 insertIndex = getHighestEntryIndex ( entries , [
255261 ...depFields ,
256262 'overrides' ,
257- 'pnpm'
263+ PNPM
258264 ] )
259265 } else if ( field === PNPM_FIELD_NAME ) {
260266 insertIndex = getLowestEntryIndex ( entries , [ 'overrides' , 'resolutions' ] )
@@ -304,14 +310,14 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
304310 }
305311
306312 return {
307- bun : updateResolutions ,
308- npm : updateOverrides ,
309- pnpm ( editablePkgJson : EditablePackageJson , overrides : Overrides ) {
313+ [ BUN ] : updateResolutions ,
314+ [ NPM ] : updateOverrides ,
315+ [ PNPM ] ( editablePkgJson : EditablePackageJson , overrides : Overrides ) {
310316 updatePkgJson ( editablePkgJson , PNPM_FIELD_NAME , overrides )
311317 } ,
312- vlt : updateOverrides ,
313- 'yarn/berry' : updateResolutions ,
314- 'yarn/classic' : updateResolutions
318+ [ VLT ] : updateOverrides ,
319+ [ YARN_BERRY ] : updateResolutions ,
320+ [ YARN_CLASSIC ] : updateResolutions
315321 }
316322} ) ( )
317323
@@ -373,7 +379,7 @@ const lsByAgent = (() => {
373379 }
374380
375381 return < Record < Agent , AgentListDepsFn > > {
376- async bun ( agentExecPath : string , cwd : string ) {
382+ async [ BUN ] ( agentExecPath : string , cwd : string ) {
377383 try {
378384 // Bun does not support filtering by production packages yet.
379385 // https://github.com/oven-sh/bun/issues/8283
@@ -382,10 +388,10 @@ const lsByAgent = (() => {
382388 } catch { }
383389 return ''
384390 } ,
385- async npm ( agentExecPath : string , cwd : string ) {
391+ async [ NPM ] ( agentExecPath : string , cwd : string ) {
386392 return await npmQuery ( agentExecPath , cwd )
387393 } ,
388- async pnpm (
394+ async [ PNPM ] (
389395 agentExecPath : string ,
390396 cwd : string ,
391397 options : AgentListDepsOptions
@@ -394,7 +400,7 @@ const lsByAgent = (() => {
394400 __proto__ : null ,
395401 ...options
396402 }
397- if ( npmExecPath && npmExecPath !== 'npm' ) {
403+ if ( npmExecPath && npmExecPath !== NPM ) {
398404 const result = await npmQuery ( npmExecPath , cwd )
399405 if ( result ) {
400406 return result
@@ -412,7 +418,7 @@ const lsByAgent = (() => {
412418 } catch { }
413419 return parseableToQueryStdout ( stdout )
414420 } ,
415- async vlt ( agentExecPath : string , cwd : string ) {
421+ async [ VLT ] ( agentExecPath : string , cwd : string ) {
416422 let stdout = ''
417423 try {
418424 stdout = (
@@ -423,7 +429,7 @@ const lsByAgent = (() => {
423429 } catch { }
424430 return cleanupQueryStdout ( stdout )
425431 } ,
426- async 'yarn/berry' ( agentExecPath : string , cwd : string ) {
432+ async [ YARN_BERRY ] ( agentExecPath : string , cwd : string ) {
427433 try {
428434 return (
429435 // Yarn Berry does not support filtering by production packages yet.
@@ -437,7 +443,7 @@ const lsByAgent = (() => {
437443 } catch { }
438444 return ''
439445 } ,
440- async 'yarn/classic' ( agentExecPath : string , cwd : string ) {
446+ async [ YARN_CLASSIC ] ( agentExecPath : string , cwd : string ) {
441447 try {
442448 // However, Yarn Classic does support it.
443449 // https://github.com/yarnpkg/yarn/releases/tag/v1.0.0
@@ -464,12 +470,12 @@ const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = (() => {
464470 }
465471
466472 return {
467- bun : matchHumanStdout ,
468- npm : matchQueryStdout ,
469- pnpm : matchQueryStdout ,
470- vlt : matchQueryStdout ,
471- 'yarn/berry' : matchHumanStdout ,
472- 'yarn/classic' : matchHumanStdout
473+ [ BUN ] : matchHumanStdout ,
474+ [ NPM ] : matchQueryStdout ,
475+ [ PNPM ] : matchQueryStdout ,
476+ [ VLT ] : matchQueryStdout ,
477+ [ YARN_BERRY ] : matchHumanStdout ,
478+ [ YARN_CLASSIC ] : matchHumanStdout
473479 }
474480} ) ( )
475481
@@ -516,7 +522,7 @@ async function getWorkspaceGlobs(
516522 pkgJson : PackageJson
517523) : Promise < string [ ] | undefined > {
518524 let workspacePatterns
519- if ( agent === 'pnpm' ) {
525+ if ( agent === PNPM ) {
520526 for ( const workspacePath of [
521527 path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yaml` ) ,
522528 path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yml` )
@@ -625,8 +631,8 @@ async function addOverrides(
625631 const isWorkspace = ! ! workspaceGlobs
626632 if (
627633 isWorkspace &&
628- agent === 'pnpm' &&
629- npmExecPath === 'npm' &&
634+ agent === PNPM &&
635+ npmExecPath === NPM &&
630636 ! state . warnedPnpmWorkspaceRequiresNpm
631637 ) {
632638 state . warnedPnpmWorkspaceRequiresNpm = true
@@ -647,8 +653,8 @@ async function addOverrides(
647653 overridesDataObjects . push ( getOverridesDataByAgent [ agent ] ( pkgJson ) )
648654 } else {
649655 overridesDataObjects . push (
650- getOverridesDataByAgent . npm ( pkgJson ) ,
651- getOverridesDataByAgent [ 'yarn/classic' ] ( pkgJson )
656+ getOverridesDataByAgent [ NPM ] ( pkgJson ) ,
657+ getOverridesDataByAgent [ YARN_CLASSIC ] ( pkgJson )
652658 )
653659 }
654660 if ( spinner ) {
@@ -692,10 +698,10 @@ async function addOverrides(
692698 if ( overrideExists || thingScanner ( thingToScan , origPkgName ) ) {
693699 const oldSpec = overrideExists ? overrides [ origPkgName ] : undefined
694700 const depAlias = depAliasMap . get ( origPkgName )
695- const regSpecStartsLike = `npm :${ regPkgName } @`
701+ const regSpecStartsLike = `${ NPM } :${ regPkgName } @`
696702 let newSpec = `${ regSpecStartsLike } ^${ pin ? version : major } `
697703 let thisVersion = version
698- if ( depAlias && type === 'npm' ) {
704+ if ( depAlias && type === NPM ) {
699705 // With npm one may not set an override for a package that one directly
700706 // depends on unless both the dependency and the override itself share
701707 // the exact same spec. To make this limitation easier to deal with,
@@ -815,7 +821,7 @@ export const optimize: CliSubcommand = {
815821 )
816822 return
817823 }
818- if ( agent === 'vlt' ) {
824+ if ( agent === VLT ) {
819825 console . error (
820826 `✖️ ${ COMMAND_TITLE } : ${ agent } does not support overrides. Soon, though ⚡`
821827 )
@@ -834,7 +840,7 @@ export const optimize: CliSubcommand = {
834840 console . error ( `✖️ ${ COMMAND_TITLE } : No package.json found` )
835841 return
836842 }
837- if ( prod && ( agent === 'bun' || agent === 'yarn/berry' ) ) {
843+ if ( prod && ( agent === BUN || agent === YARN_BERRY ) ) {
838844 console . error (
839845 `✖️ ${ COMMAND_TITLE } : --prod not supported for ${ agent } ${ agentVersion ? `@${ agentVersion . toString ( ) } ` : '' } `
840846 )
@@ -885,7 +891,7 @@ export const optimize: CliSubcommand = {
885891 } else {
886892 console . log ( 'Congratulations! Already Socket.dev optimized 🎉' )
887893 }
888- const isNpm = agent === 'npm'
894+ const isNpm = agent === NPM
889895 if ( isNpm || pkgJsonChanged ) {
890896 // Always update package-lock.json until the npm overrides PR lands:
891897 // https://github.com/npm/cli/pull/7025
0 commit comments