mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-30 10:38:13 -05:00
fix: correctly apply cleanupUnusedCatalogs when remove pkg (#10005)
close #9993 --------- Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
6
.changeset/lazy-doodles-lie.md
Normal file
6
.changeset/lazy-doodles-lie.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@pnpm/plugin-commands-installation": patch
|
||||
"pnpm": patch
|
||||
---
|
||||
|
||||
Correctly apply the `cleanupUnusedCatalogs` configuration when removing dependent packages.
|
||||
@@ -12,7 +12,7 @@ import { findWorkspacePackages } from '@pnpm/workspace.find-packages'
|
||||
import { updateWorkspaceManifest } from '@pnpm/workspace.manifest-writer'
|
||||
import { getAllDependenciesFromManifest } from '@pnpm/manifest-utils'
|
||||
import { createOrConnectStoreController, type CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
|
||||
import { type DependenciesField, type ProjectRootDir } from '@pnpm/types'
|
||||
import { type DependenciesField, type ProjectRootDir, type Project } from '@pnpm/types'
|
||||
import { mutateModulesInSingleProject } from '@pnpm/core'
|
||||
import pick from 'ramda/src/pick'
|
||||
import without from 'ramda/src/without'
|
||||
@@ -183,9 +183,14 @@ export async function handler (
|
||||
storeDir: store.dir,
|
||||
include,
|
||||
})
|
||||
const allProjects = opts.allProjects ?? (
|
||||
opts.workspaceDir
|
||||
? await findWorkspacePackages(opts.workspaceDir, { ...opts, patterns: opts.workspacePackagePatterns })
|
||||
: undefined
|
||||
)
|
||||
// @ts-expect-error
|
||||
removeOpts['workspacePackages'] = opts.workspaceDir
|
||||
? arrayOfWorkspacePackagesToMap(await findWorkspacePackages(opts.workspaceDir, { ...opts, patterns: opts.workspacePackagePatterns }))
|
||||
removeOpts['workspacePackages'] = allProjects
|
||||
? arrayOfWorkspacePackagesToMap(allProjects)
|
||||
: undefined
|
||||
const targetDependenciesField = getSaveType(opts)
|
||||
const {
|
||||
@@ -217,8 +222,22 @@ export async function handler (
|
||||
removeOpts
|
||||
)
|
||||
await writeProjectManifest(mutationResult.updatedProject.manifest)
|
||||
|
||||
const updatedProjects: Project[] = []
|
||||
if (allProjects != null) {
|
||||
for (const project of allProjects) {
|
||||
if (project.rootDir === mutationResult.updatedProject.rootDir) {
|
||||
updatedProjects.push({
|
||||
...project,
|
||||
manifest: mutationResult.updatedProject.manifest,
|
||||
})
|
||||
} else {
|
||||
updatedProjects.push(project)
|
||||
}
|
||||
}
|
||||
}
|
||||
await updateWorkspaceManifest(opts.workspaceDir ?? opts.dir, {
|
||||
cleanupUnusedCatalogs: opts.cleanupUnusedCatalogs,
|
||||
allProjects: opts.allProjects,
|
||||
allProjects: updatedProjects,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ function addCatalogs (manifest: Partial<WorkspaceManifest>, newCatalogs: Catalog
|
||||
function removePackagesFromWorkspaceCatalog (manifest: Partial<WorkspaceManifest>, packagesJson: Project[]): boolean {
|
||||
let shouldBeUpdated = false
|
||||
|
||||
if (manifest.catalog == null && manifest.catalogs == null) {
|
||||
if (packagesJson.length === 0 || (manifest.catalog == null && manifest.catalogs == null)) {
|
||||
return shouldBeUpdated
|
||||
}
|
||||
const packageReferences: Record<string, Set<string>> = {}
|
||||
|
||||
@@ -355,3 +355,62 @@ test('update catalogs and remove catalog', async () => {
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test('when allProjects is undefined should not cleanup unused catalogs', async () => {
|
||||
const dir = tempDir(false)
|
||||
const filePath = path.join(dir, WORKSPACE_MANIFEST_FILENAME)
|
||||
writeYamlFile(filePath, {
|
||||
catalogs: {
|
||||
foo: {
|
||||
ghi: '7.8.9',
|
||||
},
|
||||
},
|
||||
})
|
||||
await updateWorkspaceManifest(dir, {
|
||||
updatedCatalogs: {
|
||||
foo: {
|
||||
abc: '0.1.2',
|
||||
},
|
||||
bar: {
|
||||
def: '3.2.1',
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(readYamlFile(filePath)).toStrictEqual({
|
||||
catalogs: {
|
||||
foo: {
|
||||
abc: '0.1.2',
|
||||
ghi: '7.8.9',
|
||||
},
|
||||
bar: {
|
||||
def: '3.2.1',
|
||||
},
|
||||
},
|
||||
})
|
||||
prepare({
|
||||
dependencies: {
|
||||
def: 'catalog:bar',
|
||||
ghi: 'catalog:foo',
|
||||
},
|
||||
}, { tempDir: dir })
|
||||
await updateWorkspaceManifest(dir, {
|
||||
updatedCatalogs: {
|
||||
foo: {
|
||||
ghi: '7.9.9',
|
||||
},
|
||||
},
|
||||
cleanupUnusedCatalogs: true,
|
||||
allProjects: undefined,
|
||||
})
|
||||
expect(readYamlFile(filePath)).toStrictEqual({
|
||||
catalogs: {
|
||||
foo: {
|
||||
abc: '0.1.2',
|
||||
ghi: '7.9.9',
|
||||
},
|
||||
bar: {
|
||||
def: '3.2.1',
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user