import ora from 'ora';
import Promise from 'bluebird';
import { exec } from 'child_process';
import startCase from 'lodash/startCase';
import ui from '../../cli/ui';
import Blueprint from '../../cli/blueprint';
import pkg from '../../package.json';

const run = Promise.promisify(exec);

export default class AddonBlueprint extends Blueprint {

  static blueprintName = 'addon';
  static description = 'Creates a new addon project, initializes git and installs dependencies';

  params = [ 'name' ];

  flags = {
    'skip-npm': {
      description: 'Do not run npm install on new app',
      defaultValue: false,
      type: Boolean
    }
  }

  locals({ name }) {
    return {
      name,
      className: startCase(name).replace(/\s/g, ''),
      humanizedName: startCase(name),
      denaliVersion: pkg.version
    };
  }

  postInstall({ name }, flags) {
    let spinner = ora();
    ui.info('');
    spinner.text = 'Installing npm dependencies ...';
    spinner.start();
    return Promise.resolve().then(() => {
      if (!flags['skip-npm']) {
        return run('npm install --loglevel=error', { cwd: name }).then(() => {
          spinner.stop();
          ui.success('Installing npm dependencies ... done ✔');
        });
      }
    }).then(() => {
      spinner.text = 'Setting up git repo ...';
      spinner.start();
      return run('git init', { cwd: name });
    }).then(() => {
      return run('git add .', { cwd: name });
    }).then(() => {
      return run('git commit -am "Initial denali project scaffold"', { cwd: name });
    }).then(() => {
      spinner.stop();
      ui.success('Setting up git repo ... done ✔');
      ui.success('🏔 Installation complete!');
      ui.info('');
      ui.info('To launch your application, just run:');
      ui.info('');
      ui.info(`  $ cd ${ name } && denali server`);
      ui.info('');
    });
  }

}

@@ -128,16 +128,56 @@
 tCase';%0A
+import cmdExists from 'command-exists';%0A
 import u
@@ -319,16 +319,68 @@
 y(exec);
+%0Aconst commandExists = Promise.promisify(cmdExists);
 %0A%0Aexport
@@ -608,19 +608,20 @@
   'skip-
-npm
+deps
 ': %7B%0A   
@@ -648,23 +648,28 @@
 not 
-run npm install
+install dependencies
  on 
@@ -1137,17 +1137,175 @@
 kip-
-npm
+deps
 '%5D) %7B%0A
+        return commandExists('yarn').then((yarnExists) =%3E %7B%0A          if (yarnExists) %7B%0A            return run('yarn install', %7B cwd: name %7D);%0A          %7D%0A  
     
@@ -1363,16 +1363,28 @@
 d: name 
+%7D);%0A        
 %7D).then(
@@ -1442,36 +1442,32 @@
 ess('Installing 
-npm 
 dependencies ...

support yarn in addon blueprint