{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../src/init/frameworkSetup/tanstackStart/index.ts"],"sourcesContent":["import { join } from 'node:path';\nimport * as ANSIColors from '@intlayer/config/colors';\nimport { colorize, colorizePath, logger, v, x } from '@intlayer/config/logger';\nimport {\n  ensureDirectory,\n  exists,\n  readFileFromRoot,\n  writeFileToRoot,\n} from '../../utils/fileSystem';\nimport { findAppFile } from '../nextAppRouter/detect';\nimport type { FrameworkAdapter, FrameworkSetupContext } from '../types';\nimport { detectTanStackRoutesDir, hasTanStackStartDeps } from './detect';\nimport { getLocaleSegment, restructureRoutesIntoLocale } from './restructure';\nimport {\n  buildLocaleRouteTemplate,\n  buildRootTemplateJs,\n  buildRootTemplateTs,\n} from './templates';\nimport { type TransformResult, wrapRootWithProvider } from './transforms';\n\n/** Logs the outcome of the root-document provider-wrap transform. */\nconst logTransformOutcome = (\n  filePath: string,\n  result: TransformResult\n): void => {\n  switch (result.status) {\n    case 'wrapped':\n      logger(`${v} Added Intlayer provider to ${colorizePath(filePath)}`);\n      break;\n    case 'already':\n      logger(\n        `${v} ${colorizePath(filePath)} already wraps the Intlayer provider`\n      );\n      break;\n    case 'skipped':\n      logger(\n        `${x} Could not safely add the Intlayer provider to ${colorizePath(filePath)} — please wrap the document's {children} with <IntlayerProvider locale={locale}> manually.`,\n        { level: 'warn' }\n      );\n      break;\n  }\n};\n\n/** Transforms an existing file in place, writing it back only when changed. */\nconst transformExistingFile = async (\n  rootDir: string,\n  filePath: string,\n  transform: (code: string) => TransformResult\n): Promise<void> => {\n  const code = await readFileFromRoot(rootDir, filePath);\n  const result = transform(code);\n  if (result.status === 'wrapped') {\n    await writeFileToRoot(rootDir, filePath, result.code);\n  }\n  logTransformOutcome(filePath, result);\n};\n\n/** Creates a file from a template only when it does not already exist. */\nconst createIfMissing = async (\n  rootDir: string,\n  filePath: string,\n  content: string,\n  description: string\n): Promise<boolean> => {\n  if (await exists(rootDir, filePath)) return false;\n  await writeFileToRoot(rootDir, filePath, content);\n  logger(`${v} Created ${description} ${colorizePath(filePath)}`);\n  return true;\n};\n\n/**\n * TanStack Start adapter. Restructures the file-based routes under a `{-$locale}`\n * segment, scaffolds the locale segment route, and wraps the root document with\n * the Intlayer provider — all idempotently and without overwriting user code it\n * cannot confidently transform.\n */\nexport const tanStackStartAdapter: FrameworkAdapter = {\n  name: 'TanStack Start',\n\n  detect: async ({ rootDir, allDeps }) => {\n    if (!hasTanStackStartDeps(allDeps)) return false;\n    return (await detectTanStackRoutesDir(rootDir)) !== null;\n  },\n\n  setup: async ({\n    rootDir,\n    useTypeScript,\n    routingMode,\n  }: FrameworkSetupContext) => {\n    const routesInfo = await detectTanStackRoutesDir(rootDir);\n    if (!routesInfo) return;\n\n    const { routesDir } = routesInfo;\n    const scriptExtension = useTypeScript ? 'tsx' : 'jsx';\n\n    // `prefix-all` needs the required `$locale` segment; every other mode uses\n    // the optional `{-$locale}` segment so the default locale stays prefix-free.\n    const targetLocaleSegment = getLocaleSegment(routingMode);\n\n    logger(\n      colorize('Setting up TanStack Start integration...', ANSIColors.CYAN)\n    );\n\n    // 1. Move routable route entries under the locale segment (idempotent).\n    const restructureResult = await restructureRoutesIntoLocale(\n      rootDir,\n      routesDir,\n      targetLocaleSegment\n    );\n    if (restructureResult.status === 'moved') {\n      logger(\n        `${v} Restructured routes under ${colorizePath(join(routesDir, targetLocaleSegment))}: ${restructureResult.movedEntries\n          .map((entry) => colorize(entry, ANSIColors.MAGENTA))\n          .join(', ')}`\n      );\n    } else if (restructureResult.status === 'already-structured') {\n      logger(\n        `${v} ${colorizePath(join(routesDir, restructureResult.localeSegment))} already exists, skipping restructure`\n      );\n    }\n\n    // Scaffold the locale route for the segment actually in use: a pre-existing\n    // segment (e.g. a prefix-all `$locale` the user already set up) wins over the\n    // target, so we never create a second, conflicting locale segment.\n    const activeLocaleSegment =\n      restructureResult.status === 'already-structured'\n        ? restructureResult.localeSegment\n        : targetLocaleSegment;\n\n    const localeDir = join(routesDir, activeLocaleSegment);\n    await ensureDirectory(rootDir, localeDir);\n\n    // 2. Locale segment route (`<segment>/route.tsx`) — create when absent.\n    await createIfMissing(\n      rootDir,\n      join(localeDir, `route.${scriptExtension}`),\n      buildLocaleRouteTemplate(activeLocaleSegment),\n      'locale route'\n    );\n\n    // 3. Root document — transform an existing `__root`, else scaffold one.\n    const existingRoot = await findAppFile(rootDir, routesDir, '__root');\n    if (existingRoot) {\n      await transformExistingFile(rootDir, existingRoot, (code) =>\n        wrapRootWithProvider(code, activeLocaleSegment)\n      );\n    } else {\n      await createIfMissing(\n        rootDir,\n        join(routesDir, `__root.${scriptExtension}`),\n        useTypeScript\n          ? buildRootTemplateTs(activeLocaleSegment)\n          : buildRootTemplateJs(activeLocaleSegment),\n        'root document'\n      );\n    }\n  },\n};\n"],"mappings":";;;;;;;;;;;;AAqBA,MAAM,uBACJ,UACA,WACS;CACT,QAAQ,OAAO,QAAf;EACE,KAAK;GACH,OAAO,GAAG,EAAE,8BAA8B,aAAa,QAAQ,GAAG;GAClE;EACF,KAAK;GACH,OACE,GAAG,EAAE,GAAG,aAAa,QAAQ,EAAE,qCACjC;GACA;EACF,KAAK,WACH,OACE,GAAG,EAAE,iDAAiD,aAAa,QAAQ,EAAE,6FAC7E,EAAE,OAAO,OAAO,CAClB;CAEJ;AACF;;AAGA,MAAM,wBAAwB,OAC5B,SACA,UACA,cACkB;CAElB,MAAM,SAAS,UAAU,MADN,iBAAiB,SAAS,QAAQ,CACxB;CAC7B,IAAI,OAAO,WAAW,WACpB,MAAM,gBAAgB,SAAS,UAAU,OAAO,IAAI;CAEtD,oBAAoB,UAAU,MAAM;AACtC;;AAGA,MAAM,kBAAkB,OACtB,SACA,UACA,SACA,gBACqB;CACrB,IAAI,MAAM,OAAO,SAAS,QAAQ,GAAG,OAAO;CAC5C,MAAM,gBAAgB,SAAS,UAAU,OAAO;CAChD,OAAO,GAAG,EAAE,WAAW,YAAY,GAAG,aAAa,QAAQ,GAAG;CAC9D,OAAO;AACT;;;;;;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