@@ -153,47 +153,50 @@ type AgentListDepsFn = (
153
153
const lsByAgent : Record < AgentPlusBun , AgentListDepsFn > = {
154
154
async bun ( agentExecPath : string , cwd : string , _rootPath : string ) {
155
155
try {
156
+ // Bun does not support filtering by production packages yet.
157
+ // https://github.com/oven-sh/bun/issues/8283
156
158
return ( await spawn ( agentExecPath , [ 'pm' , 'ls' , '--all' ] , { cwd } ) ) . stdout
157
159
} catch { }
158
160
return ''
159
161
} ,
160
162
async npm ( agentExecPath : string , cwd : string , rootPath : string ) {
161
163
try {
162
- ; (
163
- await spawn (
164
- agentExecPath ,
165
- [ 'ls' , '--parseable' , '--include' , 'prod' , '--all' ] ,
166
- { cwd }
167
- )
168
- ) . stdout
169
- . replaceAll ( cwd , '' )
170
- . replaceAll ( rootPath , '' )
164
+ let { stdout } = await spawn (
165
+ agentExecPath ,
166
+ [ 'ls' , '--parseable' , '--omit' , 'dev' , '--all' ] ,
167
+ { cwd }
168
+ )
169
+ stdout = stdout . replaceAll ( cwd , '' )
170
+ return rootPath === cwd ? stdout : stdout . replaceAll ( rootPath , '' )
171
171
} catch { }
172
172
return ''
173
173
} ,
174
174
async pnpm ( agentExecPath : string , cwd : string , rootPath : string ) {
175
175
try {
176
- return (
177
- await spawn (
178
- agentExecPath ,
179
- [ 'ls' , '--parseable' , '--prod' , '--depth' , 'Infinity' ] ,
180
- { cwd }
181
- )
182
- ) . stdout
183
- . replaceAll ( cwd , '' )
184
- . replaceAll ( rootPath , '' )
176
+ let { stdout } = await spawn (
177
+ agentExecPath ,
178
+ [ 'ls' , '--parseable' , '--prod' , '--depth' , 'Infinity' ] ,
179
+ { cwd }
180
+ )
181
+ stdout = stdout . replaceAll ( cwd , '' )
182
+ return rootPath === cwd ? stdout : stdout . replaceAll ( rootPath , '' )
185
183
} catch { }
186
184
return ''
187
185
} ,
188
186
async yarn ( agentExecPath : string , cwd : string , _rootPath : string ) {
189
187
try {
190
188
return (
191
- await spawn ( agentExecPath , [ 'info' , '--recursive' , '--name-only' ] , {
192
- cwd
193
- } )
194
- ) . stdout
189
+ // Yarn Berry does not support filtering by production packages yet.
190
+ // https://github.com/yarnpkg/berry/issues/5117
191
+ (
192
+ await spawn ( agentExecPath , [ 'info' , '--recursive' , '--name-only' ] , {
193
+ cwd
194
+ } )
195
+ ) . stdout
196
+ )
195
197
} catch { }
196
198
try {
199
+ // However, Yarn Classic does support it.
197
200
return ( await spawn ( agentExecPath , [ 'list' , '--prod' ] , { cwd } ) ) . stdout
198
201
} catch { }
199
202
return ''
@@ -291,7 +294,8 @@ type AddOverridesConfig = {
291
294
manifestEntries : ManifestEntry [ ]
292
295
pkgJson ?: EditablePackageJson | undefined
293
296
pkgPath : string
294
- pin : boolean
297
+ pin ?: boolean | undefined
298
+ prod ?: boolean | undefined
295
299
rootPath : string
296
300
}
297
301
@@ -306,9 +310,10 @@ async function addOverrides(
306
310
agentExecPath,
307
311
lockSrc,
308
312
manifestEntries,
313
+ pin,
309
314
pkgJson : editablePkgJson ,
310
315
pkgPath,
311
- pin ,
316
+ prod ,
312
317
rootPath
313
318
} : AddOverridesConfig ,
314
319
state : AddOverridesState = {
@@ -321,10 +326,11 @@ async function addOverrides(
321
326
}
322
327
const pkgJson : Readonly < PackageJsonContent > = editablePkgJson . content
323
328
const isRoot = pkgPath === rootPath
324
- const thingToScan = isRoot
329
+ const isLockScanned = isRoot && ! prod
330
+ const thingToScan = isLockScanned
325
331
? lockSrc
326
332
: await lsByAgent [ agent ] ( agentExecPath , pkgPath , rootPath )
327
- const thingScanner = isRoot
333
+ const thingScanner = isLockScanned
328
334
? lockIncludesByAgent [ agent ]
329
335
: depsIncludesByAgent [ agent ]
330
336
const depEntries = getDependencyEntries ( pkgJson )
@@ -377,7 +383,7 @@ async function addOverrides(
377
383
const oldSpec = overrideExists ? overrides [ origPkgName ] : undefined
378
384
const depAlias = depAliasMap . get ( origPkgName )
379
385
const regSpecStartsLike = `npm:${ regPkgName } @`
380
- let newSpec = `${ regSpecStartsLike } ${ pin ? version : `^ ${ major } ` } `
386
+ let newSpec = `${ regSpecStartsLike } ^ ${ pin ? version : major } `
381
387
let thisVersion = version
382
388
if ( depAlias && type === 'npm' ) {
383
389
// With npm one may not set an override for a package that one directly
@@ -401,7 +407,7 @@ async function addOverrides(
401
407
? version
402
408
: ( ( await fetchPackageManifest ( thisSpec ) ) ?. version ?? version )
403
409
}
404
- newSpec = `${ regSpecStartsLike } ${ pin ? thisVersion : `^ ${ semver . major ( thisVersion ) } ` } `
410
+ newSpec = `${ regSpecStartsLike } ^ ${ pin ? thisVersion : semver . major ( thisVersion ) } `
405
411
} else {
406
412
newSpec = oldSpec
407
413
}
@@ -434,6 +440,7 @@ async function addOverrides(
434
440
manifestEntries,
435
441
pin,
436
442
pkgPath : path . dirname ( wsPkgJsonPath ) ,
443
+ prod,
437
444
rootPath
438
445
} )
439
446
for ( const regPkgName of added ) {
@@ -494,7 +501,7 @@ export const optimize: CliSubcommand = {
494
501
if ( ! commandContext ) {
495
502
return
496
503
}
497
- const { pin } = commandContext
504
+ const { pin, prod } = commandContext
498
505
const cwd = process . cwd ( )
499
506
const {
500
507
agent,
@@ -551,6 +558,7 @@ export const optimize: CliSubcommand = {
551
558
pin,
552
559
pkgJson,
553
560
pkgPath,
561
+ prod,
554
562
rootPath : pkgPath
555
563
} ,
556
564
state
@@ -606,6 +614,7 @@ export const optimize: CliSubcommand = {
606
614
607
615
type CommandContext = {
608
616
pin : boolean
617
+ prod : boolean
609
618
}
610
619
611
620
function setupCommand (
@@ -620,6 +629,11 @@ function setupCommand(
620
629
type : 'boolean' ,
621
630
default : false ,
622
631
description : 'Pin overrides to their latest version'
632
+ } ,
633
+ prod : {
634
+ type : 'boolean' ,
635
+ default : false ,
636
+ description : 'Only add overrides for production dependencies'
623
637
}
624
638
}
625
639
const cli = meow (
@@ -640,12 +654,13 @@ function setupCommand(
640
654
flags
641
655
}
642
656
)
643
- const { help, pin } = cli . flags
657
+ const { help, pin, prod } = cli . flags
644
658
if ( help ) {
645
659
cli . showHelp ( )
646
660
return
647
661
}
648
662
return < CommandContext > {
649
- pin
663
+ pin,
664
+ prod
650
665
}
651
666
}
0 commit comments