"use strict";

(function () {

  var async = require('async');
  var chalk = require('chalk');
  var childProcess = require('child_process');
  var fs = require('fs');

  var fsp = require('../../utils/fs');
  var path = require('../../utils/path.js');

  module.exports = function (options) {

    console.log('开始安装 ' + options.cmd + ' ...');

    var repo = options.cmd;
    var efesdir = path.join(__dirname, '..', '..', '..');
    var condir = path.join(efesdir, 'dist', 'commands', repo, 'command.json');

    var _cmd = fsp.readJSONSync(condir);

    if (_cmd) {

      if (_cmd.installed === true && !options.force) {
        console.log('\n' + chalk.green('此命令已经安装成功，请运行 efes install -f --cmd ' + repo + ' 进行强制重新安装。') + '\n');
        process.exit(1);
      }

      try {

        if (_cmd.packages.length > 0) {

          _cmd.packages.some(function (_repo) {

            var cmd = 'npm install ' + _repo + ' --save';

            childProcess.execSync(cmd, {
              cwd: efesdir,
              stdio: 'inherit'
            });
          });
        }
      } catch (e) {
        console.log('\n' + chalk.red('安装失败！') + '\n');
        process.exit(1);
      }

      _cmd.installed = true;

      var a = fsp.writeJSONSync(condir, _cmd);

      console.log('\n' + chalk.green('安装成功！') + '\n');
    } else {
      console.log('\n' + chalk.red('未找到 ' + repo + ' 的配置文件，无法安装此命令。') + '\n');
    }
  };
})();