Skip to content

Commit

Permalink
fix: patch should print instructions about how to commit the changes (#…
Browse files Browse the repository at this point in the history
…5809)

Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
NullVoxPopuli and zkochan committed Dec 21, 2022
1 parent b77651d commit e8aafe3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/bright-apricots-rest.md
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
"pnpm": patch
---

`pnpm patch` should print instructions about how to commit the changes.
4 changes: 3 additions & 1 deletion packages/plugin-commands-patching/src/patch.ts
Expand Up @@ -53,5 +53,7 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
}
const editDir = opts.editDir ?? tempy.directory()
await writePackage(params[0], editDir, opts)
return `You can now edit the following folder: ${editDir}`
return `You can now edit the following folder: ${editDir}
Once you're done with your changes, run "pnpm patch-commit ${editDir}"`
}
15 changes: 10 additions & 5 deletions packages/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -36,7 +36,7 @@ describe('patch and commit', () => {

test('patch and commit', async () => {
const output = await patch.handler(defaultPatchOption, ['is-positive@1.0.0'])
const patchDir = output.substring(output.indexOf(':') + 1).trim()
const patchDir = getPatchDirFromPatchOutput(output)
const tempDir = os.tmpdir() // temp dir depends on the operating system (@see tempy)

// store patch files in a temporary directory when not given editDir option
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('patch and commit', () => {
const editDir = path.join(tempy.directory())

const output = await patch.handler({ ...defaultPatchOption, editDir }, ['is-positive@1.0.0'])
const patchDir = output.substring(output.indexOf(':') + 1).trim()
const patchDir = getPatchDirFromPatchOutput(output)

expect(patchDir).toBe(editDir)
expect(fs.existsSync(patchDir)).toBe(true)
Expand All @@ -98,7 +98,7 @@ describe('patch and commit', () => {
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()
const patchDir = getPatchDirFromPatchOutput(output)

expect(patchDir).toBe(editDir)
expect(fs.existsSync(patchDir)).toBe(true)
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('patching should work when there is a no EOL in the patched file', () =
})
it('should work when adding content on a newline', async () => {
const output = await patch.handler(defaultPatchOption, ['safe-execa@0.1.2'])
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()
const userPatchDir = getPatchDirFromPatchOutput(output)
const tempDir = os.tmpdir()

expect(userPatchDir).toContain(tempDir)
Expand All @@ -167,7 +167,7 @@ describe('patching should work when there is a no EOL in the patched file', () =
})
it('should work fine when new content is appended', async () => {
const output = await patch.handler(defaultPatchOption, ['safe-execa@0.1.2'])
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()
const userPatchDir = getPatchDirFromPatchOutput(output)
const tempDir = os.tmpdir()

expect(userPatchDir).toContain(tempDir)
Expand All @@ -190,3 +190,8 @@ describe('patching should work when there is a no EOL in the patched file', () =
expect(fs.readFileSync('node_modules/safe-execa/lib/index.js', 'utf8')).toContain('//# sourceMappingURL=index.js.map// patch without newline')
})
})

function getPatchDirFromPatchOutput (output: string) {
const [firstLine] = output.split('\n')
return firstLine.substring(firstLine.indexOf(':') + 1).trim()
}

0 comments on commit e8aafe3

Please sign in to comment.