{
  "version": 3,
  "sources": ["../../src/switchCliVersion.ts"],
  "sourcesContent": ["import path from 'node:path';\nimport type { Config } from './packages/config/index.ts';\nimport { PnpmError } from './packages/error/index.ts';\nimport { globalWarn } from './packages/logger/index.ts';\nimport { packageManager } from './packages/cli-meta/index.ts';\nimport { prependDirsToPath } from './packages/env.path/index.ts';\nimport { installPnpmToTools } from './packages/tools.plugin-commands-self-updater/index.ts';\nimport spawn from 'cross-spawn';\nimport semver from 'semver';\nimport process from 'node:process';\n\nexport async function switchCliVersion(config: Config): Promise<void> {\n  const pm = config.wantedPackageManager;\n  if (\n    pm == null ||\n    pm.name !== 'pnpm' ||\n    pm.version == null ||\n    pm.version === packageManager.version\n  ) {\n    return;\n  }\n\n  const pmVersion = semver.valid(pm.version);\n\n  if (pmVersion == null || pmVersion === '') {\n    globalWarn(\n      `Cannot switch to pnpm@${pm.version}: \"${pm.version}\" is not a valid version`\n    );\n\n    return;\n  }\n\n  if (pmVersion !== pm.version.trim()) {\n    globalWarn(\n      `Cannot switch to pnpm@${pm.version}: you need to specify the version as \"${pmVersion}\"`\n    );\n\n    return;\n  }\n\n  const { binDir: wantedPnpmBinDir } = await installPnpmToTools(\n    pmVersion,\n    config\n  );\n\n  const pnpmEnv = prependDirsToPath([wantedPnpmBinDir]);\n  if (!pnpmEnv.updated) {\n    // We throw this error to prevent an infinite recursive call of the same pnpm version.\n    throw new VersionSwitchFail(pmVersion, wantedPnpmBinDir);\n  }\n\n  // Specify the exact pnpm file path that's expected to execute to spawn.sync()\n  //\n  // It's not safe spawn 'pnpm' (without specifying an absolute path) and expect\n  // it to resolve to the same file path computed above due to the $PATH\n  // environment variable. While that does happen in most cases, there's a\n  // scenario where the wanted pnpm bin dir exists, but no pnpm binary is\n  // present within that directory. If that's the case, a different pnpm bin can\n  // get executed, causing infinite spawn and fork bombing the user. See details\n  // at https://github.com/pnpm/pnpm/pull/8679.\n  const pnpmBinPath = path.join(wantedPnpmBinDir, 'pnpm');\n\n  const { status, error } = spawn.sync(pnpmBinPath, process.argv.slice(2), {\n    stdio: 'inherit',\n    env: {\n      ...process.env,\n      [pnpmEnv.name]: pnpmEnv.value,\n    },\n  });\n\n  if (typeof error === 'string') {\n    throw new VersionSwitchFail(pmVersion, wantedPnpmBinDir, error);\n  }\n\n  // eslint-disable-next-line n/no-process-exit\n  process.exit(status ?? 0);\n}\n\nclass VersionSwitchFail extends PnpmError {\n  constructor(version: string, wantedPnpmBinDir: string, cause?: unknown) {\n    super(\n      'VERSION_SWITCH_FAIL',\n      `Failed to switch pnpm to v${version}. Looks like pnpm CLI is missing at \"${wantedPnpmBinDir}\" or is incorrect`,\n      { hint: cause instanceof Error ? cause.message : undefined }\n    );\n\n    if (cause != null) {\n      this.cause = cause;\n    }\n  }\n}\n"],
  "mappings": "AAAA,OAAO,UAAU;AAEjB,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,sBAAsB;AAC/B,SAAS,yBAAyB;AAClC,SAAS,0BAA0B;AACnC,OAAO,WAAW;AAClB,OAAO,YAAY;AACnB,OAAO,aAAa;AAEpB,eAAsB,iBAAiB,QAA+B;AACpE,QAAM,KAAK,OAAO;AAClB,MACE,MAAM,QACN,GAAG,SAAS,UACZ,GAAG,WAAW,QACd,GAAG,YAAY,eAAe,SAC9B;AACA;AAAA,EACF;AAEA,QAAM,YAAY,OAAO,MAAM,GAAG,OAAO;AAEzC,MAAI,aAAa,QAAQ,cAAc,IAAI;AACzC;AAAA,MACE,yBAAyB,GAAG,OAAO,MAAM,GAAG,OAAO;AAAA,IACrD;AAEA;AAAA,EACF;AAEA,MAAI,cAAc,GAAG,QAAQ,KAAK,GAAG;AACnC;AAAA,MACE,yBAAyB,GAAG,OAAO,yCAAyC,SAAS;AAAA,IACvF;AAEA;AAAA,EACF;AAEA,QAAM,EAAE,QAAQ,iBAAiB,IAAI,MAAM;AAAA,IACzC;AAAA,IACA;AAAA,EACF;AAEA,QAAM,UAAU,kBAAkB,CAAC,gBAAgB,CAA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