#!/usr/bin/env node
/**
 * Generates src/client/utils/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 clientOutputPath = join(__dirname, "../src/client/utils/version.ts");
const serverOutputPath = join(__dirname, "../src/server/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 inspector 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 getInspectorVersion(): string {
  return VERSION;
}
`;

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