#!/usr/bin/env node
/**
 * 将静态资源复制到 dist/，便于直接部署到任意静态主机。
 * 产出: dist/index.html, dist/config.json, dist/lib/, dist/image/
 */
const fs = require('fs');
const path = require('path');

const ROOT = path.resolve(__dirname);
const DIST = path.join(ROOT, 'dist');

function copyFile(src, dest) {
  fs.mkdirSync(path.dirname(dest), { recursive: true });
  fs.copyFileSync(src, dest);
}

function copyDir(srcDir, destDir) {
  if (!fs.existsSync(srcDir)) return;
  fs.mkdirSync(destDir, { recursive: true });
  for (const name of fs.readdirSync(srcDir)) {
    const s = path.join(srcDir, name);
    const d = path.join(destDir, name);
    if (fs.statSync(s).isDirectory()) copyDir(s, d);
    else copyFile(s, d);
  }
}

function main() {
  if (fs.existsSync(DIST)) fs.rmSync(DIST, { recursive: true });
  fs.mkdirSync(DIST, { recursive: true });

  copyFile(path.join(ROOT, 'index.html'), path.join(DIST, 'index.html'));
  copyFile(path.join(ROOT, 'config.json'), path.join(DIST, 'config.json'));
  copyFile(path.join(ROOT, 'api.config.json'), path.join(DIST, 'api.config.json'));
  copyDir(path.join(ROOT, 'lib'), path.join(DIST, 'lib'));
  copyDir(path.join(ROOT, 'image'), path.join(DIST, 'image'));

  console.log('已构建到 dist/（前端静态），可直接部署。');
}

main();
