{"version":3,"file":"migrate-B6QBYDNX.mjs","sources":["../src/services/migrate.ts"],"sourcesContent":["import {\n  fetchPackageVersionFromNpmRegistry,\n  injectPnpmAllowBuilds,\n} from \"@powerhousedao/shared/clis\";\nimport { execSync } from \"node:child_process\";\nimport { readFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { detect, resolveCommand } from \"package-manager-detector\";\nimport type { MigrateArgs } from \"../types.js\";\n\nfunction getBundledPhCliVersion(): string | undefined {\n  let dir = dirname(fileURLToPath(import.meta.url));\n  for (let i = 0; i < 5; i++) {\n    try {\n      const pkg = JSON.parse(\n        readFileSync(join(dir, \"package.json\"), \"utf8\"),\n      ) as { name?: string; version?: string };\n      if (pkg.name === \"@powerhousedao/ph-cli\") return pkg.version;\n    } catch {\n      // keep walking\n    }\n    dir = dirname(dir);\n  }\n}\n\nexport function resolveCodegenVersion(\n  codegenMod: Record<string, unknown>,\n): string | undefined {\n  const getter = codegenMod.getCodegenVersion;\n  if (typeof getter === \"function\") {\n    const v = (getter as () => unknown)();\n    if (typeof v === \"string\") return v;\n  }\n  return undefined;\n}\n\nexport function assertCodegenMatchesBundled(args: {\n  codegenVersion: string | undefined;\n  bundledVersion: string | undefined;\n  force: boolean;\n}): void {\n  const { codegenVersion, bundledVersion, force } = args;\n  if (force) return;\n  if (!codegenVersion) {\n    throw new Error(\n      `@powerhousedao/codegen is older than this ph-cli expects (no version export). ` +\n        `Reinstall ph-cli to bring a matching codegen, or re-run with --force.`,\n    );\n  }\n  if (bundledVersion && codegenVersion !== bundledVersion) {\n    throw new Error(\n      `@powerhousedao/codegen@${codegenVersion} does not match ph-cli@${bundledVersion}. ` +\n        `Reinstall to align versions, or re-run with --force.`,\n    );\n  }\n}\n\nexport async function startMigrate({\n  versionPositional,\n  version,\n  force,\n  debug,\n}: MigrateArgs) {\n  const requested = versionPositional ?? version;\n  if (debug) console.log(`[migrate] requested version: ${requested}`);\n\n  let targetVersion: string | undefined;\n  try {\n    targetVersion = await fetchPackageVersionFromNpmRegistry(\n      `@powerhousedao/ph-cli@${requested}`,\n    );\n  } catch (error) {\n    const reason = error instanceof Error ? error.message : String(error);\n    if (!force) {\n      throw new Error(\n        `Failed to resolve \"${requested}\" from the npm registry: ${reason}\\nRe-run with --force to migrate using the installed version.`,\n        { cause: error },\n      );\n    }\n    if (debug) {\n      console.log(\n        `[migrate] failed to resolve target version, --force is set, falling back to bundled codegen: ${reason}`,\n      );\n    }\n  }\n\n  const bundledVersion = getBundledPhCliVersion();\n  if (debug) {\n    console.log(\n      `[migrate] resolved target version: ${targetVersion ?? \"(unknown)\"}`,\n    );\n    console.log(\n      `[migrate] current ph-cli version: ${bundledVersion ?? \"(unknown)\"}`,\n    );\n  }\n\n  if (!targetVersion || force || targetVersion === bundledVersion) {\n    if (debug) console.log(`[migrate] running migrate from bundled codegen`);\n    const codegenMod = await import(\"@powerhousedao/codegen\");\n    const codegenVersion = resolveCodegenVersion(\n      codegenMod as unknown as Record<string, unknown>,\n    );\n    assertCodegenMatchesBundled({\n      codegenVersion,\n      bundledVersion,\n      force: Boolean(force),\n    });\n    console.log(\n      `Running migrate with @powerhousedao/codegen@${codegenVersion ?? \"unknown\"}`,\n    );\n    await codegenMod.migrate(targetVersion ?? requested);\n    return;\n  }\n\n  const agent = (await detect())?.agent ?? \"npm\";\n  const resolved = resolveCommand(agent, \"execute\", [\n    `@powerhousedao/ph-cli@${targetVersion}`,\n    \"migrate\",\n    \"--version\",\n    targetVersion,\n    ...(debug ? [\"--debug\"] : []),\n  ]);\n  if (!resolved) {\n    throw new Error(\n      `Failed to resolve execute command for package manager \"${agent}\".`,\n    );\n  }\n\n  injectPnpmAllowBuilds(agent, resolved);\n\n  const command = `${resolved.command} ${resolved.args.join(\" \")}`;\n  if (debug) {\n    console.log(`[migrate] detected package manager: ${agent}`);\n    console.log(`[migrate] re-executing: ${command}`);\n  }\n  execSync(command, { stdio: \"inherit\" });\n}\n"],"names":[],"mappings":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