'use strict';

const inquirer = require('inquirer'),
  colors = require('colors'),
  shell = require('shelljs/global'),
  fs = require('fs'),
  copydir = require('copy-dir'),
  emoji = require('node-emoji'),
  del = require('del'),
  noEmoji = /^win/.test(process.platform);

if (noEmoji) {
  emoji.get = () => '';
}

function logComplete(task) {
  console.log('['.green + task.green + ' ' + emoji.get('white_check_mark') + ' ]\n'.green);
}

function folderExists(src) {
  return fs.existsSync(src);
}

function createTmpFolder() {
  return new Promise((resolve, reject) => {
    if (folderExists('tmp')) {
      console.log('Destination path already exists and is not an empty directory. '.red + emoji.get('cry'));
      return;
    }
    exec('mkdir tmp');
    cd('tmp');

    resolve();
  });
}

function gitClone(repo) {
  return new Promise((resolve, reject) => {
    console.log('Clone from ' + repo.url + ' ' + repo.branch);
    exec('git clone ' + repo.url + ' ./ -b ' + repo.branch);
    del.sync(['.git']);
    logComplete('Git cloning has been completed')

    resolve();
  });
}

function installDependencies() {
  return new Promise((resolve, reject) => {
    console.log('Installing npm dependencies...');
    exec('npm install');
    logComplete('Dependencies have been installed');

    resolve();
  });
}

function moveFiles() {
  return new Promise((resolve, reject) => {
    console.log('Copying files...');
    copydir('../tmp', '../', (err) => {
      if (err) {
        console.log(err);
        return;
      }
      cd('..');
      del.sync(['tmp']);
      logComplete('Copying files has been completed');

      resolve();
    });
  });
}

function init(repo) {
  createTmpFolder().then(
    () => {
      console.log('\nGetting starter files... ' + emoji.get('runner'));
      gitClone(repo).then(
        () => {
          moveFiles().then(
            () => {
              installDependencies().then(
                () => {
              console.log('Starter has been successfully installed. Good luck '.green +
                emoji.get('wink') + ' \n\u00A9 JustCoded'.green);
                }
              )
            }
          )
        }
      )
    }
  );
}

module.exports = function () {
  let qTypes = [{
      message: 'Select project type:',
      type: 'list',
      name: 'value',
      choices: [{
        name: 'Yes',
        // Branch name
        value: true
      }, {
        name: 'No',
        // Branch name
        value: false
      }]
    },
    {
      when: function (response) {
        return response.value;
      },
      message: 'Would you like to use SCSS maps?',
      type: 'list',
      name: 'value',
      choices: [{
        name: 'Yes',
        // Branch name
        value: 'master'
      }, {
        name: 'No',
        // Branch name
        value: 'Without-SCSS-Map'
      }]
    }
  ];

  console.log('\n*****************************************\n*\tWelcome to JustCoded Starter\t*\n*****************************************\n'.green);

  let prompt = inquirer.createPromptModule();

  prompt(qTypes).then((answers) => {
    commands(answers);
  });

  function commands(info) {
    if (!info.value) {
      console.log('In maintenance, sorry '.red + emoji.get('hourglass'));
      return;
    }

    init({
      url: 'https://github.com/justcoded/web-starter-kit.git',
      branch: info.value
    });
  }
};
@@ -1953,32 +1953,36 @@
 %7B%0A              
+    
 console.log('Sta
@@ -2039,16 +2039,20 @@
 green +%0A
+    
         
@@ -2250,26 +2250,24 @@
 es = %5B%7B%0A    
-  
 message: 'Se
@@ -2287,18 +2287,16 @@
 type:',%0A
-  
     type
@@ -2301,34 +2301,32 @@
 pe: 'list',%0A    
-  
 name: 'value',%0A 
@@ -2316,34 +2316,32 @@
  name: 'value',%0A
-  
     choices: %5B%7B%0A
@@ -2346,33 +2346,32 @@
 %7B%0A      
-  
 name: '
-Yes
+Markup
 ',%0A
-  
       //
@@ -2385,26 +2385,24 @@
  name%0A      
-  
 value: true%0A
@@ -2401,26 +2401,24 @@
 e: true%0A    
-  
 %7D, %7B%0A       
@@ -2416,32 +2416,36 @@
 %7B%0A      
- 
- 
 name: '
-No
+JavaScript
 ',%0A
-  
       //
@@ -2449,34 +2449,32 @@
  // Branch name%0A
-  
       value: fal
@@ -2484,30 +2484,24 @@
     
-  
 %7D%5D%0A  
- 
- 
 %7D,%0A
-  
   %7B%0A    
   wh
@@ -2496,18 +2496,16 @@
   %7B%0A    
-  
 when: fu
@@ -2526,26 +2526,24 @@
 se) %7B%0A      
-  
 return respo
@@ -2553,18 +2553,16 @@
 .value;%0A
-  
     %7D,%0A 
@@ -2564,18 +2564,16 @@
  %7D,%0A    
-  
 message:
@@ -2613,18 +2613,16 @@
 ?',%0A    
-  
 type: 'l
@@ -2627,26 +2627,24 @@
 'list',%0A    
- 
- 
 name: 'value
@@ -2646,18 +2646,16 @@
 value',%0A
-  
     choi
@@ -2664,26 +2664,24 @@
 s: %5B%7B%0A      
-  
 name: 'Yes',
@@ -2679,34 +2679,32 @@
 e: 'Yes',%0A      
- 
- 
 // Branch name%0A 
@@ -2694,34 +2694,32 @@
  // Branch name%0A
-  
       value: 'ma
@@ -2724,18 +2724,16 @@
 master'%0A
-  
     %7D, %7B
@@ -2735,26 +2735,24 @@
  %7D, %7B%0A      
-  
 name: 'No',%0A
@@ -2751,18 +2751,16 @@
 : 'No',%0A
-  
       //
@@ -2768,26 +2768,24 @@
 Branch name%0A
-  
       value:
@@ -2812,18 +2812,15 @@
     
-  
 %7D%5D%0A  
-  
 %7D
+,
 %0A  %5D

Change choices