import * as wooks from 'wooks'; import { TWooksHandler, TWooksOptions, WooksAdapterBase, Wooks } from 'wooks'; import * as _prostojs_cli_help from '@prostojs/cli-help'; import { CliHelpRenderer, TCliEntry, TCliHelpOptions } from '@prostojs/cli-help'; import { TConsoleBase } from '@prostojs/logger'; import * as _wooksjs_event_core from '@wooksjs/event-core'; import { EventContextOptions, EventKindSeeds } from '@wooksjs/event-core'; export { EventContext, EventContextOptions, useLogger, useRouteParams } from '@wooksjs/event-core'; import minimist from 'minimist'; import { TProstoRouterPathHandle } from '@prostojs/router'; /** Custom data attached to CLI help entries for handler resolution. */ interface TCliHelpCustom { handler: TWooksHandler; /** * ### Callback for registered path * * @param path registered path * @param aliasType 0 - direct command, 1 - direct alias, 2 - computed alias */ cb?: (path: string, aliasType: number, route?: TProstoRouterPathHandle) => void; } /** CLI help renderer type parameterized with custom help data. */ type TCliHelpRenderer = CliHelpRenderer; /** Shortcut mappings for CLI event methods. */ declare const cliShortcuts: { cli: string; }; /** Configuration options for the WooksCli adapter. */ interface TWooksCliOptions { onError?: (e: Error) => void; onNotFound?: TWooksHandler; onUnknownCommand?: (params: string[], raiseError: () => void) => unknown; logger?: TConsoleBase; eventOptions?: EventContextOptions; cliHelp?: TCliHelpRenderer | TCliHelpOptions; router?: TWooksOptions['router']; } /** Entry definition for registering a CLI command with its handler and help metadata. */ interface TWooksCliEntry extends Omit>, 'custom' | 'command'> { onRegister?: TCliHelpCustom['cb']; handler: TWooksHandler; } /** Wooks adapter for building CLI applications with command routing and help generation. */ declare class WooksCli extends WooksAdapterBase { protected opts?: TWooksCliOptions | undefined; protected logger: TConsoleBase; protected cliHelp: TCliHelpRenderer; constructor(opts?: TWooksCliOptions | undefined, wooks?: Wooks | WooksAdapterBase); /** * ### Register CLI Command * Command path segments may be separated by / or space. * * For example the folowing path are interpreted the same: * - "command test use:dev :name" * - "command/test/use:dev/:name" * * Where name will become an argument * * ```js * // example without options * app.cli('command/:arg', () => 'arg = ' + useRouteParams().params.arg ) * * // example with options * app.cli('command/:arg', { * description: 'Description of the command', * options: [{ keys: ['project', 'p'], description: 'Description of the option', value: 'myProject' }], * args: { arg: 'Description of the arg' }, * aliases: ['cmd'], // alias "cmd/:arg" will be registered * examples: [{ * description: 'Example of usage with someProject', * cmd: 'argValue -p=someProject', * // will result in help display: * // "# Example of usage with someProject\n" + * // "$ myCli command argValue -p=someProject\n" * }], * handler: () => 'arg = ' + useRouteParams().params.arg * }) * ``` * * @param path command path * @param _options handler or options * * @returns */ cli>(path: string, _options: TWooksCliEntry | TWooksHandler): wooks.TProstoRouterPathHandle; protected alreadyComputedAliases: boolean; protected computeAliases(): void; /** * ## run * ### Start command processing * Triggers command processing * * By default takes `process.argv.slice(2)` as a command * * It's possible to replace the command by passing an argument * * @param _argv optionally overwrite `process.argv.slice(2)` with your `argv` array */ run(_argv?: string[], _opts?: minimist.Opts): Promise; protected onError(e: Error): void; /** * Triggers `unknown command` processing and callbacks * @param pathParams `string[]` containing command */ onUnknownCommand(pathParams: string[]): void; protected error(e: string | Error): void; } /** * Creates a new WooksCli application instance. * @param opts - CLI adapter configuration options * @param wooks - Optional existing Wooks or adapter instance to attach to * @returns A new WooksCli instance * @example * ```ts * const app = createCliApp() * app.cli('greet/:name', () => `Hello, ${useRouteParams().get('name')}!`) * app.run() * ``` */ declare function createCliApp(opts?: TWooksCliOptions, wooks?: Wooks | WooksAdapterBase): WooksCli; declare const cliKind: _wooksjs_event_core.EventKind<{ argv: _wooksjs_event_core.SlotMarker; pathParams: _wooksjs_event_core.SlotMarker; command: _wooksjs_event_core.SlotMarker; opts: _wooksjs_event_core.SlotMarker; cliHelp: _wooksjs_event_core.SlotMarker; }>; declare const flagsKey: _wooksjs_event_core.Key>; /** * ## useCliHelp * ### Composable * ```js * // example of printing cli instructions * const { print } = useCliHelp() * // print with colors * print(true) * // print with no colors * // print(false) * ``` * @returns */ declare function useCliHelp(): { getCliHelp: () => TCliHelpRenderer; getEntry: () => _prostojs_cli_help.TCliEntry; render: (width?: number, withColors?: boolean) => string[]; print: (withColors?: boolean) => void; }; /** * ## useAutoHelp * ### Composable * * Prints help if `--help` option provided. * * ```js * // example of use: print help and exit * app.cli('test', () => { * useAutoHelp() && process.exit(0) * return 'hit test command' * }) * * // add option -h to print help, no colors * app.cli('test/nocolors', () => { * useAutoHelp(['help', 'h'], false) && process.exit(0) * return 'hit test nocolors command' * }) * ``` * @param keys default `['help']` - list of options to trigger help render * @param colors default `true`, prints with colors when true * @returns true when --help was provided. Otherwise returns false */ declare function useAutoHelp(keys?: string[], colors?: boolean): true | undefined; /** * ##useCommandLookupHelp * ### Composable * * Tries to find valid command based on provided command. * * If manages to find a valid command, throws an error * suggesting a list of valid commands * * Best to use in `onUnknownCommand` callback: * * ```js * const app = createCliApp({ * onUnknownCommand: (path, raiseError) => { * // will throw an error suggesting a list * // of valid commands if could find some * useCommandLookupHelp() * // fallback to a regular error handler * raiseError() * }, * }) * ``` * * @param lookupDepth depth of search in backwards * @example * * For provided command `run test:drive dir` * - lookup1: `run test:drive dir` (deep = 0) * - lookup2: `run test:drive` (deep = 1) * - lookup3: `run test` (deep = 2) * - lookup4: `run` (deep = 3) * ... */ declare function useCommandLookupHelp(lookupDepth?: number): void; /** * Get CLI Options * * @returns an object with CLI options */ declare function useCliOptions(): Record; /** * Getter for Cli Option value * * @param name name of the option * @returns value of a CLI option * @example * ```ts * const verbose = useCliOption('verbose') * if (verbose) { * console.log('Verbose mode enabled') * } * ``` */ declare function useCliOption(name: string): string | boolean; /** Creates a CLI event context and runs `fn` inside it. */ declare function createCliContext(options: EventContextOptions, seeds: EventKindSeeds, fn: () => R): R; export { WooksCli, cliKind, cliShortcuts, createCliApp, createCliContext, flagsKey, useAutoHelp, useCliHelp, useCliOption, useCliOptions, useCommandLookupHelp }; export type { TCliHelpCustom, TCliHelpRenderer, TWooksCliEntry, TWooksCliOptions };