{"version":3,"file":"index.cjs","names":["path","fs","createRequire"],"sources":["../../../src/builders/web/index.ts"],"sourcesContent":["import fs from 'node:fs';\nimport { createRequire } from 'node:module';\nimport path from 'node:path';\n\nconst WEB_PACKAGE = '@agimon-ai/doompi-web';\nconst WEB_PACKAGE_ROOT_ENV = 'DOOMPI_WEB_PACKAGE_ROOT';\n\nexport interface WebBundleSyncInput {\n  repoRoot: string;\n  /** Resolved extension entry paths from the sync state; package roots derive from them. */\n  resolvedEntries: Record<string, string>;\n  environment?: NodeJS.ProcessEnv;\n  /** Immutable generation directory that receives web/, generated/, and registries. */\n  outputDirectory: string;\n  onNotice?: (message: string) => void;\n  /** Test seam over the dynamic import of the web builder. */\n  importBundler?: (url: string) => Promise<unknown>;\n  /** Test seam over package resolution, which native require makes environment-dependent. */\n  resolveWebRoot?: (repoRoot: string, environment: NodeJS.ProcessEnv) => string | undefined;\n}\n\nexport type WebBundleSyncResult =\n  | { status: 'bundled'; assetsDir: string; pluginIds: string[] }\n  | { status: 'skipped'; reason: string }\n  | { status: 'failed'; reason: string };\n\ninterface BundlerModule {\n  bundleCockpitWeb?: (options: {\n    hostRoot: string;\n    pluginRoots: readonly string[];\n    outDir: string;\n    onNotice?: (message: string) => void;\n  }) => Promise<{ assetsDir: string; pluginIds: string[] }>;\n}\n\n/** Walks from a resolved module path up to the directory owning its package.json. */\nfunction packageRootOf(entryPath: string): string | undefined {\n  let dir = path.dirname(entryPath);\n  for (;;) {\n    if (fs.existsSync(path.join(dir, 'package.json'))) return dir;\n    const parent = path.dirname(dir);\n    if (parent === dir) return undefined;\n    dir = parent;\n  }\n}\n\nfunction resolveWebPackageRoot(repoRoot: string, environment: NodeJS.ProcessEnv): string | undefined {\n  const override = environment[WEB_PACKAGE_ROOT_ENV];\n  if (override !== undefined && override !== '') return path.resolve(override);\n  try {\n    return path.dirname(createRequire(path.join(repoRoot, 'package.json')).resolve(`${WEB_PACKAGE}/package.json`));\n  } catch {\n    try {\n      return path.dirname(createRequire(import.meta.url).resolve(`${WEB_PACKAGE}/package.json`));\n    } catch {\n      return undefined;\n    }\n  }\n}\n\n/** Builds the cockpit bundle directly inside an unpublished sync generation. */\nexport async function syncWebBundle(input: WebBundleSyncInput): Promise<WebBundleSyncResult> {\n  const environment = input.environment ?? process.env;\n  const notice = input.onNotice ?? ((): void => {});\n  const webRoot = (input.resolveWebRoot ?? resolveWebPackageRoot)(input.repoRoot, environment);\n  if (webRoot === undefined || (!input.resolveWebRoot && !fs.existsSync(path.join(webRoot, 'package.json')))) {\n    return { status: 'skipped', reason: `${WEB_PACKAGE} is not installed; no web composition is published` };\n  }\n  const pluginRoots = [\n    ...new Set(\n      Object.values(input.resolvedEntries)\n        .map(packageRootOf)\n        .filter((root): root is string => root !== undefined),\n    ),\n  ];\n\n  try {\n    const bundler = (\n      input.importBundler\n        ? await input.importBundler(new URL('./bundle.mjs', import.meta.url).href)\n        : await import('./bundle')\n    ) as BundlerModule;\n    if (typeof bundler.bundleCockpitWeb !== 'function') {\n      return { status: 'failed', reason: 'Web builder exports no bundleCockpitWeb' };\n    }\n    fs.rmSync(input.outputDirectory, { recursive: true, force: true });\n    fs.mkdirSync(input.outputDirectory, { recursive: true });\n    const result = await bundler.bundleCockpitWeb({\n      hostRoot: webRoot,\n      pluginRoots,\n      outDir: input.outputDirectory,\n      onNotice: notice,\n    });\n    return {\n      status: 'bundled',\n      assetsDir: result.assetsDir,\n      pluginIds: result.pluginIds,\n    };\n  } catch (error) {\n    return { status: 'failed', reason: error instanceof Error ? error.message : String(error) };\n  }\n}\n"],"mappings":";;;;;;;AAIA,MAAM,cAAc;AACpB,MAAM,uBAAuB;;AA+B7B,SAAS,cAAc,WAAuC;CAC5D,IAAI,MAAMA,UAAAA,QAAK,QAAQ,SAAS;CAChC,SAAS;EACP,IAAIC,QAAAA,QAAG,WAAWD,UAAAA,QAAK,KAAK,KAAK,cAAc,CAAC,GAAG,OAAO;EAC1D,MAAM,SAASA,UAAAA,QAAK,QAAQ,GAAG;EA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