BaseCommand
The base command class for creating custom Ace commands in AdonisJS applications. This class extends the base Ace command with AdonisJS-specific functionality like dependency injection and application lifecycle management.Import
Basic Usage
Static Properties
The unique name of the command used to invoke it from CLI
A brief description of what the command does
Configuration options for the command:
startApp?: boolean- Whether to start the application before running the commandstaysAlive?: boolean- Whether the command keeps the process alive after execution
Constructor
The AdonisJS application instance
The Ace kernel instance
Parsed command line output containing args and flags
UI primitives for rendering output
Prompt utilities for user interaction
Instance Properties
Reference to the AdonisJS application instance
Reference to the Ace kernel instance
Logger instance for outputting messages to the console
UI primitives inherited from base Ace command
Prompt utilities for interactive user input
The exit code to return when the command completes (0 for success, 1 for error)
Error object if the command execution failed
The result value returned from the run method
Returns true if the command is configured to keep the process alive
Returns true if the command is configured to start the application
Methods
run()
The main method that contains the command logic. Must be implemented by subclasses. Returns:Promise<any>
createCodemods()
Creates the codemods module to modify source files programmatically. This method provides access to AST-based code transformations. Returns:Promise<Codemods>
terminate()
Terminate the application gracefully. This method should be preferred over callingapp.terminate() directly as it only triggers termination when the current command is the main command responsible for the process.
Returns: Promise<void>
exec()
Executes the lifecycle hooks and the run method from the command. This method is called internally by the kernel. Returns:Promise<any>
Lifecycle
- Calls
prepare()if defined - Calls
interact()if defined - Calls
run()(required) - Calls
completed()if defined - Handles errors and sets exit codes
Lifecycle Hooks
prepare()
The prepare template method is used to prepare the state for the command. This is the first method executed on a given command instance. Returns:Promise<any> | any
interact()
The interact template method is used to display prompts to the user. The method is called after the prepare method. Returns:Promise<any> | any
completed()
The completed method is invoked after the command finishes or results in an error. You can access the command error using thethis.error property.
Returns: Promise<boolean | void> | boolean | void
Returning true from completed method suppresses the error reporting to the kernel layer.
Decorators
@args
Define positional arguments for the command.Argument Types
@args.string()- String argument@args.spread()- Spread argument (captures remaining arguments)
@flags
Define optional flags for the command.Flag Types
@flags.boolean()- Boolean flag (true/false)@flags.string()- String flag@flags.number()- Number flag@flags.array()- Array flag (can be specified multiple times)
Flag Options
Description shown in help output
Default value when flag is not provided
Short alias for the flag (e.g., ‘-v’ for ‘—verbose’)
Whether the flag is required
Complete Example
See Also
- Kernel API - Manage and execute commands
- Codemods API - Programmatically modify source files