import fs from "node:fs";
import path from "node:path";
import process from "node:process";

export interface VersionPluginOptions {
  version: string;
}

export function generateVersion(options: VersionPluginOptions) {
  return {
    name: "generate-version",
    closeBundle() {
      const versionInfo = {
        version: options.version,
        buildTime: new Date().toISOString(),
        environment: process.env.NODE_ENV,
      };

      // 写入到 dist 目录
      const distPath = path.resolve(process.cwd(), "dist/version.json");
      fs.writeFileSync(distPath, JSON.stringify(versionInfo, null, 2));
      console.log(`✓ Generated version.json: ${options.version}`);
    },
  };
}
