var fs = require("fs");
var path = require("path");

var dir1 = path.join(__dirname, "../dist/ReactShim.js");
var str = fs.readFileSync(dir1, "utf-8");
var text = str
    .replace(/Object\.freeze/g, "extend")
    .replace(/\/\/freeze_start[\s\S]+?freeze_end/, "");


fs.writeFileSync(dir1, text, { encoding: "utf8" });
console.log("对ReactShim瘦身完毕"); // eslint-disable-line

var dir2 = path.join(__dirname, "../dist/React.js");
var str2 = fs.readFileSync(dir2, "utf-8");
var text2 = str2
    .replace(/Object\.freeze/g, "extend")
    .replace(/\/\/freeze_start([\s\S]+?)freeze_end/, "");

fs.writeFileSync(dir2, text2, { encoding: "utf8" });
//fs.writeFileSync( path.join(__dirname, "../../draft/node_modules/anujs/dist/React.js"), text2, { encoding: "utf8" });
fs.writeFileSync( path.join(__dirname, "../../antd-test/node_modules/anujs/dist/React.js"), text2, { encoding: "utf8" });

//fs.writeFileSync( path.join(__dirname, "../../yo-demo/node_modules/anujs/dist/React.js"), text2, { encoding: "utf8" });
//fs.writeFileSync( path.join(__dirname, "../../yo-router/node_modules/anujs/dist/React.js"), text2, { encoding: "utf8" });

console.log("对React瘦身完毕"); // eslint-disable-line

var dir3 = path.join(__dirname, "../dist/ReactIE.js");
var str3 = fs.readFileSync(dir3, "utf-8");
var text3 = str3
    .replace(/Object\.freeze/g, "extend")
    .replace(/\/\/freeze_start([\s\S]+?)freeze_end/, "");

fs.writeFileSync(dir3, text3, { encoding: "utf8" });
console.log("对ReactIE瘦身完毕"); // eslint-disable-line


var dir4 = path.join(__dirname, "../dist/ReactSelection.js");
var str4 = fs.readFileSync(dir3, "utf-8");
var text4 = str4
    .replace(/Object\.freeze/g, "extend")
    .replace(/\/\/freeze_start([\s\S]+?)freeze_end/, "");

fs.writeFileSync(dir4, text4, { encoding: "utf8" });
//fs.writeFileSync( path.join(__dirname, "../../antd-test/node_modules/anujs/dist/ReactSelection.js"), text2, { encoding: "utf8" });

console.log("对ReactSelection瘦身完毕"); // eslint-disable-line


const fs = require("fs-extra");
const path = require("path");
const exec = require("child_process").exec;
const ora = require("ora");

const anuPath = path.join(__dirname, "../../anu");
const qreactPath = path.join(__dirname, "../");
const dirs = ["build", "lib", "src", "ssr"];
const spinner = ora("开始同步").start();

function empty(dir) {
  return new Promise((resolve, reject) => {
    fs.emptyDir(path.join(qreactPath, dir), err => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
}

function copy(dir) {
  return new Promise((resolve, reject) => {
    fs.copy(path.join(anuPath, dir), path.join(qreactPath, dir), err => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
}

function emptyDirs(dirs) {
  return Promise.all(dirs.map(dir => empty(dir)));
}

function copyDirs(dirs) {
  return Promise.all(dirs.map(dir => copy(dir)));
}

function build() {
  return new Promise((resolve, reject) => {
    exec("npm run build", err => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
}

async function start() {
  try {
    await emptyDirs(dirs);
    spinner.succeed("已清除 QReact 下目录");
    await copyDirs(dirs);
    spinner.succeed("已复制 anujs 至 QReact");
    spinner.start("正在编译...");
    await build();
    spinner.succeed("已完成编译");
  } catch (e) {
    console.error(e) // eslint-disable-line
    spinner.fail("同步失败");
  }
}

start();

