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 <z@kochan.io>
This commit is contained in:
John DiMatteo
2022-10-05 15:19:38 -05:00
committed by GitHub
parent d101eb0503
commit 911d295846
3 changed files with 34 additions and 6 deletions

View File

@@ -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).

View File

@@ -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
}

View File

@@ -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', () => {