Skip to content

Commit

Permalink
refactor: patch
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Sep 4, 2022
1 parent dba99dc commit 92ab5ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/funny-buses-own.md
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": minor
"pnpm": minor
---

`pnpm patch`: edit the patched package in a directory specified by the `--edit-dir` option. E.g., `pnpm patch express@3.1.0 --edit-dir=/home/xxx/src/patched-express`
28 changes: 7 additions & 21 deletions packages/plugin-commands-patching/src/patch.ts
Expand Up @@ -35,22 +35,17 @@ export function help () {
title: 'Options',
list: [
{
description: 'Directory path that patch files will be stored. If not set, the files is stored in temporary directory.',
description: 'The package that needs to be modified will be extracted to this directory',
name: '--edit-dir',
shortAlias: '-d',
},
],
}],
url: docsUrl('patch'),
usages: ['pnpm patch [--edit-dir <user directory path for patch>]'],
usages: ['pnpm patch <pkg name>@<version>'],
})
}

export type PatchCommandOptions = Pick<
Config,
'dir' | 'registries' | 'tag' | 'storeDir'
> &
CreateStoreControllerOptions & {
export type PatchCommandOptions = Pick<Config, 'dir' | 'registries' | 'tag' | 'storeDir'> & CreateStoreControllerOptions & {
editDir?: string
reporter?: (logObj: LogBase) => void
}
Expand All @@ -66,15 +61,11 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
lockfileDir: opts.dir,
preferredVersions: {},
projectDir: opts.dir,
registry:
(dep.alias && pickRegistryForPackage(opts.registries, dep.alias)) ??
opts.registries.default,
registry: (dep.alias && pickRegistryForPackage(opts.registries, dep.alias)) ?? opts.registries.default,
})
const filesResponse = await pkgResponse.files!()

const tempDir = tempy.directory()
const patchPackageDir = createPackageDirectory(opts.editDir) ?? tempDir
const userChangesDir = path.join(patchPackageDir, 'user')
const userChangesDir = opts.editDir ? createPackageDirectory(opts.editDir) : path.join(tempDir, 'user')
await Promise.all([
store.ctrl.importPackage(path.join(tempDir, 'source'), {
filesResponse,
Expand All @@ -88,15 +79,10 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
return `You can now edit the following folder: ${userChangesDir}`
}

function createPackageDirectory (editDir?: string) {
if (!editDir) {
return
}

function createPackageDirectory (editDir: string) {
if (fs.existsSync(editDir)) {
throw new PnpmError('DIR_ALREADY_EXISTS', `The package directory already exists: '${editDir}'`)
throw new PnpmError('PATCH_EDIT_DIR_EXISTS', `The target directory already exists: '${editDir}'`)
}

fs.mkdirSync(editDir, { recursive: true })
return editDir
}
4 changes: 2 additions & 2 deletions packages/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -76,11 +76,11 @@ describe('patch and commit', () => {
const output = await patchFn()
const userPatchDir = output.substring(output.indexOf(':') + 1).trim()

expect(userPatchDir).toBe(path.join(editDir, '/user'))
expect(userPatchDir).toBe(editDir)
expect(fs.existsSync(userPatchDir)).toBe(true)
expect(fs.existsSync(path.join(tempySpy.mock.results[0].value, '/source'))).toBe(true)

// If editDir already exists, it should throw an error
await expect(patchFn()).rejects.toThrow(`The package directory already exists: '${editDir}'`)
await expect(patchFn()).rejects.toThrow(`The target directory already exists: '${editDir}'`)
})
})

0 comments on commit 92ab5ca

Please sign in to comment.