text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.InstallTask = void 0;
  
  function _react() {
    const data = _interopRequireDefault(require("react"));
  
    _react = function _react() {
      return data;
    };
  
    return data;
  }
  
  function _path() {
    const data = require("path");
  
    _path = function _path() {
      return data;
    };
  
    return data;
  }
  
  function _umi() {
    const data = require("umi");
  
    _umi = function _umi() {
      return data;
    };
  
    return data;
  }
  
  function _binaryMirrorConfig() {
    const data = _interopRequireDefault(require("binary-mirror-config"));
  
    _binaryMirrorConfig = function _binaryMirrorConfig() {
      return data;
    };
  
    return data;
  }
  
  var _Base = require("./Base");
  
  var _enums = require("../enums");
  
  var _util = require("../../util");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  
  const rimraf = _umi().utils.rimraf;
  
  class InstallTask extends _Base.BaseTask {
    constructor(opts) {
      super(opts);
      this.speedUpEnv = void 0;
      this.type = _enums.TaskType.INSTALL;
    }
  
    init(collector) {
      var _superprop_getInit = () => super.init,
          _this = this;
  
      return _asyncToGenerator(function* () {
        yield _superprop_getInit().call(_this, collector);
        _this.speedUpEnv = _this.getSpeedUpEnv();
      })();
    }
  
    run(args = {}, env = {}) {
      var _superprop_getRun = () => super.run,
          _this2 = this;
  
      return _asyncToGenerator(function* () {
        yield _superprop_getRun().call(_this2); // 执行删除的日志需要自己处理
  
        try {
          _this2.emit(_enums.TaskEventType.STD_OUT_DATA, 'Cleaning node_modules...\n');
  
          yield _this2.cleanNodeModules();
  
          _this2.emit(_enums.TaskEventType.STD_OUT_DATA, 'Cleaning node_modules success.\n');
        } catch (e) {
          _this2.emit(_enums.TaskEventType.STD_OUT_DATA, 'Cleaning node_modules error\n');
  
          _this2.emit(_enums.TaskEventType.STD_OUT_DATA, `${e.message}\n`);
        }
  
        const script = _this2.getScript(env);
  
        _this2.emit(_enums.TaskEventType.STD_OUT_DATA, `Executing ${script}... \n`);
  
        _this2.proc = (0, _util.runCommand)(script, {
          cwd: _this2.cwd,
          env: _objectSpread(_objectSpread({}, process.env), _this2.speedUpEnv)
        });
  
        _this2.handleChildProcess(_this2.proc);
      })();
    }
  
    cancel() {
      var _this3 = this;
  
      return _asyncToGenerator(function* () {
        const proc = _this3.proc;
  
        if (!proc) {
          return;
        } // 子任务执行结束
  
  
        if ([_enums.TaskState.FAIL, _enums.TaskState.SUCCESS].indexOf(_this3.state) > -1) {
          return;
        }
  
        _this3.state = _enums.TaskState.INIT; // 杀掉子进程
  
        proc.kill('SIGINT');
      })();
    }
  
    cleanNodeModules() {
      var _this4 = this;
  
      return _asyncToGenerator(function* () {
        return new Promise((resolve, reject) => {
          const nodeModulePath = (0, _path().join)(_this4.cwd, 'node_modules');
          rimraf(nodeModulePath, err => {
            if (err) {
              reject(err);
            } else {
              resolve();
            }
          });
        });
      })();
    }
  
    getScript({
      NPM_CLIENT
    } = {}) {
      const client = this.getNpmClient(NPM_CLIENT);
      let script = '';
  
      switch (client) {
        case _enums.NpmClient.tnpm:
          script = 'tnpm install -d';
          break;
  
        case _enums.NpmClient.cnpm:
          script = 'cnpm install -d';
          break;
  
        case _enums.NpmClient.npm:
          script = 'npm install -d';
          break;
  
        case _enums.NpmClient.ayarn:
          script = 'ayarn';
          break;
  
        case _enums.NpmClient.tyarn:
          script = 'tyarn';
          break;
  
        case _enums.NpmClient.yarn:
          script = 'yarn';
          break;
  
        case _enums.NpmClient.pnpm:
          script = 'pnpm';
          break;
      }
  
      return script;
    }
  
    getNpmClient(client) {
      if (client) {
        return _enums.NpmClient[client];
      }
  
      return (0, _util.getNpmClient)();
    } // TODO：默认全部开启加速，二期要对这部分做修改
  
  
    getSpeedUpEnv() {
      return _binaryMirrorConfig().default.china.ENVS;
    }
  
  }
  
  exports.InstallTask = InstallTask;