Interface RunOptions

Options for the run function.

interface RunOptions {
    afterExecute?: ((payload) => void);
    afterParse?: ((payload) => void);
    afterResolve?: ((payload) => void);
    beforeEnd?: ((payload) => void);
    beforeExecute?: ((payload) => void);
    beforeNext?: ((payload) => void);
    beforeParse?: ((payload) => void);
    beforeResolve?: ((payload) => void);
    command?: string | string[];
    commandsDir?: string;
    initialData?: any;
    onError?: ((payload) => void);
    onExit?: ((payload) => void);
    options?: OptionsConfig;
    plugins?: Plugin[];
}

Properties

afterExecute?: ((payload) => void)

A hook that runs once all commands have been called or when context.end() is called.

Type declaration

    • (payload): void
    • Parameters

      • payload: {
            result: unknown;
            setResult: ((result) => void);
            state: State<unknown, OptionsConfig>;
        }
        • result: unknown

          The final result.

        • setResult: ((result) => void)

          Override the final result.

            • (result): void
            • Parameters

              • result: unknown

              Returns void

        • state: State<unknown, OptionsConfig>

          The state object.

      Returns void

afterParse?: ((payload) => void)

A hook that runs after parsing the command string.

Type declaration

    • (payload): void
    • Parameters

      • payload: {
            context: Context<OptionsConfig>;
            parsedOptions: {};
            setParsedOptions: ((optionValues) => void);
        }
        • context: Context<OptionsConfig>

          The command's context object.

        • parsedOptions: {}

          The resulting parsed options.

          • setParsedOptions: ((optionValues) => void)

            Override the parsed options.

              • (optionValues): void
              • Parameters

                • optionValues: {}

                  The parsed option values.

                  Returns void

          Returns void

      afterResolve?: ((payload) => void)

      A hook that runs after importing command modules.

      Type declaration

        • (payload): void
        • Parameters

          • payload: {
                addResolvedCommands: ((result) => void);
                context: Context<OptionsConfig>;
                resolvedCommands: ResolvedCommand[];
            }
            • addResolvedCommands: ((result) => void)

              Add additional resolved commands to the context.

              Remarks

              After each command is resolved, it's options config is merged with the context's existing options config so that context is always up to date in the beforeResolveNext hook. Because of this, resolved commands can't be replaced once resolved, only added to. If you need to manually set the resolved commands, you can use the beforeResolve hook to do so.

            • context: Context<OptionsConfig>

              The command's context object.

            • resolvedCommands: ResolvedCommand[]

              The resolved commands.

          Returns void

      beforeEnd?: ((payload) => void)

      A hook that runs before executing the context.end() function.

      Type declaration

        • (payload): void
        • Parameters

          • payload: {
                data: unknown;
                setData: ((data) => void);
                state: State<unknown, OptionsConfig>;
            }
            • data: unknown

              The data that will be returned.

            • setData: ((data) => void)

              Override the data that will be returned.

                • (data): void
                • Parameters

                  • data: unknown

                    The new data.

                  Returns void

            • state: State<unknown, OptionsConfig>

              The state object.

          Returns void

      beforeExecute?: ((payload) => void)

      A hook that runs before calling the first command.

      Type declaration

        • (payload): void
        • Parameters

          • payload: {
                initialData: unknown;
                setInitialData: ((data) => void);
                setResultAndSkip: ((result) => void);
                skip: (() => void);
                state: State<unknown, OptionsConfig>;
            }
            • initialData: unknown

              The initial data that was passed to the state.

            • setInitialData: ((data) => void)

              Override the state's initial data.

                • (data): void
                • Parameters

                  • data: unknown

                    The new initial data.

                  Returns void

            • setResultAndSkip: ((result) => void)

              Set the final result and skip execution.

                • (result): void
                • Parameters

                  • result: unknown

                  Returns void

            • skip: (() => void)

              Skip execution.

                • (): void
                • Returns void

            • state: State<unknown, OptionsConfig>

              The state object.

          Returns void

      beforeNext?: ((payload) => void)

      A hook that runs before executing the context.next() function.

      Type declaration

        • (payload): void
        • Parameters

          • payload: {
                data: unknown;
                nextCommand: undefined | ResolvedCommand;
                setData: ((data) => void);
                setNextCommand: ((command) => void);
                state: State<unknown, OptionsConfig>;
            }
            • data: unknown

              The data that will be passed to the next command.

            • nextCommand: undefined | ResolvedCommand

              The next command that will be executed.

            • setData: ((data) => void)

              Override the data that will be passed to the next command.

                • (data): void
                • Parameters

                  • data: unknown

                    The new data.

                  Returns void

            • setNextCommand: ((command) => void)

              Override the next command that will be executed.

                • (command): void
                • Parameters

                  Returns void

            • state: State<unknown, OptionsConfig>

              The state object.

          Returns void

      beforeParse?: ((payload) => void)

      A hook that runs before parsing the command string using the options config from plugins and imported command modules.

      Type declaration

        • (payload): void
        • Parameters

          • payload: {
                commandString: string | string[];
                context: Context<OptionsConfig>;
                optionsConfig: OptionsConfig;
                setParseFn: ((parseFn) => void);
                setParsedOptionsAndSkip: ((optionValues) => void);
                skip: (() => void);
            }
            • commandString: string | string[]

              The command string that was passed to the CLI.

            • context: Context<OptionsConfig>

              The command's context object.

            • optionsConfig: OptionsConfig

              The context's final options config from all plugins and resolved commands.

            • setParseFn: ((parseFn) => void)

              Override the context's configured parse function.

                • (parseFn): void
                • Parameters

                  Returns void

            • setParsedOptionsAndSkip: ((optionValues) => void)

              Manually set the parsed options and skip parsing.

                • (optionValues): void
                • Parameters

                  • optionValues: {}

                    The parsed option values.

                    Returns void

              • skip: (() => void)

                Skip parsing the command.

                  • (): void
                  • Returns void

            Returns void

        beforeResolve?: ((payload) => void)

        A hook that runs before attempting to locate and import command modules.

        Type declaration

          • (payload): void
          • Parameters

            • payload: {
                  addResolvedCommands: ((result) => void);
                  commandString: string;
                  commandsDir: string;
                  context: Context<OptionsConfig>;
                  setParseFn: ((parseFn) => void);
                  setResolveFn: ((resolveFn) => void);
                  skip: (() => void);
              }
              • addResolvedCommands: ((result) => void)

                Add additional resolved commands to the context.

              • commandString: string

                The command string that was passed to the CLI.

              • commandsDir: string

                The path to the directory where the commands live.

              • context: Context<OptionsConfig>

                The command's context object.

              • setParseFn: ((parseFn) => void)

                Override the context's configured parse function.

                  • (parseFn): void
                  • Parameters

                    Returns void

              • setResolveFn: ((resolveFn) => void)

                Override the context's configured resolve function.

                  • (resolveFn): void
                  • Parameters

                    Returns void

              • skip: (() => void)

                Skip resolving the command.

                  • (): void
                  • Returns void

            Returns void

        command?: string | string[]

        The command string or array to be parsed and executed. If not provided, it defaults to system arguments.

        commandsDir?: string

        A directory path containing command modules.

        Default

        "<cwd>/commands" || "<caller-dir>/commands"
        
        initialData?: any

        Initial context or data to pass to commands during execution.

        onError?: ((payload) => void)

        A hook that runs whenever an error is thrown.

        Type declaration

          • (payload): void
          • Parameters

            • payload: {
                  context: Context<OptionsConfig>;
                  error: unknown;
                  ignore: (() => void);
                  setError: ((error) => void);
              }
              • context: Context<OptionsConfig>

                The command's context object.

              • error: unknown

                The error that was thrown.

              • ignore: (() => void)

                Prevent the error from being thrown.

                  • (): void
                  • Returns void

              • setError: ((error) => void)

                Override the error that will be thrown.

                  • (error): void
                  • Parameters

                    • error: unknown

                      The new error.

                    Returns void

            Returns void

        onExit?: ((payload) => void)

        A hook that runs whenever a plugin or command intends to exit the process.

        Type declaration

          • (payload): void
          • Parameters

            • payload: {
                  cancel: (() => void);
                  code: number;
                  context: Context<OptionsConfig>;
                  message?: any;
                  setCode: ((code) => void);
                  setMessage: ((message) => void);
              }
              • cancel: (() => void)

                Prevent the process from exiting.

                  • (): void
                  • Returns void

              • code: number

                The exit code.

              • context: Context<OptionsConfig>

                The command's context object.

              • Optional message?: any

                An optional message to log.

              • setCode: ((code) => void)

                Override the exit code.

                  • (code): void
                  • Parameters

                    • code: number

                      The new exit code.

                    Returns void

              • setMessage: ((message) => void)

                Override the message to log.

                  • (message): void
                  • Parameters

                    • message: any

                      The new message.

                    Returns void

            Returns void

        options?: OptionsConfig

        Options to include in the context.

        plugins?: Plugin[]

        An array of plugins that can modify or augment the behavior of commands.