@@ -153,47 +153,50 @@ type AgentListDepsFn = (
153153const lsByAgent : Record < AgentPlusBun , AgentListDepsFn > = {
154154 async bun ( agentExecPath : string , cwd : string , _rootPath : string ) {
155155 try {
156+ // Bun does not support filtering by production packages yet.
157+ // https://github.com/oven-sh/bun/issues/8283
156158 return ( await spawn ( agentExecPath , [ 'pm' , 'ls' , '--all' ] , { cwd } ) ) . stdout
157159 } catch { }
158160 return ''
159161 } ,
160162 async npm ( agentExecPath : string , cwd : string , rootPath : string ) {
161163 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 , '' )
171171 } catch { }
172172 return ''
173173 } ,
174174 async pnpm ( agentExecPath : string , cwd : string , rootPath : string ) {
175175 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 , '' )
185183 } catch { }
186184 return ''
187185 } ,
188186 async yarn ( agentExecPath : string , cwd : string , _rootPath : string ) {
189187 try {
190188 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+ )
195197 } catch { }
196198 try {
199+ // However, Yarn Classic does support it.
197200 return ( await spawn ( agentExecPath , [ 'list' , '--prod' ] , { cwd } ) ) . stdout
198201 } catch { }
199202 return ''
@@ -291,7 +294,8 @@ type AddOverridesConfig = {
291294 manifestEntries : ManifestEntry [ ]
292295 pkgJson ?: EditablePackageJson | undefined
293296 pkgPath : string
294- pin : boolean
297+ pin ?: boolean | undefined
298+ prod ?: boolean | undefined
295299 rootPath : string
296300}
297301
@@ -306,9 +310,10 @@ async function addOverrides(
306310 agentExecPath,
307311 lockSrc,
308312 manifestEntries,
313+ pin,
309314 pkgJson : editablePkgJson ,
310315 pkgPath,
311- pin ,
316+ prod ,
312317 rootPath
313318 } : AddOverridesConfig ,
314319 state : AddOverridesState = {
@@ -321,10 +326,11 @@ async function addOverrides(
321326 }
322327 const pkgJson : Readonly < PackageJsonContent > = editablePkgJson . content
323328 const isRoot = pkgPath === rootPath
324- const thingToScan = isRoot
329+ const isLockScanned = isRoot && ! prod
330+ const thingToScan = isLockScanned
325331 ? lockSrc
326332 : await lsByAgent [ agent ] ( agentExecPath , pkgPath , rootPath )
327- const thingScanner = isRoot
333+ const thingScanner = isLockScanned
328334 ? lockIncludesByAgent [ agent ]
329335 : depsIncludesByAgent [ agent ]
330336 const depEntries = getDependencyEntries ( pkgJson )
@@ -377,7 +383,7 @@ async function addOverrides(
377383 const oldSpec = overrideExists ? overrides [ origPkgName ] : undefined
378384 const depAlias = depAliasMap . get ( origPkgName )
379385 const regSpecStartsLike = `npm:${ regPkgName } @`
380- let newSpec = `${ regSpecStartsLike } ${ pin ? version : `^ ${ major } ` } `
386+ let newSpec = `${ regSpecStartsLike } ^ ${ pin ? version : major } `
381387 let thisVersion = version
382388 if ( depAlias && type === 'npm' ) {
383389 // With npm one may not set an override for a package that one directly
@@ -401,7 +407,7 @@ async function addOverrides(
401407 ? version
402408 : ( ( await fetchPackageManifest ( thisSpec ) ) ?. version ?? version )
403409 }
404- newSpec = `${ regSpecStartsLike } ${ pin ? thisVersion : `^ ${ semver . major ( thisVersion ) } ` } `
410+ newSpec = `${ regSpecStartsLike } ^ ${ pin ? thisVersion : semver . major ( thisVersion ) } `
405411 } else {
406412 newSpec = oldSpec
407413 }
@@ -434,6 +440,7 @@ async function addOverrides(
434440 manifestEntries,
435441 pin,
436442 pkgPath : path . dirname ( wsPkgJsonPath ) ,
443+ prod,
437444 rootPath
438445 } )
439446 for ( const regPkgName of added ) {
@@ -494,7 +501,7 @@ export const optimize: CliSubcommand = {
494501 if ( ! commandContext ) {
495502 return
496503 }
497- const { pin } = commandContext
504+ const { pin, prod } = commandContext
498505 const cwd = process . cwd ( )
499506 const {
500507 agent,
@@ -551,6 +558,7 @@ export const optimize: CliSubcommand = {
551558 pin,
552559 pkgJson,
553560 pkgPath,
561+ prod,
554562 rootPath : pkgPath
555563 } ,
556564 state
@@ -606,6 +614,7 @@ export const optimize: CliSubcommand = {
606614
607615type CommandContext = {
608616 pin : boolean
617+ prod : boolean
609618}
610619
611620function setupCommand (
@@ -620,6 +629,11 @@ function setupCommand(
620629 type : 'boolean' ,
621630 default : false ,
622631 description : 'Pin overrides to their latest version'
632+ } ,
633+ prod : {
634+ type : 'boolean' ,
635+ default : false ,
636+ description : 'Only add overrides for production dependencies'
623637 }
624638 }
625639 const cli = meow (
@@ -640,12 +654,13 @@ function setupCommand(
640654 flags
641655 }
642656 )
643- const { help, pin } = cli . flags
657+ const { help, pin, prod } = cli . flags
644658 if ( help ) {
645659 cli . showHelp ( )
646660 return
647661 }
648662 return < CommandContext > {
649- pin
663+ pin,
664+ prod
650665 }
651666}
0 commit comments