{
  "version": 3,
  "sources": ["../../../../src/packages/plugin-commands-patching/patch.ts"],
  "sourcesContent": ["import fs from 'node:fs';\nimport path from 'node:path';\nimport { applyPatchToDir } from '../patching.apply-patch/index.ts';\nimport { docsUrl } from '../cli-utils/index.ts';\nimport { type Config, types as allTypes } from '../config/index.ts';\nimport type { CreateStoreControllerOptions } from '../store-connection-manager/index.ts';\nimport pick from 'ramda/src/pick';\nimport renderHelp from 'render-help';\nimport chalk from 'chalk';\nimport terminalLink from 'terminal-link';\nimport { PnpmError } from '../error/index.ts';\nimport { writePackage } from './writePackage.ts';\nimport { getEditDirPath } from './getEditDirPath.ts';\nimport {\n  type GetPatchedDependencyResult,\n  getPatchedDependency,\n} from './getPatchedDependency.ts';\nimport { writeEditDirState } from './stateFile.ts';\nimport { tryReadProjectManifest } from '../read-project-manifest/index.ts';\nimport isWindows from 'is-windows';\nimport type { ReporterFunction } from '../headless/index.ts';\n\nexport function rcOptionsTypes(): Record<string, unknown> {\n  return pick.default([], allTypes);\n}\n\nexport function cliOptionsTypes(): Record<string, unknown> {\n  return {\n    ...rcOptionsTypes(),\n    'edit-dir': String,\n    'ignore-existing': Boolean,\n  };\n}\n\nexport const shorthands = {\n  d: '--edit-dir',\n};\n\nexport const commandNames = ['patch'];\n\nexport function help(): string {\n  return renderHelp({\n    description: 'Prepare a package for patching',\n    descriptionLists: [\n      {\n        title: 'Options',\n        list: [\n          {\n            description:\n              'The package that needs to be modified will be extracted to this directory',\n            name: '--edit-dir',\n          },\n          {\n            description: 'Ignore existing patch files when patching',\n            name: '--ignore-existing',\n          },\n        ],\n      },\n    ],\n    url: docsUrl('patch'),\n    usages: ['pnpm patch <pkg name>@<version>'],\n  });\n}\n\nexport type PatchCommandOptions = Pick<\n  Config,\n  | 'dir'\n  | 'registries'\n  | 'tag'\n  | 'storeDir'\n  | 'rootProjectManifest'\n  | 'lockfileDir'\n  | 'modulesDir'\n  | 'virtualStoreDir'\n  | 'sharedWorkspaceLockfile'\n> &\n  CreateStoreControllerOptions & {\n    editDir?: string | undefined;\n    reporter?: ReporterFunction | undefined;\n    ignoreExisting?: boolean | undefined;\n  };\n\nexport async function handler(\n  opts: PatchCommandOptions,\n  params: string[]\n): Promise<string> {\n  if (\n    typeof opts.editDir === 'string' &&\n    fs.existsSync(opts.editDir) &&\n    fs.readdirSync(opts.editDir).length > 0\n  ) {\n    throw new PnpmError(\n      'PATCH_EDIT_DIR_EXISTS',\n      `The target directory already exists: '${opts.editDir}'`\n    );\n  }\n\n  const first = params[0];\n\n  if (typeof first === 'undefined') {\n    throw new PnpmError(\n      'MISSING_PACKAGE_NAME',\n      '`pnpm patch` requires the package name'\n    );\n  }\n\n  const lockfileDir = opts.lockfileDir;\n\n  const patchedDep = await getPatchedDependency(first, {\n    lockfileDir,\n    modulesDir: opts.modulesDir,\n    virtualStoreDir: opts.virtualStoreDir,\n  });\n\n  const modulesDir = path.join(lockfileDir, opts.modulesDir ?? 'node_modules');\n\n  const editDir =\n    typeof opts.editDir === 'string'\n      ? path.resolve(opts.dir, opts.editDir)\n      : getEditDirPath(first, patchedDep, { modulesDir });\n\n  if (fs.existsSync(editDir) && fs.readdirSync(editDir).length !== 0) {\n    throw new PnpmError(\n      'EDIT_DIR_NOT_EMPTY',\n      `The directory ${editDir} is not empty`,\n      {\n        hint: 'Either run `pnpm patch-commit` to commit or delete it then run `pnpm patch` to recreate it',\n      }\n    );\n  }\n\n  await writePackage(patchedDep, editDir, opts);\n\n  writeEditDirState({\n    editDir,\n    modulesDir: path.join(opts.dir, opts.modulesDir ?? 'node_modules'),\n    patchedPkg: first,\n    applyToAll: patchedDep.applyToAll,\n  });\n\n  if (opts.ignoreExisting !== true) {\n    let rootProjectManifest = opts.rootProjectManifest;\n\n    if (opts.sharedWorkspaceLockfile !== true) {\n      const { manifest } = await tryReadProjectManifest(lockfileDir);\n\n      if (manifest) {\n        rootProjectManifest = manifest;\n      }\n    }\n\n    if (rootProjectManifest?.pnpm?.patchedDependencies) {\n      tryPatchWithExistingPatchFile({\n        allowFailure: patchedDep.applyToAll,\n        patchedDep,\n        patchedDir: editDir,\n        patchedDependencies: rootProjectManifest.pnpm.patchedDependencies,\n        lockfileDir,\n      });\n    }\n  }\n\n  const quote = isWindows() ? '\"' : \"'\";\n\n  return `Patch: You can now edit the package at:\n\n  ${terminalLink(chalk.blue(editDir), `file://${editDir}`)}\n\nTo commit your changes, run:\n\n  ${chalk.green(`pnpm patch-commit ${quote}${editDir}${quote}`)}\n\n`;\n}\n\nfunction tryPatchWithExistingPatchFile({\n  allowFailure,\n  patchedDep: { applyToAll, alias, pref },\n  patchedDir,\n  patchedDependencies,\n  lockfileDir,\n}: {\n  allowFailure: boolean;\n  patchedDep: GetPatchedDependencyResult;\n  patchedDir: string;\n  patchedDependencies: Record<string, string>;\n  lockfileDir: string;\n}): void {\n  if (typeof alias !== 'string') return;\n\n  let existingPatchFile: string | undefined;\n\n  if (typeof pref === 'string') {\n    existingPatchFile = patchedDependencies[`${alias}@${pref}`];\n  }\n\n  if (typeof existingPatchFile === 'undefined' && applyToAll) {\n    existingPatchFile = patchedDependencies[alias];\n  }\n\n  if (typeof existingPatchFile === 'undefined') {\n    return;\n  }\n\n  const existingPatchFilePath = path.resolve(lockfileDir, existingPatchFile);\n\n  if (fs.existsSync(existingPatchFilePath) !== true) {\n    throw new PnpmError(\n      'PATCH_FILE_NOT_FOUND',\n      `Unable to find patch file ${existingPatchFilePath}`\n    );\n  }\n\n  applyPatchToDir({\n    patchedDir,\n    patchFilePath: existingPatchFilePath,\n    allowFailure,\n  });\n}\n"],
  "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,uBAAuB;AAChC,SAAS,eAAe;AACxB,SAAsB,SAAS,gBAAgB;AAE/C,OAAO,UAAU;AACjB,OAAO,gBAAgB;AACvB,OAAO,WAAW;AAClB,OAAO,kBAAkB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB;AAC7B,SAAS,sBAAsB;AAC/B;AAAA,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