const fs = require("fs");
const path = require("path");
const ch = require("child_process");

const dirs = fs.readdirSync(path.join(__dirname, "../packages"));
const DOCS_PATH = path.join(__dirname, "../packages", "docs/docs/log");

ch.execSync("pnpm changeset version", { stdio: "inherit" });

dirs.forEach((dir) => {
  const context = path.join(__dirname, "../packages", dir);
  const CHANGELOG_PATH = context + "\\CHANGELOG.md";

  if (fs.existsSync(CHANGELOG_PATH)) {
    const target = path.join(DOCS_PATH, `${dir.toUpperCase()}_CHANGELOG.md`);
    fs.copyFile(CHANGELOG_PATH, target, (err) => {
      if (err) {
        console.error(`复制${dir}时出错:`, error);
        return;
      }
    });
  }
});

console.log("===== docs 同步 log 完成 =====");
