Skip to content

Commit

Permalink
feat: support --patches-dir to patch-commit command
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhou-glean committed Mar 14, 2023
1 parent abd556b commit e84ca50
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
23 changes: 17 additions & 6 deletions patching/plugin-commands-patching/src/patchCommit.ts
Expand Up @@ -13,27 +13,38 @@ import tempy from 'tempy'
import { writePackage } from './writePackage'
import { parseWantedDependency } from '@pnpm/parse-wanted-dependency'

export const rcOptionsTypes = cliOptionsTypes
export function rcOptionsTypes () {
return pick([], allTypes)
}

export function cliOptionsTypes () {
return pick([], allTypes)
return { ...rcOptionsTypes(), 'patches-dir': String }
}

export const commandNames = ['patch-commit']

export function help () {
return renderHelp({
description: 'Generate a patch out of a directory',
descriptionLists: [],
descriptionLists: [{
title: 'Options',
list: [
{
description: 'The generated patch file will be saved to this directory',
name: '--patches-dir',
},
],
}],
url: docsUrl('patch-commit'),
usages: ['pnpm patch-commit <patchDir>'],
})
}

export async function handler (opts: install.InstallCommandOptions & Pick<Config, 'rootProjectManifest'>, params: string[]) {
export async function handler (opts: install.InstallCommandOptions & Pick<Config, 'rootProjectManifest'> & { patchesDir?: string }, params: string[]) {
const userDir = params[0]
const lockfileDir = opts.lockfileDir ?? opts.dir ?? process.cwd()
const patchesDir = path.join(lockfileDir, 'patches')
const patchesDirName = opts.patchesDir ?? 'patches'
const patchesDir = path.join(lockfileDir, patchesDirName)
await fs.promises.mkdir(patchesDir, { recursive: true })
const patchedPkgManifest = await readPackageJsonFromDir(userDir)
const pkgNameAndVersion = `${patchedPkgManifest.name}@${patchedPkgManifest.version}`
Expand All @@ -53,7 +64,7 @@ export async function handler (opts: install.InstallCommandOptions & Pick<Config
} else if (!rootProjectManifest.pnpm.patchedDependencies) {
rootProjectManifest.pnpm.patchedDependencies = {}
}
rootProjectManifest.pnpm.patchedDependencies![pkgNameAndVersion] = `patches/${patchFileName}.patch`
rootProjectManifest.pnpm.patchedDependencies![pkgNameAndVersion] = `${patchesDirName}/${patchFileName}.patch`
await writeProjectManifest(rootProjectManifest)

if (opts?.selectedProjectsGraph?.[lockfileDir]) {
Expand Down
23 changes: 23 additions & 0 deletions patching/plugin-commands-patching/test/patch.test.ts
Expand Up @@ -112,6 +112,29 @@ describe('patch and commit', () => {
expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).toContain('// test patching')
})

test('patch and commit with custom patches dir', async () => {
const patchesDir = 'custom-patches'

const output = await patch.handler({ ...defaultPatchOption }, ['is-positive@1.0.0'])
const patchDir = getPatchDirFromPatchOutput(output)

expect(fs.existsSync(patchesDir)).toBe(false)

fs.appendFileSync(path.join(patchDir, 'index.js'), '// test patching', 'utf8')

await patchCommit.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
frozenLockfile: false,
fixLockfile: true,
patchesDir,
}, [patchDir])

expect(fs.existsSync(patchesDir)).toBe(true)
expect(fs.readFileSync(path.join(patchesDir, 'is-positive@1.0.0.patch'), 'utf8')).toContain('// test patching')
expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).toContain('// test patching')
})

test('patch throws an error if the edit-dir already exists and is not empty', async () => {
const editDir = tempy.directory()
fs.writeFileSync(path.join(editDir, 'test.txt'), '', 'utf8')
Expand Down

0 comments on commit e84ca50

Please sign in to comment.