@@ -43,7 +43,13 @@ import type { Spinner } from '@socketregistry/yocto-spinner'
43
43
type PackageJson = Awaited < ReturnType < typeof readPackageJson > >
44
44
45
45
const {
46
+ BUN ,
47
+ NPM ,
48
+ PNPM ,
46
49
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE ,
50
+ VLT ,
51
+ YARN_BERRY ,
52
+ YARN_CLASSIC ,
47
53
abortSignal,
48
54
execPath,
49
55
rootBinPath
@@ -52,11 +58,11 @@ const {
52
58
const COMMAND_TITLE = 'Socket Optimize'
53
59
const OVERRIDES_FIELD_NAME = 'overrides'
54
60
const 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`
57
63
const RESOLUTIONS_FIELD_NAME = 'resolutions'
58
64
59
- const manifestNpmOverrides = getManifestData ( 'npm' ) !
65
+ const manifestNpmOverrides = getManifestData ( NPM ) !
60
66
61
67
type NpmOverrides = { [ key : string ] : string | StringKeyValueObject }
62
68
type PnpmOrYarnOverrides = { [ key : string ] : string }
@@ -68,37 +74,37 @@ type GetOverridesResult = {
68
74
}
69
75
70
76
const getOverridesDataByAgent : Record < Agent , GetOverrides > = {
71
- bun ( pkgJson : PackageJson ) {
77
+ [ BUN ] ( pkgJson : PackageJson ) {
72
78
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
73
- return { type : 'yarn/berry' , overrides }
79
+ return { type : YARN_BERRY , overrides }
74
80
} ,
75
81
// npm overrides documentation:
76
82
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
77
- npm ( pkgJson : PackageJson ) {
83
+ [ NPM ] ( pkgJson : PackageJson ) {
78
84
const overrides = ( pkgJson as any ) ?. overrides ?? { }
79
- return { type : 'npm' , overrides }
85
+ return { type : NPM , overrides }
80
86
} ,
81
87
// pnpm overrides documentation:
82
88
// https://pnpm.io/package_json#pnpmoverrides
83
- pnpm ( pkgJson : PackageJson ) {
89
+ [ PNPM ] ( pkgJson : PackageJson ) {
84
90
const overrides = ( pkgJson as any ) ?. pnpm ?. overrides ?? { }
85
- return { type : 'pnpm' , overrides }
91
+ return { type : PNPM , overrides }
86
92
} ,
87
- vlt ( pkgJson : PackageJson ) {
93
+ [ VLT ] ( pkgJson : PackageJson ) {
88
94
const overrides = ( pkgJson as any ) ?. overrides ?? { }
89
- return { type : 'vlt' , overrides }
95
+ return { type : VLT , overrides }
90
96
} ,
91
97
// Yarn resolutions documentation:
92
98
// https://yarnpkg.com/configuration/manifest#resolutions
93
- 'yarn/berry' ( pkgJson : PackageJson ) {
99
+ [ YARN_BERRY ] ( pkgJson : PackageJson ) {
94
100
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
95
- return { type : 'yarn/berry' , overrides }
101
+ return { type : YARN_BERRY , overrides }
96
102
} ,
97
103
// Yarn resolutions documentation:
98
104
// https://classic.yarnpkg.com/en/docs/selective-version-resolutions
99
- 'yarn/classic' ( pkgJson : PackageJson ) {
105
+ [ YARN_CLASSIC ] ( pkgJson : PackageJson ) {
100
106
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
101
- return { type : 'yarn/classic' , overrides }
107
+ return { type : YARN_CLASSIC , overrides }
102
108
}
103
109
}
104
110
@@ -119,13 +125,13 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
119
125
}
120
126
121
127
return {
122
- bun : yarnLockIncludes ,
123
- npm ( lockSrc : string , name : string ) {
128
+ [ BUN ] : yarnLockIncludes ,
129
+ [ NPM ] ( lockSrc : string , name : string ) {
124
130
// Detects the package name in the following cases:
125
131
// "name":
126
132
return lockSrc . includes ( `"${ name } ":` )
127
133
} ,
128
- pnpm ( lockSrc : string , name : string ) {
134
+ [ PNPM ] ( lockSrc : string , name : string ) {
129
135
const escapedName = escapeRegExp ( name )
130
136
return new RegExp (
131
137
// Detects the package name in the following cases:
@@ -137,13 +143,13 @@ const lockIncludesByAgent: Record<Agent, AgentLockIncludesFn> = (() => {
137
143
'm'
138
144
) . test ( lockSrc )
139
145
} ,
140
- vlt ( lockSrc : string , name : string ) {
146
+ [ VLT ] ( lockSrc : string , name : string ) {
141
147
// Detects the package name in the following cases:
142
148
// "name"
143
149
return lockSrc . includes ( `"${ name } "` )
144
150
} ,
145
- 'yarn/berry' : yarnLockIncludes ,
146
- 'yarn/classic' : yarnLockIncludes
151
+ [ YARN_BERRY ] : yarnLockIncludes ,
152
+ [ YARN_CLASSIC ] : yarnLockIncludes
147
153
}
148
154
} ) ( )
149
155
@@ -247,14 +253,14 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
247
253
insertIndex = getLowestEntryIndex ( entries , [ 'resolutions' ] )
248
254
if ( insertIndex === - 1 ) {
249
255
isPlacingHigher = true
250
- insertIndex = getHighestEntryIndex ( entries , [ ...depFields , 'pnpm' ] )
256
+ insertIndex = getHighestEntryIndex ( entries , [ ...depFields , PNPM ] )
251
257
}
252
258
} else if ( field === RESOLUTIONS_FIELD_NAME ) {
253
259
isPlacingHigher = true
254
260
insertIndex = getHighestEntryIndex ( entries , [
255
261
...depFields ,
256
262
'overrides' ,
257
- 'pnpm'
263
+ PNPM
258
264
] )
259
265
} else if ( field === PNPM_FIELD_NAME ) {
260
266
insertIndex = getLowestEntryIndex ( entries , [ 'overrides' , 'resolutions' ] )
@@ -304,14 +310,14 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
304
310
}
305
311
306
312
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 ) {
310
316
updatePkgJson ( editablePkgJson , PNPM_FIELD_NAME , overrides )
311
317
} ,
312
- vlt : updateOverrides ,
313
- 'yarn/berry' : updateResolutions ,
314
- 'yarn/classic' : updateResolutions
318
+ [ VLT ] : updateOverrides ,
319
+ [ YARN_BERRY ] : updateResolutions ,
320
+ [ YARN_CLASSIC ] : updateResolutions
315
321
}
316
322
} ) ( )
317
323
@@ -373,7 +379,7 @@ const lsByAgent = (() => {
373
379
}
374
380
375
381
return < Record < Agent , AgentListDepsFn > > {
376
- async bun ( agentExecPath : string , cwd : string ) {
382
+ async [ BUN ] ( agentExecPath : string , cwd : string ) {
377
383
try {
378
384
// Bun does not support filtering by production packages yet.
379
385
// https://github.com/oven-sh/bun/issues/8283
@@ -382,10 +388,10 @@ const lsByAgent = (() => {
382
388
} catch { }
383
389
return ''
384
390
} ,
385
- async npm ( agentExecPath : string , cwd : string ) {
391
+ async [ NPM ] ( agentExecPath : string , cwd : string ) {
386
392
return await npmQuery ( agentExecPath , cwd )
387
393
} ,
388
- async pnpm (
394
+ async [ PNPM ] (
389
395
agentExecPath : string ,
390
396
cwd : string ,
391
397
options : AgentListDepsOptions
@@ -394,7 +400,7 @@ const lsByAgent = (() => {
394
400
__proto__ : null ,
395
401
...options
396
402
}
397
- if ( npmExecPath && npmExecPath !== 'npm' ) {
403
+ if ( npmExecPath && npmExecPath !== NPM ) {
398
404
const result = await npmQuery ( npmExecPath , cwd )
399
405
if ( result ) {
400
406
return result
@@ -412,7 +418,7 @@ const lsByAgent = (() => {
412
418
} catch { }
413
419
return parseableToQueryStdout ( stdout )
414
420
} ,
415
- async vlt ( agentExecPath : string , cwd : string ) {
421
+ async [ VLT ] ( agentExecPath : string , cwd : string ) {
416
422
let stdout = ''
417
423
try {
418
424
stdout = (
@@ -423,7 +429,7 @@ const lsByAgent = (() => {
423
429
} catch { }
424
430
return cleanupQueryStdout ( stdout )
425
431
} ,
426
- async 'yarn/berry' ( agentExecPath : string , cwd : string ) {
432
+ async [ YARN_BERRY ] ( agentExecPath : string , cwd : string ) {
427
433
try {
428
434
return (
429
435
// Yarn Berry does not support filtering by production packages yet.
@@ -437,7 +443,7 @@ const lsByAgent = (() => {
437
443
} catch { }
438
444
return ''
439
445
} ,
440
- async 'yarn/classic' ( agentExecPath : string , cwd : string ) {
446
+ async [ YARN_CLASSIC ] ( agentExecPath : string , cwd : string ) {
441
447
try {
442
448
// However, Yarn Classic does support it.
443
449
// https://github.com/yarnpkg/yarn/releases/tag/v1.0.0
@@ -464,12 +470,12 @@ const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = (() => {
464
470
}
465
471
466
472
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
473
479
}
474
480
} ) ( )
475
481
@@ -516,7 +522,7 @@ async function getWorkspaceGlobs(
516
522
pkgJson : PackageJson
517
523
) : Promise < string [ ] | undefined > {
518
524
let workspacePatterns
519
- if ( agent === 'pnpm' ) {
525
+ if ( agent === PNPM ) {
520
526
for ( const workspacePath of [
521
527
path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yaml` ) ,
522
528
path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yml` )
@@ -625,8 +631,8 @@ async function addOverrides(
625
631
const isWorkspace = ! ! workspaceGlobs
626
632
if (
627
633
isWorkspace &&
628
- agent === 'pnpm' &&
629
- npmExecPath === 'npm' &&
634
+ agent === PNPM &&
635
+ npmExecPath === NPM &&
630
636
! state . warnedPnpmWorkspaceRequiresNpm
631
637
) {
632
638
state . warnedPnpmWorkspaceRequiresNpm = true
@@ -647,8 +653,8 @@ async function addOverrides(
647
653
overridesDataObjects . push ( getOverridesDataByAgent [ agent ] ( pkgJson ) )
648
654
} else {
649
655
overridesDataObjects . push (
650
- getOverridesDataByAgent . npm ( pkgJson ) ,
651
- getOverridesDataByAgent [ 'yarn/classic' ] ( pkgJson )
656
+ getOverridesDataByAgent [ NPM ] ( pkgJson ) ,
657
+ getOverridesDataByAgent [ YARN_CLASSIC ] ( pkgJson )
652
658
)
653
659
}
654
660
if ( spinner ) {
@@ -692,10 +698,10 @@ async function addOverrides(
692
698
if ( overrideExists || thingScanner ( thingToScan , origPkgName ) ) {
693
699
const oldSpec = overrideExists ? overrides [ origPkgName ] : undefined
694
700
const depAlias = depAliasMap . get ( origPkgName )
695
- const regSpecStartsLike = `npm :${ regPkgName } @`
701
+ const regSpecStartsLike = `${ NPM } :${ regPkgName } @`
696
702
let newSpec = `${ regSpecStartsLike } ^${ pin ? version : major } `
697
703
let thisVersion = version
698
- if ( depAlias && type === 'npm' ) {
704
+ if ( depAlias && type === NPM ) {
699
705
// With npm one may not set an override for a package that one directly
700
706
// depends on unless both the dependency and the override itself share
701
707
// the exact same spec. To make this limitation easier to deal with,
@@ -815,7 +821,7 @@ export const optimize: CliSubcommand = {
815
821
)
816
822
return
817
823
}
818
- if ( agent === 'vlt' ) {
824
+ if ( agent === VLT ) {
819
825
console . error (
820
826
`✖️ ${ COMMAND_TITLE } : ${ agent } does not support overrides. Soon, though ⚡`
821
827
)
@@ -834,7 +840,7 @@ export const optimize: CliSubcommand = {
834
840
console . error ( `✖️ ${ COMMAND_TITLE } : No package.json found` )
835
841
return
836
842
}
837
- if ( prod && ( agent === 'bun' || agent === 'yarn/berry' ) ) {
843
+ if ( prod && ( agent === BUN || agent === YARN_BERRY ) ) {
838
844
console . error (
839
845
`✖️ ${ COMMAND_TITLE } : --prod not supported for ${ agent } ${ agentVersion ? `@${ agentVersion . toString ( ) } ` : '' } `
840
846
)
@@ -885,7 +891,7 @@ export const optimize: CliSubcommand = {
885
891
} else {
886
892
console . log ( 'Congratulations! Already Socket.dev optimized 🎉' )
887
893
}
888
- const isNpm = agent === 'npm'
894
+ const isNpm = agent === NPM
889
895
if ( isNpm || pkgJsonChanged ) {
890
896
// Always update package-lock.json until the npm overrides PR lands:
891
897
// https://github.com/npm/cli/pull/7025
0 commit comments