const fs = require('fs');
const path = require('path');
const child_process = require('child_process');

const pkgPath = path.resolve(__dirname, '../package.json');
const pkg = require(pkgPath);

// 自动递增版本号
const [major, minor, patch] = pkg.version.split('.').map(Number);
const newVersion = `${major}.${minor}.${patch + 1}`;
pkg.version = newVersion;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));

// 获取 Git hash 和构建时间
const gitHash = child_process.execSync('git rev-parse --short HEAD').toString().trim();
const buildTime = new Date().toISOString();

// 写入 src/versionEnv.ts
const content = `// 由 scripts/writeVersion.js 自动生成
export const VERSION = "${newVersion}";
export const GIT_HASH = "${gitHash}";
export const BUILD_TIME = "${buildTime}";
`;

fs.writeFileSync(path.resolve(__dirname, '../src/versionEnv.ts'), content);
console.log('✅ 自动写入 src/versionEnv.ts 完成');
