{"version":3,"file":"spawnCommand.cjs","names":["spawn","spawnSync"],"sources":["../../../src/utils/spawnCommand.ts"],"sourcesContent":["import {\n  type ChildProcess,\n  type SpawnOptions,\n  type SpawnSyncOptions,\n  spawn,\n  spawnSync,\n} from 'node:child_process';\n\n/** Exit code cmd.exe returns when the command is not recognized. */\nconst WINDOWS_COMMAND_NOT_FOUND_EXIT_CODE = 9009;\n\n/** Characters that never need quoting for cmd.exe. */\nconst SAFE_WINDOWS_ARGUMENT_PATTERN = /^[\\w@+=:,./\\\\-]+$/;\n\nconst isWindows = (): boolean => process.platform === 'win32';\n\n/**\n * Quotes an argument for a cmd.exe command line; plain tokens are kept as is.\n *\n * @example quoteWindowsArgument('my project') // '\"my project\"'\n */\nexport const quoteWindowsArgument = (argument: string): string =>\n  SAFE_WINDOWS_ARGUMENT_PATTERN.test(argument)\n    ? argument\n    : `\"${argument.replaceAll('\"', '\"\"')}\"`;\n\n/** Builds the cmd.exe command line running `command` with `args`. */\nexport const toWindowsCommandLine = (command: string, args: string[]): string =>\n  [command, ...args].map(quoteWindowsArgument).join(' ');\n\n/**\n * `spawn` that also runs Windows `.cmd` shims (`npx`, `pnpm`, `yarn`), which\n * Node only starts through a shell.\n *\n * On Windows a missing command does not emit `error`: the shell exits with a\n * code that `isCommandNotFoundExitCode` recognizes.\n */\nexport const spawnCommand = (\n  command: string,\n  args: string[],\n  options: SpawnOptions = {}\n): ChildProcess =>\n  isWindows()\n    ? spawn(toWindowsCommandLine(command, args), { ...options, shell: true })\n    : spawn(command, args, options);\n\n/** Synchronous counterpart of `spawnCommand`. */\nexport const spawnCommandSync = (\n  command: string,\n  args: string[],\n  options: SpawnSyncOptions = {}\n): ReturnType<typeof spawnSync> =>\n  isWindows()\n    ? spawnSync(toWindowsCommandLine(command, args), {\n        ...options,\n        shell: true,\n      })\n    : spawnSync(command, args, options);\n\n/** Whether a `spawnCommand` exit code means the command was not found. */\nexport const isCommandNotFoundExitCode = (exitCode: number | null): boolean =>\n  isWindows() && exitCode === WINDOWS_COMMAND_NOT_FOUND_EXIT_CODE;\n"],"mappings":";;;;;AASA,MAAM,sCAAsC;;AAG5C,MAAM,gCAAgC;AAEtC,MAAM,kBAA2B,QAAQ,aAAa;;;;;;AAOtD,MAAa,wBAAwB,aACnC,8BAA8B,KAAK,QAAQ,IACvC,WACA,IAAI,SAAS,WAAW,MAAK,MAAI,EAAE;;AAGzC,MAAa,wBAAwB,SAAiB,SACpD,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC,IAAI,oBAAoB,CAAC,CAAC,KAAK,GAAG;;;;;;;;AASvD,MAAa,gBACX,SACA,MACA,UAAwB,CAAC,MAEzB,UAAU,QACNA,0BAAM,qBAAqB,SAAS,IAAI,GAAG;CAAE,GAAG;CAAS,OAAO;AAAK,CAAC,QACtEA,0BAAM,SAAS,MAAM,OAAO;;AAGlC,MAAa,oBACX,SACA,MACA,UAA4B,CAAC,MAE7B,UAAU,QACNC,8BAAU,qBAAqB,SAAS,IAAI,GAAG;CAC7C,GAAG;CACH,OAAO;AACT,CAAC,QACDA,8BAAU,SAAS,MAAM,OAAO;;AAGtC,MAAa,6BAA6B,aACxC,UAAU,KAAK,aAAa"}