Skip to content

Commit 441050b

Browse files
committed
Add workspaces count to result log of socket optimize
1 parent 7144bc1 commit 441050b

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

src/commands/optimize.ts

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ const depsIncludesByAgent: Record<Agent, AgentDepsIncludesFn> = (() => {
325325
}
326326
})()
327327

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+
328336
function getDependencyEntries(pkgJson: PackageJsonContent) {
329337
const {
330338
dependencies,
@@ -354,36 +362,44 @@ function getDependencyEntries(pkgJson: PackageJsonContent) {
354362
].filter(({ 1: o }) => o)
355363
}
356364

357-
async function getWorkspaces(
365+
async function getWorkspaceGlobs(
358366
agent: Agent,
359367
pkgPath: string,
360368
pkgJson: PackageJsonContent
361369
): 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+
}
379386
}
380387
}
388+
} else {
389+
workspacePatterns = pkgJson['workspaces']
381390
}
382-
return undefined
391+
return Array.isArray(workspacePatterns)
392+
? workspacePatterns
393+
.filter(isNonEmptyString)
394+
.map(workspacePatternToGlobPattern)
395+
: undefined
383396
}
384397

385-
function workspaceToGlobPattern(workspace: string): string {
398+
function workspacePatternToGlobPattern(workspace: string): string {
386399
const { length } = workspace
400+
if (!length) {
401+
return ''
402+
}
387403
// If the workspace ends with "/"
388404
if (workspace.charCodeAt(length - 1) === 47 /*'/'*/) {
389405
return `${workspace}/*/package.json`
@@ -415,16 +431,20 @@ type AddOverridesConfig = {
415431

416432
type AddOverridesState = {
417433
added: Set<string>
434+
addedInWorkspaces: Set<string>
418435
spinner?: Ora | undefined
419436
updated: Set<string>
437+
updatedInWorkspaces: Set<string>
420438
warnedPnpmWorkspaceRequiresNpm: boolean
421439
}
422440

423441
function createAddOverridesState(initials?: any): AddOverridesState {
424442
return {
425443
added: new Set(),
444+
addedInWorkspaces: new Set(),
426445
spinner: undefined,
427446
updated: new Set(),
447+
updatedInWorkspaces: new Set(),
428448
warnedPnpmWorkspaceRequiresNpm: false,
429449
...initials
430450
}
@@ -452,9 +472,9 @@ async function addOverrides(
452472
const pkgJson: Readonly<PackageJsonContent> = editablePkgJson.content
453473
const isRoot = pkgPath === rootPath
454474
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
458478
if (
459479
isWorkspace &&
460480
agent === 'pnpm' &&
@@ -484,7 +504,7 @@ async function addOverrides(
484504
)
485505
}
486506
if (spinner) {
487-
spinner.text = `Adding overrides${relPath ? ` to ${relPath}` : ''}...`
507+
spinner.text = `Adding overrides${workspaceName ? ` to ${workspaceName}` : ''}...`
488508
}
489509
const depAliasMap = new Map<string, { id: string; version: string }>()
490510
// Chunk package names to process them in parallel 3 at a time.
@@ -507,6 +527,7 @@ async function addOverrides(
507527
pkgSpec = `${regSpecStartsLike}^${version}`
508528
depObj[origPkgName] = pkgSpec
509529
state.added.add(regPkgName)
530+
state.addedInWorkspaces.add(workspaceName)
510531
}
511532
depAliasMap.set(origPkgName, {
512533
id: pkgSpec,
@@ -551,28 +572,27 @@ async function addOverrides(
551572
}
552573
}
553574
if (newSpec !== oldSpec) {
575+
overrides[origPkgName] = newSpec
554576
if (overrideExists) {
555577
state.updated.add(regPkgName)
578+
state.updatedInWorkspaces.add(workspaceName)
556579
} else {
557580
state.added.add(regPkgName)
581+
state.addedInWorkspaces.add(workspaceName)
558582
}
559-
overrides[origPkgName] = newSpec
560583
}
561584
}
562585
})
563586
})
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+
})
573593
// Chunk package names to process them in parallel 3 at a time.
574594
await pEach(wsPkgJsonPaths, 3, async wsPkgJsonPath => {
575-
const { added, updated } = await addOverrides(
595+
const otherState = await addOverrides(
576596
{
577597
agent,
578598
agentExecPath,
@@ -586,11 +606,15 @@ async function addOverrides(
586606
},
587607
createAddOverridesState({ spinner })
588608
)
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+
}
594618
}
595619
})
596620
}
@@ -751,16 +775,18 @@ export const optimize: CliSubcommand = {
751775
state
752776
)
753777
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
755781
if (pkgJsonChanged) {
756-
if (state.updated.size > 0) {
782+
if (updatedCount > 0) {
757783
console.log(
758-
`Updated ${state.updated.size} Socket.dev optimized overrides ${state.added.size ? '.' : '🚀'}`
784+
`${createActionMessage('Updated', updatedCount, state.updatedInWorkspaces.size)}${addedCount ? '.' : '🚀'}`
759785
)
760786
}
761-
if (state.added.size > 0) {
787+
if (addedCount > 0) {
762788
console.log(
763-
`Added ${state.added.size} Socket.dev optimized overrides 🚀`
789+
`${createActionMessage('Added', addedCount, state.addedInWorkspaces.size)} 🚀`
764790
)
765791
}
766792
} else {

0 commit comments

Comments
 (0)