Skip to content

Commit

Permalink
fix: pnpm patch-commit with a trailing slash on Windows does not work (
Browse files Browse the repository at this point in the history
…#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 <z@kochan.io>
  • Loading branch information
johnnyd710 and zkochan committed Oct 5, 2022
1 parent d101eb0 commit 911d295
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .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).
15 changes: 9 additions & 6 deletions packages/plugin-commands-patching/src/patchCommit.ts
Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions packages/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 911d295

Please sign in to comment.