All files / lib/command keypress-command-info.ts

14.55% Statements 8/55
0% Branches 0/24
20% Functions 1/5
12.77% Lines 6/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 871x     1x 1x   1x               8x 8x                                                                                                                                              
import _filter from "lodash/filter";
import { State } from "makitso-prompt";
 
import { parse } from "./args";
import { findCommand } from "./find";
 
import { debug } from "../debug";
 
import { CommandInfo, CommandInfoArgs } from "../types";
 
function getSuggests(obj: { [key: string]: unknown }) {
  return _filter(Object.keys(obj), choice => !choice.startsWith("_"));
}
 
export function keyPressCommandInfo({ context, commands }: CommandInfoArgs) {
  return async function keyPress(state: State) {
    const cmdLine = state.command;
    const cmd = await findCommand({ cmdLine, commands });
 
    const info: CommandInfo = {};
 
    if (cmd) {
      const { appCmd, cmdArgs } = cmd;
      // info.description = appCmd._description || appCmd.description;
      info.description = appCmd.description;
      info.opts = appCmd.opts;
      info.args = appCmd.args;
      info.suggests = getSuggests(appCmd.commands || appCmd);
      info.help = null;
      if (!appCmd.commands) {
        info.input = parse({ appCmd, cmdArgs, cmdLine });
        info.hasAction = true;
        if (!appCmd.suggest) {
          info.suggests = [];
        } else {
          debug({ suggestsFn: typeof appCmd.suggest });
          if (typeof appCmd.suggest === "function") {
            debug(["appCmd.suggest", appCmd.suggest.toString()]);
            info.suggests = await appCmd.suggest({
              context,
              command: appCmd,
              input: info.input
            });
            debug(["command.suggests", info.suggests]);
          } else {
            info.suggests = appCmd.suggest;
          }
        }
        if (!appCmd.help) {
          info.helps = [];
        } else {
          debug({ helpsFn: typeof appCmd.help });
          if (typeof appCmd.help === "function") {
            debug(["appCmd.help", appCmd.help.toString()]);
            info.help = await appCmd.help({
              context,
              command: appCmd,
              input: info.input
            });
            debug(["command.helps", info.help]);
          } else {
            info.help = appCmd.help;
          }
        }
      }
    } else {
      info.suggests = getSuggests(commands);
    }
 
    debug({ cmd, cmdLine: `"${cmdLine}"` });
    let filter = "";
    if (!/\s$/.test(cmdLine)) {
      filter = (cmd ? cmd.cmdArgsList.pop() : cmdLine) || "";
    }
    if (filter) {
      const matches = _filter(info.suggests, choice =>
        choice.startsWith(filter)
      );
      if (matches.length) {
        info.suggests = matches;
      }
    }
    // state.commandLine({ info });
    state.stash.info = info;
  };
}