@@ -325,6 +325,14 @@ const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = (() => {
325
325
}
326
326
} ) ( )
327
327
328
+ function createActionMessage (
329
+ verb : string ,
330
+ overrideCount : number ,
331
+ workspaceCount : number
332
+ ) {
333
+ return `${ verb } ${ overrideCount } Socket.dev optimized overrides${ workspaceCount ? ` in ${ workspaceCount } workspace${ workspaceCount > 1 ? 's' : '' } ` : '' } `
334
+ }
335
+
328
336
function getDependencyEntries ( pkgJson : PackageJsonContent ) {
329
337
const {
330
338
dependencies,
@@ -354,36 +362,44 @@ function getDependencyEntries(pkgJson: PackageJsonContent) {
354
362
] . filter ( ( { 1 : o } ) => o )
355
363
}
356
364
357
- async function getWorkspaces (
365
+ async function getWorkspaceGlobs (
358
366
agent : Agent ,
359
367
pkgPath : string ,
360
368
pkgJson : PackageJsonContent
361
369
) : Promise < string [ ] | undefined > {
362
- if ( agent !== 'pnpm' ) {
363
- return Array . isArray ( pkgJson [ 'workspaces' ] )
364
- ? < string [ ] > pkgJson [ 'workspaces' ] . filter ( isNonEmptyString )
365
- : undefined
366
- }
367
- for ( const workspacePath of [
368
- path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yaml` ) ,
369
- path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yml` )
370
- ] ) {
371
- if ( existsSync ( workspacePath ) ) {
372
- let packages
373
- try {
374
- // eslint-disable-next-line no-await-in-loop
375
- packages = yamlParse ( await fs . readFile ( workspacePath , 'utf8' ) ) ?. packages
376
- } catch { }
377
- if ( Array . isArray ( packages ) ) {
378
- return packages . filter ( isNonEmptyString )
370
+ let workspacePatterns
371
+ if ( agent === 'pnpm' ) {
372
+ for ( const workspacePath of [
373
+ path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yaml` ) ,
374
+ path . join ( pkgPath ! , `${ PNPM_WORKSPACE } .yml` )
375
+ ] ) {
376
+ if ( existsSync ( workspacePath ) ) {
377
+ try {
378
+ workspacePatterns = yamlParse (
379
+ // eslint-disable-next-line no-await-in-loop
380
+ await fs . readFile ( workspacePath , 'utf8' )
381
+ ) ?. packages
382
+ } catch { }
383
+ if ( workspacePatterns ) {
384
+ break
385
+ }
379
386
}
380
387
}
388
+ } else {
389
+ workspacePatterns = pkgJson [ 'workspaces' ]
381
390
}
382
- return undefined
391
+ return Array . isArray ( workspacePatterns )
392
+ ? workspacePatterns
393
+ . filter ( isNonEmptyString )
394
+ . map ( workspacePatternToGlobPattern )
395
+ : undefined
383
396
}
384
397
385
- function workspaceToGlobPattern ( workspace : string ) : string {
398
+ function workspacePatternToGlobPattern ( workspace : string ) : string {
386
399
const { length } = workspace
400
+ if ( ! length ) {
401
+ return ''
402
+ }
387
403
// If the workspace ends with "/"
388
404
if ( workspace . charCodeAt ( length - 1 ) === 47 /*'/'*/ ) {
389
405
return `${ workspace } /*/package.json`
@@ -415,16 +431,20 @@ type AddOverridesConfig = {
415
431
416
432
type AddOverridesState = {
417
433
added : Set < string >
434
+ addedInWorkspaces : Set < string >
418
435
spinner ?: Ora | undefined
419
436
updated : Set < string >
437
+ updatedInWorkspaces : Set < string >
420
438
warnedPnpmWorkspaceRequiresNpm : boolean
421
439
}
422
440
423
441
function createAddOverridesState ( initials ?: any ) : AddOverridesState {
424
442
return {
425
443
added : new Set ( ) ,
444
+ addedInWorkspaces : new Set ( ) ,
426
445
spinner : undefined ,
427
446
updated : new Set ( ) ,
447
+ updatedInWorkspaces : new Set ( ) ,
428
448
warnedPnpmWorkspaceRequiresNpm : false ,
429
449
...initials
430
450
}
@@ -452,9 +472,9 @@ async function addOverrides(
452
472
const pkgJson : Readonly < PackageJsonContent > = editablePkgJson . content
453
473
const isRoot = pkgPath === rootPath
454
474
const isLockScanned = isRoot && ! prod
455
- const relPath = path . relative ( rootPath , pkgPath )
456
- const workspaces = await getWorkspaces ( agent , pkgPath , pkgJson )
457
- const isWorkspace = ! ! workspaces
475
+ const workspaceName = path . relative ( rootPath , pkgPath )
476
+ const workspaceGlobs = await getWorkspaceGlobs ( agent , pkgPath , pkgJson )
477
+ const isWorkspace = ! ! workspaceGlobs
458
478
if (
459
479
isWorkspace &&
460
480
agent === 'pnpm' &&
@@ -484,7 +504,7 @@ async function addOverrides(
484
504
)
485
505
}
486
506
if ( spinner ) {
487
- spinner . text = `Adding overrides${ relPath ? ` to ${ relPath } ` : '' } ...`
507
+ spinner . text = `Adding overrides${ workspaceName ? ` to ${ workspaceName } ` : '' } ...`
488
508
}
489
509
const depAliasMap = new Map < string , { id : string ; version : string } > ( )
490
510
// Chunk package names to process them in parallel 3 at a time.
@@ -507,6 +527,7 @@ async function addOverrides(
507
527
pkgSpec = `${ regSpecStartsLike } ^${ version } `
508
528
depObj [ origPkgName ] = pkgSpec
509
529
state . added . add ( regPkgName )
530
+ state . addedInWorkspaces . add ( workspaceName )
510
531
}
511
532
depAliasMap . set ( origPkgName , {
512
533
id : pkgSpec ,
@@ -551,28 +572,27 @@ async function addOverrides(
551
572
}
552
573
}
553
574
if ( newSpec !== oldSpec ) {
575
+ overrides [ origPkgName ] = newSpec
554
576
if ( overrideExists ) {
555
577
state . updated . add ( regPkgName )
578
+ state . updatedInWorkspaces . add ( workspaceName )
556
579
} else {
557
580
state . added . add ( regPkgName )
581
+ state . addedInWorkspaces . add ( workspaceName )
558
582
}
559
- overrides [ origPkgName ] = newSpec
560
583
}
561
584
}
562
585
} )
563
586
} )
564
- if ( workspaces ) {
565
- const wsPkgJsonPaths = await tinyGlob (
566
- workspaces . map ( workspaceToGlobPattern ) ,
567
- {
568
- absolute : true ,
569
- cwd : pkgPath ! ,
570
- ignore : [ '**/node_modules/**' , '**/bower_components/**' ]
571
- }
572
- )
587
+ if ( workspaceGlobs ) {
588
+ const wsPkgJsonPaths = await tinyGlob ( workspaceGlobs , {
589
+ absolute : true ,
590
+ cwd : pkgPath ! ,
591
+ ignore : [ '**/node_modules/**' , '**/bower_components/**' ]
592
+ } )
573
593
// Chunk package names to process them in parallel 3 at a time.
574
594
await pEach ( wsPkgJsonPaths , 3 , async wsPkgJsonPath => {
575
- const { added , updated } = await addOverrides (
595
+ const otherState = await addOverrides (
576
596
{
577
597
agent,
578
598
agentExecPath,
@@ -586,11 +606,15 @@ async function addOverrides(
586
606
} ,
587
607
createAddOverridesState ( { spinner } )
588
608
)
589
- for ( const regPkgName of added ) {
590
- state . added . add ( regPkgName )
591
- }
592
- for ( const regPkgName of updated ) {
593
- state . updated . add ( regPkgName )
609
+ for ( const key of [
610
+ 'added' ,
611
+ 'addedInWorkspaces' ,
612
+ 'updated' ,
613
+ 'updatedInWorkspaces'
614
+ ] ) {
615
+ for ( const value of ( otherState as any ) [ key ] ) {
616
+ ; ( state as any ) [ key ] . add ( value )
617
+ }
594
618
}
595
619
} )
596
620
}
@@ -751,16 +775,18 @@ export const optimize: CliSubcommand = {
751
775
state
752
776
)
753
777
spinner . stop ( )
754
- const pkgJsonChanged = state . added . size > 0 || state . updated . size > 0
778
+ const addedCount = state . added . size
779
+ const updatedCount = state . updated . size
780
+ const pkgJsonChanged = addedCount > 0 || updatedCount > 0
755
781
if ( pkgJsonChanged ) {
756
- if ( state . updated . size > 0 ) {
782
+ if ( updatedCount > 0 ) {
757
783
console . log (
758
- `Updated ${ state . updated . size } Socket.dev optimized overrides ${ state . added . size ? '.' : '🚀' } `
784
+ `${ createActionMessage ( 'Updated' , updatedCount , state . updatedInWorkspaces . size ) } ${ addedCount ? '.' : '🚀' } `
759
785
)
760
786
}
761
- if ( state . added . size > 0 ) {
787
+ if ( addedCount > 0 ) {
762
788
console . log (
763
- `Added ${ state . added . size } Socket.dev optimized overrides 🚀`
789
+ `${ createActionMessage ( 'Added' , addedCount , state . addedInWorkspaces . size ) } 🚀`
764
790
)
765
791
}
766
792
} else {
0 commit comments