import { resolve } from "path";
import { isEmpty } from "lodash";
import { mkdirp, copy as cp, remove as rm } from "fs-extra";
import chokidar from "chokidar";

const defaultOutputDir = resolve(process.cwd(), "./dist");

function outputDir(output) {
  if (output) {
    return resolve(process.cwd(), output);
  }

  return defaultOutputDir;
}

export async function outputPath(output) {
  const outDir = outputDir(output);
  const tmpDir = resolve(outDir, "tmp");

  await mkdirp(tmpDir);
  await mkdirp(resolve(outDir, "html"));

  const buildDir = await tmpdir({ dir: tmpDir, prefix: "build-" });

  return [outDir, buildDir];
}

export async function copyOverrides(overrides, buildDir) {
  if (!isEmpty(overrides)) {
    for await (let [target, source] of Object.entries(overrides)) {
      if (source) {
        copyOverride(target, source, buildDir);
      }
    }
  }
}

function copyOverride(target, source, buildDir) {
  return cp(source, resolve(buildDir, target));
}

export function watchTemplate(tplDir, buildDir) {
  const watcher = chokidar.watch(tplDir, {
    persistent: true
  });

  watcher.on("change", async path => {
    await cp(path, path.replace(tplDir, buildDir));
  });

  return watcher;
}

@@ -152,16 +152,50 @@
 okidar%22;
+%0Aimport %7B tmpdir %7D from %22./index%22;
 %0A%0Aconst 
@@ -1240,12 +1240,184 @@
  watcher;%0A%7D%0A
+%0Aexport function seedMap(seeds) %7B%0A  const data = %7B%7D;%0A%0A  seeds.transitions.forEach(transition =%3E %7B%0A    data%5Btransition.permalink%5D = transition.uuid;%0A  %7D);%0A%0A  return data;%0A%7D%0A

add seedMap util