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

feat: when doing patches, it'd be useful to be given instructions for what … #5809

Merged
merged 3 commits into from Dec 21, 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/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()
}