const fs = require("node:fs");
const path = require("node:path");
const source = path.resolve(process.cwd(), "web/dist/menu.js");
const targetDir = path.resolve(process.cwd(), "src/generated");
const targetTs = path.join(targetDir, "menuBundle.ts");
const targetDts = path.join(targetDir, "menuBundle.d.ts");
if (!fs.existsSync(source)) {
console.error(
`[embed-menu-bundle] Source bundle missing at ${source}. Run "npm run build" before embedding.`
);
process.exit(1);
}
fs.mkdirSync(targetDir, { recursive: true });
const bundle = fs.readFileSync(source, "utf8");
const banner = `// AUTO-GENERATED FILE. DO NOT EDIT.\n// Generated by scripts/embed-menu-bundle.cjs\n\n`;
const tsContent = `${banner}export const MENU_WIDGET_BUNDLE = ${JSON.stringify(bundle)};\n`;
const dtsContent = `${banner}export declare const MENU_WIDGET_BUNDLE: string;\n`;
fs.writeFileSync(targetTs, tsContent);
fs.writeFileSync(targetDts, dtsContent);
console.log(`[embed-menu-bundle] Embedded widget bundle into ${targetTs}`);