// import fs from 'fs/promises'
// import path from 'path'
// import { fileURLToPath } from 'url'
// import dayjs from 'dayjs'
// import utc from 'dayjs/plugin/utc.js'
// import timezone from 'dayjs/plugin/timezone.js'
const fs = require('fs').promises; // 或 require('fs/promises')
const path = require('path');
// const { fileURLToPath } = require('url');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc.js');
const timezone = require('dayjs/plugin/timezone.js');

dayjs.extend(utc)
dayjs.extend(timezone)

// const __filename = fileURLToPath(import.meta.url)
// const __dirname = path.dirname(__filename)

async function generateVersion() {
  try {
    const pkgBuffer = await fs.readFile(path.resolve(__dirname, 'package.json'))
    const pkg = JSON.parse(pkgBuffer.toString())
    
    const now = dayjs().tz("Asia/Shanghai")
    
    const versionData = {
      version: pkg.version,
      timestamp: now.format("YYYY-MM-DDTHH:mm:ss.SSS"),
      commitHash: process.env.GIT_COMMIT_HASH || 'dev-build'
    }
    
    await fs.writeFile(
      path.resolve(__dirname, 'public', 'version.json'),
      JSON.stringify(versionData, null, 2)
    )
    
    console.log('✅ 版本文件已生成:', versionData)
  } catch (error) {
    console.error('❌ 版本生成失败:', error)
    process.exit(1)
  }
}

generateVersion()