{"version":3,"file":"service-B4Gh_bbL.mjs","names":[],"sources":["../src/cli/commands/upgrade/version-detector.ts","../src/cli/commands/upgrade/service.ts"],"sourcesContent":["import * as path from \"pathe\";\nimport { readPackageJSON } from \"pkg-types\";\n\nconst SDK_PACKAGE_NAME = \"@tailor-platform/sdk\";\n\n/**\n * Detect the installed SDK version from the user's project.\n * Walks up from projectRoot to find the SDK package in node_modules,\n * matching Node's module resolution for workspace setups with hoisted deps.\n * @param projectRoot - The project root directory to search from\n * @returns The installed SDK version string, or null if not found\n */\nexport async function detectInstalledVersion(projectRoot: string): Promise<string | null> {\n  let dir = path.resolve(projectRoot);\n  // loop exits when a package.json is found or the root is reached\n  // oxlint-disable-next-line typescript/no-unnecessary-condition\n  while (true) {\n    try {\n      const sdkPath = path.join(dir, \"node_modules\", SDK_PACKAGE_NAME);\n      const pkg = await readPackageJSON(sdkPath);\n      if (pkg.version) return pkg.version;\n    } catch {\n      // Not found at this level, try parent\n    }\n    const parent = path.dirname(dir);\n    if (parent === dir) break;\n    dir = parent;\n  }\n  return null;\n}\n","import { spawnSync } from \"node:child_process\";\nimport { CLIError } from \"#/cli/shared/errors\";\nimport { logger, styles } from \"#/cli/shared/logger\";\nimport { detectInstalledVersion } from \"./version-detector\";\nimport type { RunOutput } from \"./types\";\n\ninterface UpgradeOptions {\n  from: string;\n  dryRun: boolean;\n  path: string;\n}\n\n/**\n * Print the upgrade summary to the terminal.\n * @param output - The parsed JSON output from sdk-codemod\n * @param dryRun - Whether this was a dry-run\n */\nfunction printUpgradeSummary(output: RunOutput, dryRun: boolean): void {\n  if (dryRun) {\n    logger.info(`${styles.bold(\"[Dry Run]\")} No files were modified.`);\n    logger.log(\"\");\n  }\n\n  const total = output.codemodsApplied + output.codemodsSkipped + output.errors.length;\n  logger.info(\n    `Upgrade complete: ${styles.success(`${output.codemodsApplied} applied`)}, ${styles.dim(`${output.codemodsSkipped} skipped`)} (${total} total codemods)`,\n  );\n\n  if (output.filesModified.length > 0) {\n    logger.log(\"\");\n    logger.info(\n      `${dryRun ? \"Files that would be modified\" : \"Modified files\"} (${output.filesModified.length}):`,\n    );\n    for (const file of output.filesModified) {\n      logger.log(`  ${styles.path(file)}`);\n    }\n  }\n\n  if (output.warnings.length > 0) {\n    logger.log(\"\");\n    logger.warn(`Manual attention needed (${output.warnings.length}):`);\n    for (const warning of output.warnings) {\n      logger.log(`  ${styles.warning(\"!\")} ${warning}`);\n    }\n  }\n\n  if (output.errors.length > 0) {\n    logger.log(\"\");\n    logger.error(`Failed codemods (${output.errors.length}):`);\n    for (const { codemodId, message } of output.errors) {\n      logger.log(`  ${styles.error(codemodId)}: ${message}`);\n    }\n  }\n}\n\n/**\n * Run the upgrade pipeline:\n * 1. Detect target SDK version from node_modules\n * 2. Invoke @tailor-platform/sdk-codemod CLI\n * 3. Parse JSON output and display results\n * @param options - Upgrade options\n */\nexport async function upgrade(options: UpgradeOptions): Promise<void> {\n  const projectRoot = options.path;\n\n  // Step 1: Detect target SDK version (the newly installed version)\n  const targetVersion = await detectInstalledVersion(projectRoot);\n  if (!targetVersion) {\n    throw CLIError({\n      code: \"UPGRADE_SDK_VERSION_UNDETECTED\",\n      message: `Could not detect installed @tailor-platform/sdk version in ${projectRoot}`,\n      suggestion:\n        \"Ensure @tailor-platform/sdk is installed. Run 'pnpm install' or 'npm install' first.\",\n      command: \"upgrade\",\n    });\n  }\n\n  logger.info(\n    `Upgrading from ${styles.highlight(options.from)} → ${styles.highlight(targetVersion)}`,\n  );\n\n  if (options.dryRun) {\n    logger.info(`${styles.bold(\"[Dry Run]\")} Changes will be previewed but not applied.`);\n  }\n\n  logger.log(\"\");\n\n  // Step 2: Invoke sdk-codemod CLI\n  // Use \"latest\" because sdk-codemod may not be published at the exact same\n  // version as @tailor-platform/sdk.  Version filtering is handled internally\n  // by sdk-codemod's registry via the --from / --to arguments.\n  const npxCommand = process.platform === \"win32\" ? \"npx.cmd\" : \"npx\";\n\n  const result = spawnSync(\n    npxCommand,\n    [\n      \"@tailor-platform/sdk-codemod@latest\",\n      \"--from\",\n      options.from,\n      \"--to\",\n      targetVersion,\n      \"--target\",\n      projectRoot,\n      ...(options.dryRun ? [\"--dry-run\"] : []),\n    ],\n    {\n      cwd: projectRoot,\n      stdio: [\"ignore\", \"pipe\", \"pipe\"],\n      encoding: \"utf-8\",\n      timeout: 300_000,\n    },\n  );\n\n  if (result.error) {\n    throw CLIError({\n      code: \"UPGRADE_CODEMOD_SPAWN_FAILED\",\n      message: `Failed to run @tailor-platform/sdk-codemod: ${result.error.message}`,\n      suggestion: \"Ensure npx is available and the network is accessible.\",\n      command: \"upgrade\",\n    });\n  }\n\n  // Check for non-zero exit without a launch error (e.g. registry/auth/network failures)\n  if (result.status !== 0 && !result.stdout.trim()) {\n    throw CLIError({\n      code: \"UPGRADE_CODEMOD_FAILED\",\n      message: `@tailor-platform/sdk-codemod exited with code ${result.status}`,\n      details: result.stderr.trim() || \"(no stderr output)\",\n      suggestion:\n        \"Review the error above. Common causes: invalid version arguments, network issues, or missing package registry access.\",\n      command: \"upgrade\",\n    });\n  }\n\n  // Forward captured stderr so users see dry-run diffs and progress messages\n  // written by sdk-codemod. stderr is piped (not inherited) so that the error\n  // path above can surface it via CLIError, but on success we still need to\n  // replay it verbatim to keep the colorized unified diff output visible.\n  if (result.stderr) {\n    process.stderr.write(result.stderr);\n  }\n\n  // Step 3: Parse JSON output\n  let output: RunOutput;\n  try {\n    output = JSON.parse(result.stdout);\n  } catch {\n    throw CLIError({\n      code: \"UPGRADE_CODEMOD_OUTPUT_INVALID\",\n      message: \"Failed to parse output from @tailor-platform/sdk-codemod\",\n      details: result.stdout || \"(empty stdout)\",\n      suggestion: \"This is likely a bug. Please report it.\",\n      command: \"upgrade\",\n    });\n  }\n\n  // Step 4: Display results\n  // Emit structured data on stdout (honors --json via logger.out) and\n  // human-readable summary on stderr (via printUpgradeSummary).\n  logger.out(output);\n  printUpgradeSummary(output, options.dryRun);\n\n  if (output.errors.length > 0) {\n    throw CLIError({\n      code: \"UPGRADE_COMPLETED_WITH_ERRORS\",\n      message: `Upgrade completed with ${output.errors.length} error(s)`,\n      suggestion: \"Review the errors above and re-run the upgrade after fixing the issues.\",\n      command: \"upgrade\",\n    });\n  }\n}\n"],"mappings":"8MAYA,eAAsB,uBAAuB,EAA6C,CACxF,IAAI,EAAM,EAAK,QAAQ,CAAW,EAGlC,OAAa,CACX,GAAI,CACF,IAAM,EAAU,EAAK,KAAK,EAAK,eAAgB,sBAAgB,EACzD,EAAM,MAAM,EAAgB,CAAO,EACzC,GAAI,EAAI,QAAS,OAAO,EAAI,OAC9B,MAAQ,CAER,CACA,IAAM,EAAS,EAAK,QAAQ,CAAG,EAC/B,GAAI,IAAW,EAAK,MACpB,EAAM,CACR,CACA,OAAO,IACT,CCZA,SAAS,oBAAoB,EAAmB,EAAuB,CACjE,IACF,EAAO,KAAK,GAAG,EAAO,KAAK,WAAW,EAAE,yBAAyB,EACjE,EAAO,IAAI,EAAE,GAGf,IAAM,EAAQ,EAAO,gBAAkB,EAAO,gBAAkB,EAAO,OAAO,OAK9E,GAJA,EAAO,KACL,qBAAqB,EAAO,QAAQ,GAAG,EAAO,gBAAgB,SAAS,EAAE,IAAI,EAAO,IAAI,GAAG,EAAO,gBAAgB,SAAS,EAAE,IAAI,EAAM,iBACzI,EAEI,EAAO,cAAc,OAAS,EAAG,CACnC,EAAO,IAAI,EAAE,EACb,EAAO,KACL,GAAG,EAAS,+BAAiC,iBAAiB,IAAI,EAAO,cAAc,OAAO,GAChG,EACA,IAAK,IAAM,KAAQ,EAAO,cACxB,EAAO,IAAI,KAAK,EAAO,KAAK,CAAI,GAAG,CAEvC,CAEA,GAAI,EAAO,SAAS,OAAS,EAAG,CAC9B,EAAO,IAAI,EAAE,EACb,EAAO,KAAK,4BAA4B,EAAO,SAAS,OAAO,GAAG,EAClE,IAAK,IAAM,KAAW,EAAO,SAC3B,EAAO,IAAI,KAAK,EAAO,QAAQ,GAAG,EAAE,GAAG,GAAS,CAEpD,CAE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