Files
pnpm/patching/apply-patch/src/index.ts
Johan Quan Vo 7b1c189f2e feat!: remove deprecated patch options (#10505)
* refactor: remove allowNonAppliedPatches

* refactor: remove ignorePatchFailures

* refactor: remove `strict` field in groupPatchedDependencies

* test: update test failure in package patching

* test: fix

* docs: update changesets

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
2026-01-27 17:08:45 +01:00

32 lines
1.1 KiB
TypeScript

import { PnpmError } from '@pnpm/error'
import { applyPatch } from '@pnpm/patch-package/dist/applyPatches.js'
export interface ApplyPatchToDirOpts {
patchedDir: string
patchFilePath: string
}
export function applyPatchToDir (opts: ApplyPatchToDirOpts): boolean {
// Ideally, we would just run "patch" or "git apply".
// However, "patch" is not available on Windows and "git apply" is hard to execute on a subdirectory of an existing repository
const cwd = process.cwd()
process.chdir(opts.patchedDir)
let success = false
try {
success = applyPatch({
patchFilePath: opts.patchFilePath,
})
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError('PATCH_NOT_FOUND', `Patch file not found: ${opts.patchFilePath}`)
}
throw new PnpmError('INVALID_PATCH', `Applying patch "${opts.patchFilePath}" failed: ${err.message as string}`)
} finally {
process.chdir(cwd)
}
if (!success) {
throw new PnpmError('PATCH_FAILED', `Could not apply patch ${opts.patchFilePath} to ${opts.patchedDir}`)
}
return success
}