"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _assert() {
  const data = _interopRequireDefault(require("assert"));

  _assert = function _assert() {
    return data;
  };

  return data;
}

function _fs() {
  const data = require("fs");

  _fs = function _fs() {
    return data;
  };

  return data;
}

function _path() {
  const data = require("path");

  _path = function _path() {
    return data;
  };

  return data;
}

function _rimraf() {
  const data = _interopRequireDefault(require("rimraf"));

  _rimraf = function _rimraf() {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _default(api) {
  const paths = api.paths,
        config = api.config,
        log = api.log;

  function deleteFile(absPath) {
    _rimraf().default.sync(absPath);

    log.success(`rm ${(0, _path().relative)(api.cwd, absPath)}`);
  }

  function testAndDelete(absPath) {
    if ((0, _fs().existsSync)(absPath)) {
      deleteFile(absPath);
    }
  }

  function changePathExt(path, newExtname) {
    const dir = (0, _path().dirname)(path);
    const oldExtname = (0, _path().extname)(path);
    const base = (0, _path().basename)(path, oldExtname);
    return (0, _path().join)(dir, `${base}${newExtname}`);
  }

  function deletePage(name) {
    const jsPath = (0, _path().join)(paths.cwd, name);
    console.log(`jsPath`, jsPath);

    if ((0, _fs().existsSync)(jsPath)) {
      deleteFile(jsPath);
      testAndDelete(changePathExt(jsPath, '.css'));
      testAndDelete(changePathExt(jsPath, '.less'));
      testAndDelete(changePathExt(jsPath, '.sass'));
      testAndDelete(changePathExt(jsPath, '.scss'));
    } else {
      throw new Error(`route component ${name} not found`);
    }
  }

  function rm(args) {
    (0, _assert().default)(!config.routes, `umi rm is not supported now when routes is configured.`);
    (0, _assert().default)(args._ && args._.length === 2, `Invalid args, checkout umi help rm for more details.`);

    const _args$_ = _slicedToArray(args._, 2),
          type = _args$_[0],
          name = _args$_[1];

    switch (type) {
      case 'page':
        deletePage(name);
        break;

      default:
        throw new Error(`Type ${type} not supported, checkout umi help rm for more details`);
    }

    console.log('done');
  }

  api.registerCommand('rm', {
    description: '[alpha] remove files quickly',
    usage: 'umi rm name args',
    options: {}
  }, rm);
}