All files / lib/command index.ts

97.3% Statements 36/37
57.14% Branches 4/7
100% Functions 3/3
96.97% Lines 32/33

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101    1x   1x 1x 1x 1x 1x 1x 1x                                           16x   16x 16x 16x 16x 16x 16x 16x 16x 32x     8x   8x           8x 8x   8x       8x       8x       8x       8x                     8x                     1x 8x   8x 8x    
"use strict";
 
import { Prompt, keyPressHistory } from "makitso-prompt";
 
import { debug } from "../debug";
import { parse } from "./args";
import { findCommand } from "./find";
import { Autocomplete } from "./keypress-autocomplete";
import { AutoHelp } from "./keypress-autohelp";
import { keyPressCommandInfo } from "./keypress-command-info";
import { keyPressValidate } from "./keypress-validate";
 
import { ContextSchema, Defs } from "../types";
 
type Args = {
  context: ContextSchema;
  commands: Defs;
  prompt: Prompt;
};
 
/**
 * Prompt the user for a command and run it
 *
 * @param {Object} arg0 -
 * @param {Object} arg0.context - An instance of context
 * @param {Object} arg0.commands - app commands
 * @param {Object} arg0.options - app options
 * @param {Function|Array} [arg0.complete=[]] - prompt autocomplete
 * @param {Function} [arg0.validate] - prompt validation
 * @returns {void}
 */
async function promptAndRun(arg0: Args): Promise<Error | void> {
  const { context, commands, prompt } = arg0;
  try {
    const cmdLine = `${await prompt.start()}`;
    debug({ cmdLine });
    const cmd = await findCommand({ cmdLine, commands });
    if (Icmd) {
      const { appCmd, cmdArgs } = cmd;
      const input = parse({ appCmd, cmdArgs });
      debug({ appCmd, cmdArgs, input });
      if (IappCmd.action) {
        await appCmd.action({ context, command: appCmd, input });
      }
    }
    return promptAndRun(arg0);
  } catch (error) {
    Iif (error.message !== "quit") {
      return Promise.reject(error);
    }
  }
}
 
function initCommandPrompt({ context, commands, prompt }: Args) {
  const commandInfo = keyPressCommandInfo({ context, commands });
 
  const keyPressParser = {
    keyPress: commandInfo
  };
 
  const keyPressAutocomplete = {
    keyPress: Autocomplete({ commandInfo })
  };
 
  const keyPressAutoHelp = {
    keyPress: AutoHelp()
  };
 
  const keyPressValidator = {
    keyPress: keyPressValidate()
  };
 
  Object.assign(prompt, {
    keyPressers: [
      ...prompt.keyPressers,
      keyPressHistory,
      keyPressParser,
      keyPressAutocomplete,
      keyPressAutoHelp,
      keyPressValidator
    ]
  });
 
  return prompt;
}
 
/**
 * Start the console app
 *
 * @param {Object} arg0 -
 * @param {Object} arg0.context - An instance of context
 * @param {Object} arg0.commands - app commands
 * @returns {void}
 */
export async function start(arg0: Args) {
  const { context, commands, prompt } = arg0;
 
  initCommandPrompt({ context, commands, prompt });
  return promptAndRun({ context, commands, prompt });
}