{"version":3,"file":"generateBundlerConfig.js","names":[],"sources":["esm/cli/generateBundlerConfig.js"],"sourcesContent":["import { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport path from \"node:path\";\nimport colors from \"picocolors\";\nimport { getRouterRootFromOneOptions } from \"../utils/getRouterRootFromOneOptions\";\nconst ONE_GENERATED_MARKER = \"@one/generated bundler-config\";\nfunction buildBabelConfigContent({\n  eject,\n  options\n}) {\n  const header = eject ? `// you own this file. edit freely. \\`one\\` will not regenerate it.\n// delegates to one/babel-preset which holds the canonical plugin chain.\n` : `// ${ONE_GENERATED_MARKER}\n//\n// auto-generated by \\`one metro-eject\\`. delegates to one/babel-preset so\n// Metro uses the same router/setup options as \\`one dev\\` and \\`one build\\`.\n//\n// to customize, delete this header and edit freely. re-runs will then\n// leave this file alone.\n`;\n  return `${header}\nconst oneBabelPreset = require('one/babel-preset')\nconst preset = oneBabelPreset.default || oneBabelPreset\nconst oneBundlerOptions = ${serializeBundlerConfigOptions(options)}\n\nmodule.exports = function (api) {\n  return preset(api, oneBundlerOptions)\n}\n`;\n}\nfunction buildMetroConfigContent({\n  eject,\n  options\n}) {\n  const header = eject ? `// you own this file. edit freely. \\`one\\` will not regenerate it.\n// withOne() invokes the same Metro pipeline One uses for production bundles.\n` : `// ${ONE_GENERATED_MARKER}\n//\n// auto-generated by \\`one metro-eject\\`. delegates to one/metro-config,\n// which invokes the same metro pipeline One uses for production native\n// bundles with your router and setup options.\n//\n// to customize, delete this header and edit freely. re-runs will then\n// leave this file alone.\n`;\n  return `${header}\nconst { withOne } = require('one/metro-config')\nconst oneBundlerOptions = ${serializeBundlerConfigOptions(options)}\n\nmodule.exports = withOne(__dirname, oneBundlerOptions)\n`;\n}\nconst FILES = [\n  {\n    name: \"babel.config.cjs\",\n    getContent: buildBabelConfigContent,\n    conflicting: [\"babel.config.js\", \"babel.config.mjs\", \".babelrc\", \".babelrc.js\"]\n  },\n  {\n    name: \"metro.config.cjs\",\n    getContent: buildMetroConfigContent,\n    conflicting: [\"metro.config.js\", \"metro.config.mjs\"]\n  }\n];\nfunction stripUndefined(value) {\n  if (Array.isArray(value)) {\n    return value.map(stripUndefined);\n  }\n  if (value && typeof value === \"object\") {\n    return Object.fromEntries(\n      Object.entries(value).filter(([, entry]) => entry !== void 0).map(([key, entry]) => [key, stripUndefined(entry)])\n    );\n  }\n  return value;\n}\nfunction assertSerializable(value, keyPath = \"one bundler options\") {\n  if (typeof value === \"function\" || typeof value === \"symbol\" || typeof value === \"bigint\") {\n    throw new Error(\n      `[one] ${keyPath} must be JSON-serializable to generate Babel/Metro config files. Move function-valued native linking/customization into an ejected config.`\n    );\n  }\n  if (Array.isArray(value)) {\n    value.forEach((entry, index) => assertSerializable(entry, `${keyPath}[${index}]`));\n    return;\n  }\n  if (value && typeof value === \"object\") {\n    for (const [key, entry] of Object.entries(value)) {\n      assertSerializable(entry, `${keyPath}.${key}`);\n    }\n  }\n}\nfunction serializeBundlerConfigOptions(options) {\n  const clean = stripUndefined(options);\n  assertSerializable(clean);\n  return JSON.stringify(clean, null, 2);\n}\nfunction getBundlerConfigOptionsFromOneOptions(oneOptions = {}) {\n  return stripUndefined({\n    routerRoot: getRouterRootFromOneOptions(oneOptions),\n    ignoredRouteFiles: oneOptions.router?.ignoredRouteFiles,\n    linking: oneOptions.router?.linking,\n    setupFile: oneOptions.setupFile\n  });\n}\nfunction generateBundlerConfig(args = {}) {\n  const cwd = path.resolve(args.cwd ?? process.cwd());\n  const force = !!args.force;\n  const check = !!args.check;\n  const quiet = !!args.quiet;\n  const log = (msg) => {\n    if (!quiet) console.info(msg);\n  };\n  const warn = (msg) => {\n    if (!quiet) console.warn(msg);\n  };\n  const results = [];\n  const eject = !!args.eject;\n  const bundlerOptions = getBundlerConfigOptionsFromOneOptions(args.oneOptions);\n  for (const file of FILES) {\n    const filePath = path.join(cwd, file.name);\n    const targetContent = file.getContent({ eject, options: bundlerOptions });\n    const conflict = file.conflicting.find((alt) => existsSync(path.join(cwd, alt)));\n    if (conflict && !existsSync(filePath)) {\n      results.push({\n        filePath: path.join(cwd, conflict),\n        action: \"skipped-other-format\",\n        reason: `Found ${conflict}; not creating ${file.name}. To switch, delete ${conflict} and re-run with --force.`\n      });\n      warn(\n        colors.yellow(\n          `[one] found ${conflict}. Leaving it alone. Delete it and re-run with --force to switch to ${file.name}.`\n        )\n      );\n      continue;\n    }\n    if (!existsSync(filePath)) {\n      if (check) {\n        results.push({ filePath, action: \"would-write\" });\n        log(colors.yellow(`[one] missing: ${file.name}`));\n        continue;\n      }\n      writeFileSync(filePath, targetContent);\n      results.push({ filePath, action: \"wrote\" });\n      log(colors.green(`[one] wrote ${file.name}`));\n      continue;\n    }\n    const existing = readFileSync(filePath, \"utf8\");\n    if (existing === targetContent) {\n      results.push({ filePath, action: \"kept\" });\n      log(colors.dim(`[one] up to date: ${file.name}`));\n      continue;\n    }\n    const hasMarker = existing.includes(ONE_GENERATED_MARKER);\n    if (!hasMarker && !force) {\n      results.push({\n        filePath,\n        action: \"skipped-customized\",\n        reason: `${file.name} has been customized (no @one marker). Re-add the marker comment or pass --force to overwrite.`\n      });\n      warn(\n        colors.yellow(\n          `[one] ${file.name} appears customized. Skipping. Pass --force to overwrite.`\n        )\n      );\n      continue;\n    }\n    if (check) {\n      results.push({ filePath, action: \"would-overwrite\" });\n      log(colors.yellow(`[one] out of date: ${file.name}`));\n      continue;\n    }\n    writeFileSync(filePath, targetContent);\n    results.push({ filePath, action: \"wrote\" });\n    log(colors.green(`[one] updated ${file.name}`));\n  }\n  const acceptableAlways = /* @__PURE__ */ new Set([\n    \"wrote\",\n    \"kept\",\n    \"skipped-other-format\",\n    \"skipped-customized\"\n  ]);\n  const acceptableInCheck = /* @__PURE__ */ new Set([\n    \"kept\",\n    \"skipped-other-format\",\n    \"skipped-customized\"\n  ]);\n  const ok = (check ? acceptableInCheck : acceptableAlways).size ? results.every((r) => (check ? acceptableInCheck : acceptableAlways).has(r.action)) : false;\n  return { results, ok };\n}\nexport {\n  ONE_GEN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