{"version":3,"file":"index.cjs","names":["path","createHistoryOwnership","wantsHelp","historyExportHelp","exportV4ToV3"],"sources":["../../../../src/cli/commands/history-export/index.ts"],"sourcesContent":["import path from 'node:path';\n\nimport type { HistoryOwnership } from '@agimon-ai/doompi-core/history-import';\nimport { createHistoryOwnership } from '@agimon-ai/doompi-core/history-ownership';\nimport { exportV4ToV3 } from '@agimon-ai/doompi-core/v3-export';\n\nimport { wantsHelp } from '../../router';\nimport { historyExportHelp } from './help';\n\ntype ValueOption =\n  | '--source'\n  | '--source-path'\n  | '--destination'\n  | '--destination-path'\n  | '--report'\n  | '--report-path'\n  | '--state'\n  | '--state-path';\nconst VALUE_OPTIONS = new Map<ValueOption, keyof HistoryExportPaths>([\n  ['--source', 'sourcePath'],\n  ['--source-path', 'sourcePath'],\n  ['--destination', 'destinationPath'],\n  ['--destination-path', 'destinationPath'],\n  ['--report', 'reportPath'],\n  ['--report-path', 'reportPath'],\n  ['--state', 'statePath'],\n  ['--state-path', 'statePath'],\n]);\n\ntype HistoryExportPaths = {\n  sourcePath?: string;\n  destinationPath?: string;\n  reportPath?: string;\n  statePath?: string;\n};\n\nexport interface HistoryExportOutput {\n  write(chunk: string): void;\n}\n\nfunction parsePaths(args: readonly string[], currentDirectory: string): HistoryExportPaths {\n  const paths: HistoryExportPaths = {};\n  const positional: string[] = [];\n  for (let index = 0; index < args.length; index += 1) {\n    const argument = args[index]!;\n    if (argument === '--') throw new Error('doompi history-export does not accept passthrough arguments');\n    const equals = argument.indexOf('=');\n    const option = equals < 0 ? argument : argument.slice(0, equals);\n    const key = VALUE_OPTIONS.get(option as ValueOption);\n    if (key !== undefined) {\n      const value = equals < 0 ? args[++index] : argument.slice(equals + 1);\n      if (!value) throw new Error(`${option} requires a value`);\n      if (paths[key] !== undefined) throw new Error(`${option} was specified more than once`);\n      paths[key] = value;\n      continue;\n    }\n    if (argument.startsWith('-')) throw new Error(`doompi history-export does not accept ${argument}`);\n    positional.push(argument);\n  }\n  if (paths.sourcePath === undefined) paths.sourcePath = positional.shift();\n  if (paths.destinationPath === undefined) paths.destinationPath = positional.shift();\n  if (positional.length > 0) throw new Error(`Unexpected history-export argument: ${positional[0]}`);\n  if (paths.sourcePath === undefined || paths.destinationPath === undefined) {\n    throw new Error('doompi history-export requires a v4 source and distinct v3 destination');\n  }\n  return {\n    sourcePath: path.resolve(currentDirectory, paths.sourcePath),\n    destinationPath: path.resolve(currentDirectory, paths.destinationPath),\n    ...(paths.reportPath === undefined ? {} : { reportPath: path.resolve(currentDirectory, paths.reportPath) }),\n    ...(paths.statePath === undefined ? {} : { statePath: path.resolve(currentDirectory, paths.statePath) }),\n  };\n}\n\n/** Exports one canonical v4 journal without starting the harness or Pi runtime. */\nexport async function runHistoryExport(\n  args: readonly string[],\n  _environment: NodeJS.ProcessEnv = process.env,\n  currentDirectory = process.cwd(),\n  output: HistoryExportOutput = process.stdout,\n  owner: HistoryOwnership = createHistoryOwnership(),\n): Promise<number> {\n  if (wantsHelp(args.slice(1))) {\n    output.write(historyExportHelp());\n    return 0;\n  }\n  const paths = parsePaths(args.slice(1), currentDirectory);\n  const result = await exportV4ToV3({\n    sourcePath: paths.sourcePath!,\n    destinationPath: paths.destinationPath!,\n    owner,\n    ...(paths.reportPath === undefined ? {} : { reportPath: paths.reportPath }),\n    ...(paths.statePath === undefined ? {} : { statePath: paths.statePath }),\n  });\n  output.write(\n    `${JSON.stringify({\n      status: result.status,\n      sourcePath: result.sourcePath,\n      destinationPath: result.destinationPath,\n      reportPath: result.reportPath,\n      statePath: result.statePath,\n      lossCount: result.losses.length,\n    })}\\n`,\n  );\n  return 0;\n}\n"],"mappings":";;;;;;;;AAkBA,MAAM,gCAAgB,IAAI,IAA2C;CACnE,CAAC,YAAY,YAAY;CACzB,CAAC,iBAAiB,YAAY;CAC9B,CAAC,iBAAiB,iBAAiB;CACnC,CAAC,sBAAsB,iBAAiB;CACxC,CAAC,YAAY,YAAY;CACzB,CAAC,iBAAiB,YAAY;CAC9B,CAAC,WA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