fix(plugin-commands-patching): do not create empty patch directory (#7410)

This commit is contained in:
btea
2023-12-13 18:11:02 +08:00
committed by GitHub
parent f3cd0a61d4
commit 9a53179e37
3 changed files with 9 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
"pnpm": patch
---
Do not create empty patch directory.

View File

@@ -48,7 +48,6 @@ export async function handler (opts: install.InstallCommandOptions & Pick<Config
const lockfileDir = opts.lockfileDir ?? opts.dir ?? process.cwd()
const patchesDirName = normalizePath(path.normalize(opts.patchesDir ?? 'patches'))
const patchesDir = path.join(lockfileDir, patchesDirName)
await fs.promises.mkdir(patchesDir, { recursive: true })
const patchedPkgManifest = await readPackageJsonFromDir(userDir)
const pkgNameAndVersion = `${patchedPkgManifest.name}@${patchedPkgManifest.version}`
const gitTarballUrl = await getGitTarballUrlFromLockfile({
@@ -67,6 +66,7 @@ export async function handler (opts: install.InstallCommandOptions & Pick<Config
if (!patchContent.length) {
return `No changes were found to the following directory: ${userDir}`
}
await fs.promises.mkdir(patchesDir, { recursive: true })
const patchFileName = pkgNameAndVersion.replace('/', '__')
await fs.promises.writeFile(path.join(patchesDir, `${patchFileName}.patch`), patchContent, 'utf8')

View File

@@ -317,7 +317,7 @@ describe('patch and commit', () => {
expect(JSON.parse(fs.readFileSync(path.join(patchDir, 'package.json'), 'utf8')).version).toBe('1.0.0')
})
test('should skip empty patch content', async () => {
test('should skip empty patch content and not create patches dir', async () => {
const output = await patch.handler(defaultPatchOption, ['is-positive@1.0.0'])
const patchDir = getPatchDirFromPatchOutput(output)
const result = await patchCommit.handler({
@@ -331,6 +331,7 @@ describe('patch and commit', () => {
}, [patchDir])
expect(result).toBe(`No changes were found to the following directory: ${patchDir}`)
expect(fs.existsSync('patches/is-positive@1.0.0.patch')).toBe(false)
expect(fs.existsSync('patches')).toBe(false)
})
})