import type { cliui } from '@poppinss/cliui'; import type { Arguments, Options } from 'yargs-parser'; import type { HookHandler } from '@poppinss/hooks/types'; import type { Kernel } from './kernel.ts'; /** * Parsed output of yargs */ export type YargsOutput = Arguments; /** * Parsed output from the parser */ export type ParsedOutput = YargsOutput & { nodeArgs: string[]; /** * Parsed arguments */ args: (any | any[])[]; /** * Left over arguments after parsing flags * and args */ _: Array; /** * An array of unknown flags that were parsed */ unknownFlags: string[]; /** * List of parsed flags */ flags: { [argName: string]: any; }; }; /** * The UI primitives used by commands */ export type UIPrimitives = ReturnType; /** * All loaders must adhere to the LoadersContract */ export interface LoadersContract { /** * The method should return an array of commands metadata */ getMetaData(): Promise; /** * The method should return the command instance by command * name */ getCommand(command: CommandMetaData): Promise; } /** * Command executor is used to create a new instance of the command * and run it. */ export interface ExecutorContract { /** * Create a new instance of the command */ create(command: Command, parsedOutput: ParsedOutput, kernel: Kernel): Promise> | InstanceType; /** * Run the command */ run>(command: Instance, kernel: Kernel): Promise; } /** * Parser options accepted by the yargs to process * flags */ export type FlagsParserOptions = { all: string[]; array?: string[]; boolean?: Options['boolean']; string?: Options['string']; number?: Options['boolean']; default?: Options['default']; coerce?: Options['coerce']; alias?: Options['alias']; count?: Options['count']; }; /** * The options accepted by the arguments parser */ export type ArgumentsParserOptions = { type: 'string' | 'spread'; default?: any; parse?: (value: any) => any; }; /** * Options for defining an argument */ export type BaseArgument = { name: string; argumentName: string; required?: boolean; description?: string; default?: T; }; /** * Type for a string argument */ export type StringArgument = BaseArgument & { type: 'string'; /** * Whether or not to allow empty values. When set to false, * the validation will fail if the argument is provided * an empty string * * Defaults to false */ allowEmptyValue?: boolean; parse?: (input: T) => T; }; /** * Type for a spread argument */ export type SpreadArgument = BaseArgument & { type: 'spread'; /** * Whether or not to allow empty values. When set to false, * the validation will fail if the argument is provided * an empty string * * Defaults to false */ allowEmptyValue?: boolean; parse?: (input: T extends any[] ? T : [T]) => T; }; /** * A union of known arguments */ export type Argument = StringArgument | SpreadArgument; /** * Base properties for a flag */ export type BaseFlag = { name: string; flagName: string; required?: boolean; default?: T; description?: string; alias?: string | string[]; }; /** * String flag */ export type StringFlag = BaseFlag & { type: 'string'; /** * Whether or not to allow empty values. When set to false, * the validation will fail if the flag is mentioned but * no value is provided * * Defaults to false */ allowEmptyValue?: boolean; parse?: (input: T) => T; }; /** * Boolean flag */ export type BooleanFlag = BaseFlag & { type: 'boolean'; /** * Whether or not to display the negated variant in the * help output. * * Applicable for boolean flags only * * Defaults to false */ showNegatedVariantInHelp?: boolean; parse?: (input: T) => T; }; /** * Number flag */ export type NumberFlag = BaseFlag & { type: 'number'; parse?: (input: T) => T; }; /** * An array of string flag */ export type ArrayFlag = BaseFlag & { type: 'array'; /** * Whether or not to allow empty values. When set to false, * the validation will fail if the flag is mentioned but * no value is provided * * Defaults to false */ allowEmptyValue?: boolean; parse?: (input: T) => T; }; /** * A union of known flags */ export type Flag = StringFlag | BooleanFlag | NumberFlag | ArrayFlag; /** * Command metdata required to display command help. */ export type CommandMetaData = { /** * Help text for the command */ help?: string | string[]; /** * The name of the command */ commandName: string; /** * The command description to show on the help * screen */ description: string; /** * Command namespace. The namespace is extracted * from the command name */ namespace: string | null; /** * Command aliases. The same command can be run using * these aliases as well. */ aliases: string[]; /** * Flags accepted by the command */ flags: Omit[]; /** * Args accepted by the command */ args: Omit[]; /** * Command configuration options */ options: CommandOptions; } & Record; /** * Static set of command options */ export type CommandOptions = { /** * Whether or not to allow for unknown flags. If set to false, * the command will not run when unknown flags are provided * through the CLI * * Defaults to false */ allowUnknownFlags?: boolean; /** * When flag set to true, the kernel will not trigger the termination * process unless the command explicitly calls the terminate method. * * Defaults to false */ staysAlive?: boolean; } & Record; /** * Finding hook handler and data */ export type FindingHookArgs = [[string], [string]]; /** * Finding hook handler type */ export type FindingHookHandler = HookHandler; /** * Loading hook handler and data */ export type LoadingHookArgs = [[CommandMetaData], [CommandMetaData]]; /** * Loading hook handler type */ export type LoadingHookHandler = HookHandler; /** * Loaded hook handler and data */ export type LoadedHookArgs = [[Command], [Command]]; /** * Loaded hook handler type */ export type LoadedHookHandler = HookHandler[0], LoadedHookArgs[1]>; /** * Executing hook handler and data */ export type ExecutingHookArgs = [[Command, boolean], [Command, boolean]]; /** * Executing hook handler type */ export type ExecutingHookHandler = HookHandler[0], ExecutingHookArgs[1]>; /** * Executed hook handler and data */ export type ExecutedHookArgs = ExecutingHookArgs; /** * Executed hook handler type */ export type ExecutedHookHandler = ExecutingHookHandler; /** * A listener that listeners for flags when they are mentioned. */ export type FlagListener = (command: Command, kernel: Kernel, parsedOutput: ParsedOutput) => any | Promise; /** * Commands and options list table */ export type ListTable = { columns: { option: string; description: string; }[]; heading: string; }; /** * A union of data types allowed for the info key-value pair */ export type AllowedInfoValues = number | boolean | string | string[] | number[] | boolean[]; /** * Abstract command defines the mandatory properties on a * command class needed by the internals of ace. */ export type AbstractBaseCommand = { commandName: string; options: CommandOptions; serialize(): CommandMetaData; validate(parsedOutput: ParsedOutput): void; getParserOptions(options?: FlagsParserOptions): { flagsParserOptions: Required; argumentsParserOptions: ArgumentsParserOptions[]; }; new (...args: any[]): { hydrate(): void; exitCode?: number; }; }; /** * Command execution tracing data for observability */ export type CommandExecTracingData = { command: AbstractBaseCommand; commandInstance: InstanceType; argv: string[]; };