#!/usr/bin/env node
/**
 * Generates src/version.ts with the current package version.
 * Run this before build to ensure the version is embedded in the compiled output.
 */

import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const packagePath = join(__dirname, "../package.json");
const outputPath = join(__dirname, "../src/version.ts");

const pkg = JSON.parse(readFileSync(packagePath, "utf-8"));

const content = `// Auto-generated by scripts/generate-version.mjs - DO NOT EDIT
// This file is regenerated during build to embed the package version
export const VERSION = "${pkg.version}";

/**
 * Get the package version.
 * The version is embedded at build time via scripts/generate-version.mjs
 * Works in all environments (Node.js, browser, Cloudflare Workers, Deno, etc.)
 */
export function getPackageVersion(): string {
  return VERSION;
}
`;

writeFileSync(outputPath, content);
console.log(`Generated version.ts with version: ${pkg.version}`);
