'use strict'

const fs = require('fs')
const shelljs = require('shelljs')
const log = require('../utilities/log')

const checkCapability = () => {
  const hasGit = shelljs.which('git')
  const hasNode = shelljs.which('node')
  const hasNpm = shelljs.which('npm')
  return hasGit && hasNode && hasNpm
}

const checkAvailability = name => {
  return fs.existsSync(name)
}

const executeClone = name => {
  return shelljs.exec(`git clone git@github.com:rhberro/the-react-client.git ${name} --quiet`)
}

const installWithYarn = name => {
  return shelljs.exec(`cd ${name} && yarn --no-progress`)
}

const installWithNpm = name => {
  return shelljs.exec(`cd ${name} && npm install --silent`)
}

const executeInstall = name => {
  const hasYarn = shelljs.which('yarn')
  if (hasYarn) return installWithYarn(name)
  return installWithNpm(name)
}

module.exports = name => {
  const capable = checkCapability()
  if (!capable) log.failure('There are missing dependencies.')

  const unnavailable = checkAvailability(name)
  if (unnavailable) log.failure('The destination folder already exists.')

  log.message('Cloning...')

  const cloned = executeClone(name)
  if (cloned.code !== 0) log.failure('Failed to clone the project.')

  log.message('Installing...')

  const installed = executeInstall(name)
  if (installed.code !== 0) log.failure('Installation aborted.')

  log.success('The application has been created.')
}

@@ -71,49 +71,42 @@
 s')%0A
-const log = require('../utilities/log')%0A%0A
+%0Amodule.exports = (%0A  () =%3E %7B%0A    
 cons
@@ -129,24 +129,28 @@
 y = () =%3E %7B%0A
+    
   const hasG
@@ -175,16 +175,20 @@
 ('git')%0A
+    
   const 
@@ -219,16 +219,20 @@
 'node')%0A
+    
   const 
@@ -261,16 +261,20 @@
 ('npm')%0A
+    
   return
@@ -302,19 +302,27 @@
  hasNpm%0A
-%7D%0A%0A
+    %7D%0A%0A    
 const ch
@@ -341,32 +341,36 @@
 ity = name =%3E %7B%0A
+    
   return fs.exis
@@ -382,19 +382,27 @@
 c(name)%0A
-%7D%0A%0A
+    %7D%0A%0A    
 const ex
@@ -416,32 +416,36 @@
 one = name =%3E %7B%0A
+    
   return shelljs
@@ -519,27 +519,35 @@
 %7D --quiet%60)%0A
-%7D%0A%0A
+    %7D%0A%0A    
 const instal
@@ -560,32 +560,36 @@
 arn = name =%3E %7B%0A
+    
   return shelljs
@@ -620,25 +620,28 @@
 n --
-no-progress%60)%0A%7D%0A%0A
+silent%60)%0A    %7D%0A%0A    
 cons
@@ -669,16 +669,20 @@
 me =%3E %7B%0A
+    
   return
@@ -733,19 +733,27 @@
 ilent%60)%0A
-%7D%0A%0A
+    %7D%0A%0A    
 const ex
@@ -769,32 +769,36 @@
 all = name =%3E %7B%0A
+    
   const hasYarn 
@@ -821,16 +821,20 @@
 'yarn')%0A
+    
   if (ha
@@ -869,16 +869,20 @@
 n(name)%0A
+    
   return
@@ -907,27 +907,25 @@
 me)%0A
-%7D%0A%0Amodule.exports =
+    %7D%0A%0A    return
  nam
@@ -931,16 +931,20 @@
 me =%3E %7B%0A
+    
   const 
@@ -971,16 +971,20 @@
 ility()%0A
+    
   if (!c
@@ -987,35 +987,45 @@
  (!capable) 
-log.failure
+%7B%0A        console.log
 ('There are 
@@ -1044,25 +1044,61 @@
 ndencies.')%0A
-%0A
+        process.exit(1)%0A      %7D%0A%0A    
   const unna
@@ -1128,24 +1128,28 @@
 ility(name)%0A
+    
   if (unnava
@@ -1156,27 +1156,37 @@
 ilable) 
-log.failure
+%7B%0A        console.log
 ('The de
@@ -1220,30 +1220,66 @@
 ists.')%0A
-%0A  log.message
+        process.exit(1)%0A      %7D%0A%0A      console.log
 ('Clonin
@@ -1278,32 +1278,36 @@
 ('Cloning...')%0A%0A
+    
   const cloned =
@@ -1322,24 +1322,28 @@
 Clone(name)%0A
+    
   if (cloned
@@ -1355,27 +1355,37 @@
  !== 0) 
-log.failure
+%7B%0A        console.log
 ('Failed
@@ -1413,22 +1413,58 @@
 .')%0A
-%0A  log.message
+        process.exit(1)%0A      %7D%0A%0A      console.log
 ('In
@@ -1478,16 +1478,20 @@
 g...')%0A%0A
+    
   const 
@@ -1523,16 +1523,20 @@
 l(name)%0A
+    
   if (in
@@ -1559,19 +1559,29 @@
  0) 
-log.failure
+%7B%0A        console.log
 ('In
@@ -1606,22 +1606,58 @@
 .')%0A
-%0A  log.success
+        process.exit(1)%0A      %7D%0A%0A      console.log
 ('Th
@@ -1690,10 +1690,44 @@
 ated.')%0A
-%7D
+      process.exit(0)%0A    %7D%0A  %7D%0A)()
 %0A

Remove the log module from the new command.