From 911d295846c7d172b0fa4da097fce2c2e2a928be Mon Sep 17 00:00:00 2001 From: John DiMatteo <19596966+johnnyd710@users.noreply.github.com> Date: Wed, 5 Oct 2022 15:19:38 -0500 Subject: [PATCH] fix: pnpm patch-commit with a trailing slash on Windows does not work (#5455) pnpm patch-commit did not apply patch if the absolute path to a directory passed to it contained a trailing slash on Windows, now the trailing slash is removed close #5449 Co-authored-by: Zoltan Kochan --- .changeset/wicked-zebras-deliver.md | 6 ++++++ .../src/patchCommit.ts | 15 +++++++++------ .../test/patch.test.ts | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 .changeset/wicked-zebras-deliver.md diff --git a/.changeset/wicked-zebras-deliver.md b/.changeset/wicked-zebras-deliver.md new file mode 100644 index 00000000000..e7a41797136 --- /dev/null +++ b/.changeset/wicked-zebras-deliver.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-patching": patch +"pnpm": patch +--- + +`pnpm patch-commit` should work when the patch directory is specified with a trailing slash [#5449](https://github.com/pnpm/pnpm/issues/5449). diff --git a/packages/plugin-commands-patching/src/patchCommit.ts b/packages/plugin-commands-patching/src/patchCommit.ts index cc4666d9991..cbda347e31e 100644 --- a/packages/plugin-commands-patching/src/patchCommit.ts +++ b/packages/plugin-commands-patching/src/patchCommit.ts @@ -89,14 +89,17 @@ async function diffFolders (folderA: string, folderB: string) { if (stderr.length > 0) throw new Error(`Unable to diff directories. Make sure you have a recent version of 'git' available in PATH.\nThe following error was reported by 'git':\n${stderr}`) - const normalizePath = folderAN.startsWith('/') - ? (p: string) => p.slice(1) - : (p: string) => p - return stdout - .replace(new RegExp(`(a|b)(${escapeStringRegexp(`/${normalizePath(folderAN)}/`)})`, 'g'), '$1/') - .replace(new RegExp(`(a|b)${escapeStringRegexp(`/${normalizePath(folderBN)}/`)}`, 'g'), '$1/') + .replace(new RegExp(`(a|b)(${escapeStringRegexp(`/${removeTrailingAndLeadingSlash(folderAN)}/`)})`, 'g'), '$1/') + .replace(new RegExp(`(a|b)${escapeStringRegexp(`/${removeTrailingAndLeadingSlash(folderBN)}/`)}`, 'g'), '$1/') .replace(new RegExp(escapeStringRegexp(`${folderAN}/`), 'g'), '') .replace(new RegExp(escapeStringRegexp(`${folderBN}/`), 'g'), '') .replace(/\n\\ No newline at end of file$/, '') } + +function removeTrailingAndLeadingSlash (p: string) { + if (p.startsWith('/') || p.endsWith('/')) { + return p.replace(/^\/|\/$/g, '') + } + return p +} diff --git a/packages/plugin-commands-patching/test/patch.test.ts b/packages/plugin-commands-patching/test/patch.test.ts index 949081c30bf..5eab0074617 100644 --- a/packages/plugin-commands-patching/test/patch.test.ts +++ b/packages/plugin-commands-patching/test/patch.test.ts @@ -93,6 +93,25 @@ describe('patch and commit', () => { await expect(() => patch.handler({ ...defaultPatchOption, editDir }, ['is-positive@1.0.0'])) .rejects.toThrow(`The target directory already exists: '${editDir}'`) }) + + test('patch and commit should work when the patch directory is specified with a trailing slash', async () => { + const editDir = path.join(tempy.directory()) + (os.platform() === 'win32' ? '\\' : '/') + + const output = await patch.handler({ ...defaultPatchOption, editDir }, ['is-positive@1.0.0']) + const patchDir = output.substring(output.indexOf(':') + 1).trim() + + expect(patchDir).toBe(editDir) + expect(fs.existsSync(patchDir)).toBe(true) + + fs.appendFileSync(path.join(patchDir, 'index.js'), '// test patching', 'utf8') + + await patchCommit.handler({ + ...DEFAULT_OPTS, + dir: process.cwd(), + }, [patchDir]) + + expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).toContain('// test patching') + }) }) describe('patching should work when there is a no EOL in the patched file', () => {