UNPKG

6.71 kBTypeScriptView Raw
1import { Prompt } from '@poppinss/prompts';
2import { BaseCommand } from './commands/base.js';
3import type { Flag, UIPrimitives, FlagListener, CommandMetaData, LoadersContract, ExecutorContract, LoadedHookHandler, AllowedInfoValues, LoadingHookHandler, FindingHookHandler, AbstractBaseCommand, ExecutedHookHandler, ExecutingHookHandler } from './types.js';
4/**
5 * The Ace kernel manages the registration and execution of commands.
6 *
7 * The kernel is the main entry point of a console application, and
8 * is tailored for a standard CLI environment.
9 */
10export declare class Kernel<Command extends AbstractBaseCommand> {
11 #private;
12 errorHandler: {
13 render(error: unknown, kernel: Kernel<any>): Promise<any>;
14 };
15 /**
16 * The default executor for creating command's instance
17 * and running them
18 */
19 static commandExecutor: ExecutorContract<typeof BaseCommand>;
20 /**
21 * The default command to use when creating kernel instance
22 * via "static create" method.
23 */
24 static defaultCommand: typeof BaseCommand;
25 /**
26 * Creates an instance of kernel with the default executor
27 * and default command
28 */
29 static create(): Kernel<typeof BaseCommand>;
30 /**
31 * The exit code for the kernel. The exit code is inferred
32 * from the main code when not set explicitly.
33 */
34 exitCode?: number;
35 /**
36 * The UI primitives to use within commands
37 */
38 ui: UIPrimitives;
39 /**
40 * Instance of prompt to display CLI prompts. We share
41 * a single instance with all the commands. This
42 * allows trapping prompts for commands executed
43 * internally.
44 */
45 prompt: Prompt;
46 /**
47 * CLI info map
48 */
49 info: Map<string, AllowedInfoValues>;
50 /**
51 * List of global flags
52 */
53 get flags(): ({
54 name: string;
55 } & Flag)[];
56 constructor(defaultCommand: Command, executor: ExecutorContract<Command>);
57 /**
58 * Listen for CLI options and execute an action. Only one listener
59 * can be defined per option.
60 *
61 * The callbacks are only executed for the main command
62 */
63 on(option: string, callback: FlagListener<Command>): this;
64 /**
65 * Define a global flag that is applicable for all the
66 * commands.
67 */
68 defineFlag(name: string, options: Partial<Flag> & {
69 type: 'string' | 'boolean' | 'array' | 'number';
70 }): void;
71 /**
72 * Register a commands loader. The commands will be collected by
73 * all the loaders.
74 *
75 * Incase multiple loaders returns a single command, the command from the
76 * most recent loader will be used.
77 */
78 addLoader(loader: LoadersContract<Command> | (() => Promise<LoadersContract<Command>>)): this;
79 /**
80 * Register alias for a comamnd name.
81 */
82 addAlias(alias: string, command: string): this;
83 /**
84 * Check if a command or an alias is registered with kernel
85 */
86 hasCommand(commandName: string): boolean;
87 /**
88 * Get the current state of the kernel.
89 */
90 getState(): "booted" | "idle" | "running" | "completed";
91 /**
92 * Returns a flat list of commands metadata registered with the kernel.
93 * The list is sorted alphabetically by the command name.
94 */
95 getCommands(): CommandMetaData[];
96 /**
97 * Get a list of commands for a specific namespace. All non-namespaces
98 * commands will be returned if no namespace is defined.
99 */
100 getNamespaceCommands(namespace?: string): CommandMetaData[];
101 /**
102 * Returns the command metadata by its name. Returns null when the
103 * command is missing.
104 */
105 getCommand(commandName: string): CommandMetaData | null;
106 /**
107 * Returns a reference for the default command. The return value
108 * is the default command constructor
109 */
110 getDefaultCommand(): Command;
111 /**
112 * Returns reference to the main command
113 */
114 getMainCommand(): InstanceType<Command> | undefined;
115 /**
116 * Returns an array of aliases registered.
117 *
118 * - Call `getCommandAliases` method to get aliases for a given command
119 * - Call `getAliasCommand` to get the command or a given alias
120 */
121 getAliases(): string[];
122 /**
123 * Returns the command metata for a given alias. Returns null
124 * if alias is not recognized.
125 */
126 getAliasCommand(alias: string): CommandMetaData | null;
127 /**
128 * Returns an array of aliases for a given command
129 */
130 getCommandAliases(commandName: string): string[];
131 /**
132 * Returns a list of namespaces. The list is sorted alphabetically
133 * by the namespace name
134 */
135 getNamespaces(): string[];
136 /**
137 * Returns an array of command and aliases name suggestions for
138 * a given keyword.
139 */
140 getCommandSuggestions(keyword: string): string[];
141 /**
142 * Returns an array of namespaces suggestions for a given keyword.
143 */
144 getNamespaceSuggestions(keyword: string): string[];
145 /**
146 * Listen for the event before we begin the process of finding
147 * the command.
148 */
149 finding(callback: FindingHookHandler): this;
150 /**
151 * Listen for the event when importing the command
152 */
153 loading(callback: LoadingHookHandler): this;
154 /**
155 * Listen for the event when the command has been imported
156 */
157 loaded(callback: LoadedHookHandler<Command>): this;
158 /**
159 * Listen for the event before we start to execute the command.
160 */
161 executing(callback: ExecutingHookHandler<InstanceType<Command>>): this;
162 /**
163 * Listen for the event after the command has been executed
164 */
165 executed(callback: ExecutedHookHandler<InstanceType<Command>>): this;
166 /**
167 * Loads commands from all the registered loaders. The "addLoader" method
168 * must be called before calling the "load" method.
169 */
170 boot(): Promise<void>;
171 /**
172 * Find a command by its name
173 */
174 find<T extends Command>(commandName: string): Promise<T>;
175 /**
176 * Execute a command. The second argument is an array of commandline
177 * arguments (without the command name)
178 */
179 exec<T extends Command>(commandName: string, argv: string[]): Promise<InstanceType<T>>;
180 /**
181 * Creates a command instance by parsing and validating
182 * the command-line arguments.
183 */
184 create<T extends Command>(command: T, argv: string | string[]): Promise<InstanceType<T>>;
185 /**
186 * Handle process argv and execute the command. Calling this method
187 * makes kernel own the process and register SIGNAL listeners
188 */
189 handle(argv: string[]): Promise<void>;
190 /**
191 * A named function that returns true. To be used
192 * by flag listeners
193 */
194 shortcircuit(): boolean;
195}