{"version":3,"file":"build-manifest.mjs","names":[],"sources":["../src/vite-plugin/build-manifest.ts"],"sourcesContent":["/**\n * Build a RawManifestResponse from local portal/ files.\n *\n * Reads the portal directory structure produced by `fluid portal pull` and\n * assembles it into the same shape the SDK expects from the\n * `/api/fluid_os/definitions/active` endpoint, so the existing\n * `transformManifestToRepAppData()` pipeline works unchanged.\n */\n\nimport { readFile, readdir } from \"node:fs/promises\";\nimport { join, basename, extname } from \"node:path\";\nimport { existsSync } from \"node:fs\";\nimport { createHash } from \"node:crypto\";\n\nimport type {\n  LocalScreen,\n  LocalTheme,\n  LocalNavigation,\n  LocalNavigationItem,\n  LocalProfile,\n} from \"../utils/transform.js\";\n\n// Types matching the RawManifestResponse shape from\n// packages/portal/sdk/src/transforms/index.ts\n\ninterface RawNavigationItem {\n  id: number;\n  label: string;\n  slug?: string;\n  icon?: string;\n  screen_id?: number;\n  parent_id?: number;\n  source?: string;\n  position: number;\n  children: RawNavigationItem[];\n}\n\ninterface RawNavigation {\n  id: number;\n  name?: string;\n  definition_id: number;\n  navigation_items?: RawNavigationItem[];\n}\n\ninterface RawTheme {\n  id: number;\n  config?: Record<string, unknown> | null;\n  active?: boolean | null;\n  name?: string | null;\n}\n\ninterface RawScreen {\n  id: number;\n  definition_id?: number;\n  name?: string | null;\n  slug?: string | null;\n  component_tree?: unknown;\n}\n\ninterface RawManifestProfile {\n  name?: string;\n  definition_id: number;\n  themes?: RawTheme[];\n  navigation?: RawNavigation;\n  mobile_navigation?: RawNavigation;\n}\n\ninterface RawManifestResponse {\n  manifest: {\n    definition_id: number;\n    published_version?: number;\n    screens?: RawScreen[];\n    profile?: RawManifestProfile;\n  };\n}\n\n// Synthetic ID generation\n\n/**\n * Generate a deterministic synthetic numeric ID from a string key.\n * Uses a CRC32-like hash so the same slug always produces the same ID\n * within a dev session, while avoiding collisions in practice.\n */\nfunction syntheticId(key: string): number {\n  const hash = createHash(\"md5\").update(key).digest();\n  // Read first 4 bytes as unsigned 32-bit integer, ensure positive\n  return hash.readUInt32BE(0);\n}\n\n/**\n * Slug reported for the profile this builder synthesises when a portal has no\n * `portal/profiles/` files. Not a real filename — it exists so a host offering\n * a profile switcher has something stable to key a selection on.\n */\nconst SYNTHETIC_PROFILE_SLUG = \"default\";\n\n// File reading helpers\n\n/**\n * Read and parse a JSON file, returning null on failure.\n */\nasync function readJsonFile<T>(filePath: string): Promise<T | null> {\n  try {\n    const content = await readFile(filePath, \"utf-8\");\n    return JSON.parse(content) as T;\n  } catch {\n    return null;\n  }\n}\n\n/**\n * Read all JSON files in a directory, returning an array of [slug, data] tuples.\n * Slug is the filename without extension.\n */\nasync function readJsonDir<T>(\n  dirPath: string,\n): Promise<Array<[slug: string, data: T]>> {\n  if (!existsSync(dirPath)) return [];\n\n  const entries = await readdir(dirPath, { withFileTypes: true });\n  const results: Array<[string, T]> = [];\n\n  for (const entry of entries) {\n    if (!entry.isFile() || extname(entry.name) !== \".json\") continue;\n\n    const slug = basename(entry.name, \".json\");\n    const data = await readJsonFile<T>(join(dirPath, entry.name));\n    if (data != null) {\n      results.push([slug, data]);\n    }\n  }\n\n  return results;\n}\n\n// Navigation item conversion\n\n/**\n * Convert local navigation items (which reference screens by slug)\n * back to the raw API format (which uses screen_id).\n */\nfunction toRawNavigationItems(\n  items: readonly LocalNavigationItem[],\n  screenSlugToId: Map<string, number>,\n  navigationSlug: string,\n  parentPath: readonly number[] = [],\n): RawNavigationItem[] {\n  return items.map((item, index) => {\n    const itemPath = [...parentPath, index];\n    const rawItem: RawNavigationItem = {\n      id:\n        item.id ??\n        syntheticId(`navigation-item:${navigationSlug}:${itemPath.join(\".\")}`),\n      label: item.label ?? \"Untitled\",\n      position: item.position ?? 0,\n      children: toRawNavigationItems(\n        item.children ?? [],\n        screenSlugToId,\n        navigationSlug,\n        itemPath,\n      ),\n    };\n\n    if (item.slug != null) rawItem.slug = item.slug;\n    if (item.icon != null) rawItem.icon = item.icon;\n    if (item.source != null) rawItem.source = item.source;\n    if (item.parent_id != null) rawItem.parent_id = item.parent_id;\n\n    // Resolve screen slug -> synthetic screen ID\n    if (item.screen != null) {\n      const screenId = screenSlugToId.get(item.screen);\n      if (screenId != null) {\n        rawItem.screen_id = screenId;\n      }\n    }\n\n    return rawItem;\n  });\n}\n\n// Main builder\n\nexport interface BuildManifestOptions {\n  /**\n   * Slug (filename stem) of the profile to assemble. Defaults to the profile\n   * flagged `default`, else the first one found — the historical behaviour.\n   *\n   * A definition can hold several profiles, each with its own navigation and\n   * themes, and hosts that let a user preview a profile other than their own\n   * (the fluid-admin builder, Mist's in-process preview) need to pick which\n   * one lands in the manifest. Unknown slugs fall back to the default rather\n   * than producing an empty profile, so a stale persisted selection can't\n   * blank a preview.\n   */\n  readonly profileSlug?: string;\n}\n\n/** A profile discovered on disk, for hosts that offer a profile switcher. */\nexport interface LocalProfileSummary {\n  readonly slug: string;\n  readonly name: string;\n  /** Stable synthetic id, derived from the slug — not a server id. */\n  readonly id: number;\n  readonly isDefault: boolean;\n}\n\n/**\n * List the profiles a portal directory defines, without building a manifest.\n *\n * Deliberately separate from `buildManifestFromLocalFiles` rather than widening\n * its return type: `portal-dev-plugin` serialises that function's result\n * directly as the `/api/app/manifest` response body, so any extra key would\n * leak into the wire payload.\n *\n * When `portal/profiles/` is absent or empty, reports the single synthetic\n * \"Default\" profile the builder falls back to, so a switcher never renders an\n * empty list for a scratch portal.\n */\nexport async function listLocalProfiles(\n  portalDir: string,\n): Promise<LocalProfileSummary[]> {\n  const profileEntries = await readJsonDir<LocalProfile>(\n    join(portalDir, \"profiles\"),\n  );\n\n  if (profileEntries.length === 0) {\n    return [\n      {\n        slug: SYNTHETIC_PROFILE_SLUG,\n        name: \"Default\",\n        id: syntheticId(`profile:${SYNTHETIC_PROFILE_SLUG}`),\n        isDefault: true,\n      },\n    ];\n  }\n\n  // Exactly one entry is flagged default, and it is the same one the builder\n  // would assemble. `buildManifestFromLocalFiles` resolves the default with\n  // `find(([, p]) => p.default) ?? profileEntries[0]` — first-wins, with a\n  // fallback to the first profile — so mirror both halves here. Local files\n  // are hand-edited and can easily claim zero or several defaults; leaving\n  // either case unnormalised would make a host's initial selection disagree\n  // with the manifest it actually receives, decided by directory order.\n  const defaultIndex = Math.max(\n    profileEntries.findIndex(([, profile]) => profile.default === true),\n    0,\n  );\n\n  return profileEntries.map(([slug, profile], index) => ({\n    slug,\n    name: profile.name ?? slug,\n    id: syntheticId(`profile:${slug}`),\n    isDefault: index === defaultIndex,\n  }));\n}\n\n/**\n * Build a `RawManifestResponse` from local portal/ files.\n *\n * The returned object matches the shape from `/api/fluid_os/definitions/active`\n * so the SDK's `transformManifestToRepAppData()` works without modification.\n */\nexport async function buildManifestFromLocalFiles(\n  portalDir: string,\n  options: BuildManifestOptions = {},\n): Promise<RawManifestResponse> {\n  // 1. Read definition.json\n  const definition = await readJsonFile<{ name?: string }>(\n    join(portalDir, \"definition.json\"),\n  );\n  const definitionName = definition?.name ?? \"Local Dev\";\n  const definitionId = syntheticId(`definition:${definitionName}`);\n\n  // 2. Read all resource files in parallel\n  const [screenEntries, themeEntries, navEntries, profileEntries] =\n    await Promise.all([\n      readJsonDir<LocalScreen>(join(portalDir, \"screens\")),\n      readJsonDir<LocalTheme>(join(portalDir, \"themes\")),\n      readJsonDir<LocalNavigation>(join(portalDir, \"navigations\")),\n      readJsonDir<LocalProfile>(join(portalDir, \"profiles\")),\n    ]);\n\n  // 3. Build screen slug -> synthetic ID map\n  const screenSlugToId = new Map<string, number>();\n  for (const [slug] of screenEntries) {\n    screenSlugToId.set(slug, syntheticId(`screen:${slug}`));\n  }\n\n  // 4. Build raw screens\n  const rawScreens: RawScreen[] = screenEntries.map(([slug, screen]) => ({\n    id: screenSlugToId.get(slug)!,\n    definition_id: definitionId,\n    name: screen.name,\n    slug,\n    component_tree: screen.component_tree,\n  }));\n\n  // 5. Build raw themes (keyed by slug for profile lookup)\n  const themeSlugToRaw = new Map<string, RawTheme>();\n  for (const [slug, theme] of themeEntries) {\n    themeSlugToRaw.set(slug, {\n      id: syntheticId(`theme:${slug}`),\n      name: theme.name,\n      config: { ...theme.config },\n      active: theme.active,\n    });\n  }\n  const rawThemes = Array.from(themeSlugToRaw.values());\n\n  // 6. Build navigation slug -> synthetic ID map\n  const navSlugToId = new Map<string, number>();\n  for (const [slug] of navEntries) {\n    navSlugToId.set(slug, syntheticId(`navigation:${slug}`));\n  }\n\n  // Build raw navigations (keyed by slug for profile lookup)\n  const rawNavigations = new Map<string, RawNavigation>();\n  for (const [slug, nav] of navEntries) {\n    rawNavigations.set(slug, {\n      id: navSlugToId.get(slug)!,\n      name: nav.name,\n      definition_id: definitionId,\n      navigation_items: toRawNavigationItems(\n        nav.navigation_items ?? [],\n        screenSlugToId,\n        slug,\n      ),\n    });\n  }\n\n  // 7. Pick the profile to assemble: the requested slug when it resolves,\n  //    else the one flagged `default`, else the first. An unknown requested\n  //    slug falls back rather than yielding an empty profile, so a stale\n  //    persisted selection in a host can never blank the manifest.\n  const requestedProfile =\n    options.profileSlug != null\n      ? (profileEntries.find(([slug]) => slug === options.profileSlug)?.[1] ??\n        null)\n      : null;\n\n  const defaultProfile =\n    requestedProfile ??\n    profileEntries.find(([, p]) => p.default)?.[1] ??\n    profileEntries[0]?.[1] ??\n    null;\n\n  // 8. Assemble the profile for the manifest\n  let rawProfile: RawManifestProfile | undefined;\n  if (defaultProfile) {\n    // Resolve navigation slugs to raw navigation objects\n    const navigation = defaultProfile.navigation\n      ? rawNavigations.get(defaultProfile.navigation)\n      : undefined;\n\n    const mobileNavigation = defaultProfile.mobile_navigation\n      ? rawNavigations.get(defaultProfile.mobile_navigation)\n      : undefined;\n\n    // Filter themes to those referenced by the profile (by slug)\n    const profileThemeSet = new Set(defaultProfile.themes);\n    const profileThemes =\n      profileThemeSet.size > 0\n        ? Array.from(profileThemeSet)\n            .map((slug) => themeSlugToRaw.get(slug))\n            .filter((t): t is RawTheme => t != null)\n        : rawThemes; // If no themes specified in profile, include all\n\n    rawProfile = {\n      name: defaultProfile.name,\n      definition_id: definitionId,\n      themes: profileThemes,\n      ...(navigation ? { navigation } : {}),\n      ...(mobileNavigation ? { mobile_navigation: mobileNavigation } : {}),\n    };\n  } else if (rawThemes.length > 0 || rawNavigations.size > 0) {\n    // No profile files but we have themes/navs — create a synthetic profile\n    const firstNav =\n      rawNavigations.size > 0\n        ? rawNavigations.values().next().value\n        : undefined;\n\n    rawProfile = {\n      name: \"Default\",\n      definition_id: definitionId,\n      themes: rawThemes,\n      ...(firstNav ? { navigation: firstNav } : {}),\n    };\n  }\n\n  // 9. Return assembled manifest\n  return {\n    manifest: {\n      definition_id: definitionId,\n      published_version: 0,\n      screens: rawScreens,\n      ...(rawProfile ? { profile: rawProfile } : {}),\n    },\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmFA,SAAS,YAAY,KAAqB;AAGxC,QAFa,WAAW,MAAM,CAAC,OAAO,IAAI,CAAC,QAAQ,CAEvC,aAAa,EAAE;;;;;;;AAQ7B,MAAM,yBAAyB;;;;AAO/B,eAAe,aAAgB,UAAqC;AAClE,KAAI;EACF,MAAM,UAAU,MAAM,SAAS,UAAU,QAAQ;AACjD,SAAO,KAAK,MAAM,QAAQ;SACpB;AACN,SAAO;;;;;;;AAQX,eAAe,YACb,SACyC;AACzC,KAAI,CAAC,WAAW,QAAQ,CAAE,QAAO,EAAE;CAEnC,MAAM,UAAU,MAAM,QAAQ,SAAS,EAAE,eAAe,MAAM,CAAC;CAC/D,MAAM,UAA8B,EAAE;AAEtC,MAAK,MAAM,SAAS,SAAS;AAC3B,MAAI,CAAC,MAAM,QAAQ,IAAI,QAAQ,MAAM,KAAK,KAAK,QAAS;EAExD,MAAM,OAAO,SAAS,MAAM,MAAM,QAAQ;EAC1C,MAAM,OAAO,MAAM,aAAgB,KAAK,SAAS,MAAM,KAAK,CAAC;AAC7D,MAAI,QAAQ,KACV,SAAQ,KAAK,CAAC,MAAM,KAAK,CAAC;;AAI9B,QAAO;;;;;;AAST,SAAS,qBACP,OACA,gBACA,gBAC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