#!/usr/bin/env node
/**
 * Reads version from package.json and generates src/generated/version.ts.
 * Executed as a prebuild step so the compiled bundle always carries
 * the correct package version at runtime.
 */

import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const pkg = JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf-8'));

const outDir = resolve(root, 'src', 'generated');
mkdirSync(outDir, { recursive: true });

writeFileSync(
  resolve(outDir, 'version.ts'),
  `// Auto-generated by scripts/generate-version.js — do not edit\nexport const PACKAGE_VERSION = '${pkg.version}';\n`,
);
