fix: remove empty patch dir (#7532)

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2024-01-19 07:30:06 +08:00
committed by Zoltan Kochan
parent 342222d20d
commit 6964eade56
3 changed files with 17 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-patching": minor
---
`pnpm patch-remove` removes the directory containing the patch file if it is empty.

View File

@@ -57,9 +57,11 @@ export async function handler (opts: PatchRemoveCommandOptions, params: string[]
throw new PnpmError('NO_PATCHES_TO_REMOVE', 'There are no patches that need to be removed')
}
const patchesDirs = new Set<string>()
await Promise.all(patchesToRemove.map(async (patch) => {
if (Object.prototype.hasOwnProperty.call(patchedDependencies, patch)) {
const patchFile = path.join(lockfileDir, patchedDependencies[patch])
patchesDirs.add(path.dirname(patchFile))
await fs.rm(patchFile, { force: true })
delete rootProjectManifest.pnpm!.patchedDependencies![patch]
if (!Object.keys(rootProjectManifest.pnpm!.patchedDependencies!).length) {
@@ -71,6 +73,15 @@ export async function handler (opts: PatchRemoveCommandOptions, params: string[]
}
}))
await Promise.all(Array.from(patchesDirs).map(async (dir) => {
try {
const files = await fs.readdir(dir)
if (!files.length) {
await fs.rmdir(dir)
}
} catch {}
}))
await writeProjectManifest(rootProjectManifest)
if (opts?.selectedProjectsGraph?.[lockfileDir]) {

View File

@@ -898,6 +898,7 @@ describe('patch-remove', () => {
const { manifest: newManifest } = await readProjectManifest(process.cwd())
expect(newManifest!.pnpm!).toBeUndefined()
expect(fs.existsSync(path.join(process.cwd(), 'patches/is-positive@1.0.0.patch'))).toBe(false)
expect(fs.existsSync(path.join(process.cwd(), 'patches'))).toBe(false)
})
test('prompt to select patches that to be removed', async () => {