const childProcess = require('child_process');
const execSync = childProcess.execSync;
const path = require('path');
const tools = require('@eui/tools/scripts/utils/tools');
const utils = require('./utils');
const chalk = require('chalk');

module.exports.start = (options) => {

  if (!options.config.npmInstall) {
    return;
  }

  return Promise.resolve()
    .then(() => {
      return install(options.config, options.targetPath)
    })
    .catch((e) => {
      throw e;
    })
}

const install = (cliConfig, targetPath) => {

  const wd = path.resolve(utils.getAngularPath(targetPath, cliConfig.appName, cliConfig.appType, cliConfig.artifactId));

  console.log('');
  console.log('');
  console.log('*****************************************************');
  console.log(chalk.cyan(' Installing app dependencies... PLEASE WAIT....'));
  console.log('*****************************************************');

  return Promise.resolve()
    .then(() => {
      return tools.remove(path.join(wd, 'yarn.lock'));
    })
    .then(() => {
      return execSync("yarn", { cwd: wd, stdio: "inherit" });
    })

    .catch((e) => {
      throw e;
    })
}

