"use strict";

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

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

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

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

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

  return data;
}

function _lodash() {
  const data = require("lodash");

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

  return data;
}

var _getPadLength = _interopRequireDefault(require("../../utils/getPadLength"));

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; }

var _default = api => {
  api.registerCommand('help', {
    hide: true
  }, args => {
    const helpInfo = api.applyPlugins('_modifyHelpInfo', {
      initialValue: {
        scriptName: 'umi',
        commands: api.service.commands
      }
    });
    helpInfo.commands = _objectSpread({
      ui: {
        opts: {
          usage: 'umi ui',
          description: 'visual assist tool for umi project',
          options: {}
        }
      }
    }, helpInfo.commands);
    const command = args._[0];

    if (!command) {
      logMainHelp(helpInfo);
    } else {
      logHelpForCommand(command, helpInfo.commands[command]);
    }
  });

  function logMainHelp(helpInfo) {
    console.log(`\n  Usage: ${helpInfo.scriptName} <command> [options]\n`);
    console.log(`  Commands:\n`);
    const commands = helpInfo.commands;
    const padLength = (0, _getPadLength.default)(commands);

    for (const name in commands) {
      const opts = commands[name].opts || {};

      if (opts.hide !== true) {
        console.log(`    ${_chalk().default.green((0, _lodash().padEnd)(name, padLength))}${opts.description || ''}`);
      }
    }

    console.log(`\n  run ${_chalk().default.blue(`${helpInfo.scriptName} help [command]`)} for usage of a specific command.\n`);
  }

  function logHelpForCommand(name, command) {
    if (!command) {
      console.log(_chalk().default.red(`\n  command "${name}" does not exist.`));
    } else {
      const opts = command.opts || {};

      if (opts.usage) {
        console.log(`\n  Usage: ${opts.usage}`);
      }

      if (opts.options) {
        console.log(`\n  Options:\n`);
        const padLength = (0, _getPadLength.default)(opts.options);

        for (const name in opts.options) {
          console.log(`    ${_chalk().default.green((0, _lodash().padEnd)(name, padLength))}${opts.options[name]}`);
        }
      }

      if (opts.details) {
        console.log();
        console.log(opts.details.split('\n').map(line => `  ${line}`).join('\n'));
      }

      console.log();
    }
  }
};

exports.default = _default;