{
  "version": 3,
  "sources": ["../../../../src/packages/plugin-commands-setup/setup.ts"],
  "sourcesContent": ["import fs from 'node:fs';\nimport path from 'node:path';\nimport { detectIfCurrentPkgIsExecutable } from '../cli-meta/index.ts';\nimport { docsUrl } from '../cli-utils/index.ts';\nimport { logger } from '../logger/index.ts';\nimport {\n  addDirToEnvPath,\n  type ConfigReport,\n  type PathExtenderReport,\n} from '@pnpm/os.env.path-extender';\nimport renderHelp from 'render-help';\nimport rimraf from '@zkochan/rimraf';\n\nexport function rcOptionsTypes(): Record<string, unknown> {\n  return {};\n}\n\nexport function cliOptionsTypes(): Record<string, unknown> {\n  return {\n    force: Boolean,\n  };\n}\n\nexport const shorthands = {};\n\nexport const commandNames = ['setup'];\n\nexport function help(): string {\n  return renderHelp({\n    description: 'Sets up pnpm',\n    descriptionLists: [\n      {\n        title: 'Options',\n\n        list: [\n          {\n            description:\n              'Override the PNPM_HOME env variable in case it already exists',\n            name: '--force',\n            shortAlias: '-f',\n          },\n        ],\n      },\n    ],\n    url: docsUrl('setup'),\n    usages: ['pnpm setup'],\n  });\n}\n\nfunction getExecPath(): string {\n  if (detectIfCurrentPkgIsExecutable()) {\n    // If the pnpm CLI was bundled by vercel/pkg then we cannot use the js path for npm_execpath\n    // because in that case the js is in a virtual filesystem inside the executor.\n    // Instead, we use the path to the exe file.\n    return process.execPath;\n  }\n\n  return require.main != null ? require.main.filename : process.cwd();\n}\n\nfunction copyCli(currentLocation: string, targetDir: string): void {\n  const newExecPath = path.join(targetDir, path.basename(currentLocation));\n\n  if (path.relative(newExecPath, currentLocation) === '') return;\n\n  logger.info({\n    message: `Copying pnpm CLI from ${currentLocation} to ${newExecPath}`,\n    prefix: process.cwd(),\n  });\n\n  fs.mkdirSync(targetDir, { recursive: true });\n\n  rimraf.sync(newExecPath);\n\n  fs.copyFileSync(currentLocation, newExecPath);\n}\n\nfunction createPnpxScripts(targetDir: string): void {\n  // Why script files instead of aliases?\n  // 1. Aliases wouldn't work on all platform, such as Windows Command Prompt or POSIX `sh`.\n  // 2. Aliases wouldn't work on all environments, such as non-interactive shells and CI environments.\n  // 3. Aliases must be set for different shells while script files are limited to only 2 types: POSIX and Windows.\n  // 4. Aliases cannot be located with the `which` or `where` command.\n  // 5. Editing rc files is more error-prone than just write new files to the filesystem.\n\n  fs.mkdirSync(targetDir, { recursive: true });\n\n  // windows can also use shell script via mingw or cygwin so no filter\n  const shellScript = ['#!/bin/sh', 'exec pnpm dlx \"$@\"'].join('\\n');\n\n  fs.writeFileSync(path.join(targetDir, 'pnpx'), shellScript, { mode: 0o755 });\n\n  if (process.platform === 'win32') {\n    const batchScript = ['@echo off', 'pnpm dlx %*'].join('\\n');\n\n    fs.writeFileSync(path.join(targetDir, 'pnpx.cmd'), batchScript);\n\n    const powershellScript = 'pnpm dlx @args';\n\n    fs.writeFileSync(path.join(targetDir, 'pnpx.ps1'), powershellScript);\n  }\n}\n\nexport async function handler(opts: {\n  force?: boolean | undefined;\n  pnpmHomeDir: string;\n}): Promise<string> {\n  const execPath = getExecPath();\n\n  if (execPath.match(/\\.[cm]?js$/) == null) {\n    copyCli(execPath, opts.pnpmHomeDir);\n\n    createPnpxScripts(opts.pnpmHomeDir);\n  }\n\n  try {\n    const report = await addDirToEnvPath(opts.pnpmHomeDir, {\n      configSectionName: 'pnpm',\n      proxyVarName: 'PNPM_HOME',\n      overwrite: opts.force ?? false,\n      position: 'start',\n    });\n\n    return renderSetupOutput(report);\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  } catch (err: any) {\n    switch (err.code) {\n      case 'ERR_PNPM_BAD_ENV_FOUND': {\n        err.hint =\n          'If you want to override the existing env variable, use the --force option';\n        break;\n      }\n\n      case 'ERR_PNPM_BAD_SHELL_SECTION': {\n        err.hint =\n          'If you want to override the existing configuration section, use the --force option';\n        break;\n      }\n    }\n\n    throw err;\n  }\n}\n\nfunction renderSetupOutput(report: PathExtenderReport): string {\n  if (report.oldSettings === report.newSettings) {\n    return 'No changes to the environment were made. Everything is already up to date.';\n  }\n\n  const output = [];\n\n  if (typeof report.configFile !== 'undefined') {\n    output.push(reportConfigChange(report.configFile));\n  }\n\n  output.push(`Next configuration changes were made: ${report.newSettings}`);\n\n  if (report.configFile == null) {\n    output.push('Setup complete. Open a new terminal to start using pnpm.');\n  } else if (report.configFile.changeType !== 'skipped') {\n    output.push(`To start using pnpm, run: source ${report.configFile.path}`);\n  }\n\n  return output.join('\\n\\n');\n}\n\nfunction reportConfigChange(configReport: ConfigReport): string {\n  switch (configReport.changeType) {\n    case 'created': {\n      return `Created ${configReport.path}`;\n    }\n\n    case 'appended': {\n      return `Appended new lines to ${configReport.path}`;\n    }\n\n    case 'modified': {\n      return `Replaced configuration in ${configReport.path}`;\n    }\n\n    case 'skipped': {\n      return `Configuration already up to date in ${configReport.path}`;\n    }\n  }\n}\n"],
  "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,sCAAsC;AAC/C,SAAS,eAAe;AACxB,SAAS,cAAc;AACvB;AAAA,EACE;AAAA,OAGK;AACP,OAAO,gBAAgB;AACvB,OAAO,YAAY;AAEZ,SAAS,iBAA0C;AACxD,SAAO,CAAC;AACV;AAEO,SAAS,kBAA2C;AACzD,SAAO;AAAA,IACL,OAAO;AAAA,EACT;AACF;AAEO,MAAM,aAAa,CAAC;AAEpB,MAAM,eAAe,CAAC,OAAO;AAE7B,SAAS,OAAe;AAC7B,SAAO,WAAW;AAAA,IAChB,aAAa;AAAA,IACb,kBAAkB;AAAA,MAChB;AAAA,QACE,OAAO;AAAA,QAEP,MAAM;AAAA,UACJ;AAAA,YACE,aACE;AAAA,YACF,MAAM;AAAA,YACN,YAAY;AAAA,UACd;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,KAAK,QAAQ,OAAO;AAAA,IACpB,QAAQ,CAAC,YAAY;AAAA,EACvB,CAAC;AACH;AAEA,SAAS,cAAsB;AAC7B,MAAI,+BAA+B,GAAG;AAIpC,WAAO,QAAQ;AAAA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