#!/usr/bin/env node
/* eslint-disable */

const appRootDir = require("app-root-dir").get
const { mkdtempSync, writeFileSync } = require("fs")
const path = require("path")
const { tmpdir, platform } = require("os")
const { execSync, fork } = require("child_process")
const { request } = require("http")
const chalk = require("chalk")

function log(message)
{
  console.log(chalk.cyan(`+ ${message}`))
}

const CWD = appRootDir()
log(`Source path: ${CWD}`)
const TESTPATH = mkdtempSync(path.join(tmpdir(), "edge-"))
log(`Temp path:   ${TESTPATH}`)

const SRC_EXEC = {
  cwd: CWD
}
const TARGET_EXEC = {
  cwd: TESTPATH
}

function exec(cmd, options)
{
  log(`Execute: ${cmd}`)
  options.stdio = "inherit"
  return execSync(cmd, options)
}

/*
if (platform() !== "win32")
{
  try
  {
    exec("yarn unlink", SRC_EXEC)
  }
  catch (error)
  {
    // ignore unlink failure
  }

  exec("yarn link", SRC_EXEC)
}
*/

log(`Write package.json to ${TESTPATH}`)
writeFileSync(
  path.join(TESTPATH, "package.json"),
  JSON.stringify({
    "name":"test",
    "version":"1.0.0",
    "devDependencies": {
      "cross-env": "^3.1.4"
    },
    "scripts": {
      "prod":"cross-env NODE_ENV=production edge build"
    }
  }, null, 2),
  "utf8"
)

// exec(`yarn add file:${CWD}`, TARGET_EXEC)
exec(`npm install ${CWD}`, TARGET_EXEC)
/*
if (platform() !== "win32")
  exec("yarn link edgestack", TARGET_EXEC)
*/

const edgeCommand = path.join("node_modules", ".bin", "edge")
exec(`${edgeCommand} bootstrap --title="Test" --description="Test" --language="de-DE"`, TARGET_EXEC)

exec("npm install", TARGET_EXEC)
exec("npm run prod", TARGET_EXEC)

const serverProcess = fork(path.join(TESTPATH, "build", "server", "main.js"), [], {
  cwd: TESTPATH,
  stdio: "inherit"
})
serverProcess.on("exit", (code) =>
{
  if (code)
    process.exit(2)
})

function quitProcess(exitCode)
{
  log(`Output path: ${TESTPATH}`)
  log(`Exit code:   ${exitCode}`)
  serverProcess.kill("SIGKILL")
  process.exit(exitCode)
}

function check()
{
  const options = {
    hostname: 'localhost',
    port: 1339,
    path: '/',
    method: 'GET'
  }

  const req = request(
    options,
    (res) => {
      if (res.statusCode !== 200)
        quitProcess(1)

      const chunks = []
      res.on("data", (chunk) =>
      {
        chunks.push(chunk)
      })
      res.on('end', () =>
      {
        console.log(
          Buffer.concat(chunks).toString("utf8")
        )

        quitProcess(0)
      })
      res.on('error', () =>
      {
        quitProcess(3)
      })
    }
  )

  req.on("error", () => {
    quitProcess(1)
  })

  req.end()
}

setTimeout(check, 2000)

@@ -731,19 +731,16 @@
 ons)%0A%7D%0A%0A
-/*%0A
 if (plat
@@ -894,19 +894,16 @@
 _EXEC)%0A%7D
-%0A*/
 %0A%0Alog(%60W
@@ -1218,18 +1218,47 @@
 tf8%22%0A)%0A%0A
-//
+if (platform() !== %22win32%22)%0A%7B%0A 
  exec(%60y
@@ -1296,107 +1296,85 @@
 EC)%0A
+  
 exec(
-%60npm install $%7BCWD%7D%60, TARGET_EXEC)%0A/*%0Aif (platform() !== %22win32%22)%0A  exec(%22yarn link edgestack%22
+%22yarn link edgestack%22, TARGET_EXEC)%0A%7D%0Aelse%0A  exec(%60npm install $%7BCWD%7D%60
 , TA
@@ -1387,11 +1387,8 @@
 XEC)
-%0A*/
 %0A%0Aco
@@ -1549,16 +1549,130 @@
 _EXEC)%0A%0A
+if (platform() !== %22win32%22)%0A%7B%0A  exec(%22yarn install%22, TARGET_EXEC)%0A  exec(%22yarn run prod%22, TARGET_EXEC)%0A%7D%0Aelse %7B%0A  
 exec(%22np
@@ -1696,16 +1696,18 @@
 T_EXEC)%0A
+  
 exec(%22np
@@ -1723,32 +1723,34 @@
 d%22, TARGET_EXEC)
+%0A%7D
 %0A%0Aconst serverPr

Use npm for windows and yarn for other platforms