// ⚠️
// not too much es2015 as this is delivered/run via npm as is
// ⚠️

const stat = require("fs").stat
const spawn = require("child_process").spawn
const join = require("path").join

const fs = require("fs-extra")

const pkg = require("../package.json")

// no need for th is step on CI
if (process.env.CI) {
  process.exit(0)
}

const spawnOpts = {
  stdio: "inherit",
  cwd: join(__dirname, "../"),
}

stat("__tests__", function(error, stats) {
  if (!error && stats.isDirectory()) {
    console.log("Tweaking and installing dependencies for docs & themes")

    spawn("babel-node", [ "scripts/docs.js" ], spawnOpts)
    .on("error", (err) => {
      console.error("Failed to prepare docs")
      console.error(err)
    })

    spawn("babel-node", [ "scripts/phenomic-theme-base.js" ], spawnOpts)
    .on("error", (err) => {
      console.error("Failed to prepare docs")
      console.error(err)
    })
  }
})

const phenomicThemeBaseDir = join(__dirname, "../themes/phenomic-theme-base")
stat(join(phenomicThemeBaseDir, ".npmignore"), function(err) {
  if (err) {
    console.log("No .npmignore in phenomic-theme-base to rename")
    return true
  }

  fs.move(
    join(phenomicThemeBaseDir, ".npmignore"),
    join(phenomicThemeBaseDir, ".gitignore"),
    function(err) {
      if (err) {
        throw new Error(
          "Cannot rename .npmignore to .gitignore in phenomic-theme-base"
        )
      }
    }
  )
})

stat("lib", function(error, stats) {
  if (!error && stats.isDirectory()) {
    return true
  }

  console.warn(
    "\n" +
    "Builded sources not found. It looks like you might be attempting " +
    `to install ${ pkg.name } from git. \n` +
    "Sources need to be transpiled before use. " +
    // see commented code below
    // "This may take a moment." +
    "\n"
  )

  const fail  = (err) => {
    console.error(
      `Failed to build ${ pkg.name } automatically. ` +

      // see commented babel install below
      "Do you have to required dependencies to install from git?\n" +
      "Be sure to have installed run this before:" +
      "\n\n" +
      `npm install babel-cli ${ pkg.babel.presets }` +
      "\n\n" +
      "Then rebuild phenomic by running: " +
      "\n\n" +
      "npm rebuild phenomic" +
      "\n\n"
    )

    if (err) {
      throw err
    }
  }

  // npm install fails with
  //    Cannot read property 'target' of null
  // https://github.com/npm/npm/issues/10686

  // const installTranspiler = spawn(
  //   "npm",
  //   [ "i" , "babel-cli", ...pkg.babel.presets ],
  //   spawnOpts
  // )

  // installTranspiler.on("error", fail)
  // installTranspiler.on("close", (code) => {
  //   console.log(pkg.name, "postinstall deps installed")
  //   if (code === 0) {
  const installer = spawn(
    "npm",
    [ "run", "transpile" ],
    spawnOpts
  )

  installer.on("error", fail)
  //   }
  // })
})

@@ -256,163 +256,198 @@
 %22)%0A%0A
-// no need for th is step on CI%0Aif (process.env.CI) %7B%0A  process.exit(0)%0A%7D%0A%0Aconst spawnOpts = %7B%0A  stdio: %22inherit%22,%0A  cwd: join(__dirname, %22../%22),%0A%7D
+const spawnOpts = %7B%0A  stdio: %22inherit%22,%0A  cwd: join(__dirname, %22../%22),%0A%7D%0A%0A// e2e-tests are only present if you clone the full repo%0A// (won't be present when npm install with git ref)
 %0A%0Astat(%22
 __te
@@ -446,17 +446,17 @@
 at(%22
-__
+e2e-
 tests
-__
 %22, f
@@ -470,32 +470,91 @@
 (error, stats) %7B
+%0A%0A  // for dev install, we prepare docs and base theme deps
 %0A  if (!error &&
@@ -656,34 +656,149 @@
 %22)%0A%0A    
-spawn(%22babel-node%22
+const babelNode = /%5Ewin/.test(process.platform)%0A      ? %22.%5C%5Cnode_modules%5C%5C.bin%5C%5Cbabel-node%22%0A      : %22babel-node%22%0A%0A    spawn(babelNode
 , %5B %22scr
@@ -942,28 +942,25 @@
   spawn(
-%22
 babel
--n
+N
 ode
-%22
 , %5B %22scr
@@ -1073,54 +1073,312 @@
 are 
-docs%22)%0A      console.error(err)%0A    %7D)%0A  %7D%0A%7D)%0A
+phenomic-theme-base%22)%0A      console.error(err)%0A    %7D)%0A%0A    spawn(babelNode, %5B %22scripts/test-setup.js%22 %5D, spawnOpts)%0A    .on(%22error%22, (err) =%3E %7B%0A      console.error(%22Failed to prepare test-setup%22)%0A      console.error(err)%0A    %7D)%0A  %7D%0A%7D)%0A%0A// npm rename .gitignore to .npmignore (which is just stupid for us)
 %0Acon
@@ -1890,44 +1890,274 @@
 %7D)%0A%0A
-stat(%22lib%22, function(error, stats) %7B
+// if repo installed from npm, we try to compile src%0Astat(%22lib%22, function(error, stats) %7B%0A%0A  // no need for th is step on CI,%0A  // only make sense when cloning the repo for dev%0A  if (process.env.CI) %7B%0A    return true%0A  %7D%0A%0A  // if lib is present, no need to transpile
 %0A  i

Fix posinstall script and let them run on CI (CircleCi save cache right after the initial npm install)