Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pnpm patch-commit with a trailing slash on Windows does not work #5455

Merged
merged 4 commits into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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