const execa = require("execa");
const path = require("path");
const fs = require("fs-extra");

async function cleanDist() {
  const distPath = path.resolve(__dirname, "../dist");
  console.log(" -------- cleaning dist directory -------- ");
  await fs.remove(distPath);
  console.log(" -------- dist directory cleaned -------- ");
}

async function build() {
  const entryPath = path.resolve(__dirname, "../packages/rcs_5.0");
  const toolPath = path.resolve(__dirname, "../tool/app/web");
  const themePath = path.resolve(__dirname, "../tool/theme/web");
  const paths = [entryPath, toolPath, themePath];

  for await (const curPath of paths) {
    if (!(await fs.exists(path.resolve(curPath, "node_modules")))) {
      try {
        console.log(` -------- ${curPath} dependencies installing  -------- `);
        const dependencyProcess = execa(
          "yarn install --registry=http://192.168.10.78:7001/",
          {
            cwd: curPath,
            shell: true,
          }
        );
        dependencyProcess.stdout.pipe(process.stdout);
        dependencyProcess.stderr.pipe(process.stderr);
        await dependencyProcess;
        console.log(` -------- ${curPath} dependencies installed -------- `);
      } catch (error) {
        throw new Error(error);
      }
    }
    console.log(` -------- ${curPath} build start  -------- `);
    const buildProcess = execa("yarn build", {
      cwd: curPath,
      shell: true,
    });
    buildProcess.stdout.pipe(process.stdout);
    buildProcess.stderr.pipe(process.stderr);
    await buildProcess;
    console.log(` -------- ${curPath} build end  -------- `);
  }
}

async function generateHtml() {
  const destPath = path.resolve(__dirname, "../dist/static/index.html");
  const content = `<!DOCTYPE html>
                  <html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>ecas-vue</title>
                        <meta http-equiv="refresh" content="0;url=app/zh_CN/views/login/index.html">
                    </head>
                    <body>
                    </body>
                  </html>
  `;
  await fs.writeFile(destPath, content);
  console.log(" -------- index.html generated -------- ");
}

async function generateMapping() {
  console.log(" -------- generate mapping start -------- ");
  const destPath = path.resolve(__dirname, "../dist/static/mapping.json");
  const destPathApp = path.resolve(
    __dirname,
    "../dist/static/app/mapping.json"
  );
  const content = `
    {
      "route" : "..",
      "master": true
    }
  `;
  await fs.writeFile(destPath, content);
  await fs.writeFile(destPathApp, content);
  console.log(" -------- generate mapping end -------- ");
}

async function copyConfig2Data() {
  console.log(" -------- copy config start -------- ");
  const sourcePath = path.resolve(__dirname, "../config/web");
  const destPath = path.resolve(__dirname, "../dist/static/app/data");
  const files = ["desktop.json", "menu.json"];

  for await (const file of files) {
    const content = await fs.readFile(path.resolve(sourcePath, file), "utf-8");
    await fs.writeFile(path.resolve(destPath, file), content);
  }
  console.log(" -------- copy config end -------- ");
}

(async function () {
  await cleanDist();
  await build();
  await generateHtml();
  await generateMapping();
  await copyConfig2Data();
})();
