All files / lib/command keypress-validate.ts

26.32% Statements 5/19
0% Branches 0/16
33.33% Functions 1/3
27.78% Lines 5/18

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 301x 1x 1x       1x 8x                                            
import _map from "lodash/map";
import _get from "lodash/get";
import chalk from "chalk";
 
import { KeyPress, State } from "makitso-prompt";
 
export function keyPressValidate() {
  return async function keyPress(state: State, press: KeyPress) {
    if (state.returnCommand && state.command.trim()) {
      if (press.key.name === "return") {
        const { info } = state.stash;
        if (!info.hasAction) {
          state.returnCommand = false;
          state.header = chalk`{yellow "${state.command.trim()}" is not a complete command}`;
        }
        const missing = _get(info, "input.missing", []);
        if (missing.length) {
          const args = _map(missing, arg => arg.name);
          const { name, description } = missing[0];
          state.returnCommand = false;
          state.header = chalk`{yellow Missing Args: ${args.join(
            " "
          )}}\r\n{grey Enter ${description || name}}`;
        }
      }
    }
    return state;
  };
}