console.log("> postbuild");
const fs = require("fs");

console.log("process.env.IS_PROD", process.env.IS_PROD);

if (process.env.IS_PROD === "true") {
  console.log("> postbuild 将sourcemap文件改名，防止源码泄露");

  const revision = require("child_process")
    .execSync("git rev-parse HEAD")
    .toString()
    .trim()
    .slice(10, 17);
  const DIR = "./dist/js/";
  const sourcemaps = fs
    .readdirSync(DIR)
    .filter((v) => v.endsWith(".map"))
    .map((v) => DIR + v);
  for (const s of sourcemaps) {
    fs.renameSync(s, s.replace(".js.map", "-" + revision + ".js.map"));
    console.log(
      "  rename ",
      s,
      " => ",
      s.replace(".js.map", "-" + revision + ".js.map")
    );
  }

  console.log();
}
