Documentation
    Preparing search index...

    The base command sets the foundation for defining ace commands. Every command should inherit from the base command.

    export class MyCommand extends BaseCommand {
    static commandName = 'my:command'
    static description = 'My custom command'

    async run() {
    this.logger.info('Hello from my command!')
    }
    }

    Hierarchy (View Summary)

    Index

    Constructors

    • Create a new base command instance

      Parameters

      • kernel: Kernel<any>

        The Ace kernel instance

      • parsed: ParsedOutput

        The parsed CLI input

      • ui: {}

        UI primitives for output

      • prompt: Prompt

        Prompt utilities for user interaction

      Returns BaseCommand

    Properties

    booted: boolean = false

    Whether the command class has been booted

    Configuration options accepted by the command

    aliases: string[]

    A collection of aliases for the command

    commandName: string

    The command name one can type to run the command

    description: string

    The command description

    help?: string | string[]

    The help text for the command. Help text can be a multiline string explaining the usage of command

    args: Argument[]

    Registered arguments

    flags: Flag[]

    Registered flags

    exitCode?: number

    The exit code for the command

    error?: any

    The error raised at the time of executing the command. The value is undefined if no error is raised.

    result?: any

    The result property stores the return value of the "run" method (unless command sets it explicitly)

    ui: {}

    UI primitives for output

    prompt: Prompt

    Prompt utilities for user interaction

    Accessors

    • get logger(): Logger

      Logger to log messages

      Returns Logger

      this.logger.info('Command executed successfully')
      this.logger.error('Something went wrong')
    • get colors(): Colors

      Add colors to console messages

      Returns Colors

      this.logger.info(this.colors.green('Success!'))
      this.logger.error(this.colors.red('Error!'))
    • get isMain(): boolean

      Is the current command the main command executed from the CLI

      Returns boolean

      if (this.isMain) {
      this.logger.info('This is the main command')
      }

    Methods

    • Define static properties on the class. During inheritance, certain properties must inherit from the parent.

      Returns void

      MyCommand.boot()
      
    • Specify the argument the command accepts. The arguments via the CLI will be accepted in the same order as they are defined.

      Mostly, you will be using the @args decorator to define the arguments.

      Parameters

      • name: string

        The name of the argument

      • options: Partial<Argument> & { type: "string" | "spread" }

        Configuration options for the argument

      Returns void

      Command.defineArgument('entity', { type: 'string' })
      Command.defineArgument('files', { type: 'spread', required: false })
    • Specify a flag the command accepts.

      Mostly, you will be using the @flags decorator to define a flag.

      Parameters

      • name: string

        The name of the flag

      • options: Partial<Flag> & { type: "string" | "number" | "boolean" | "array" }

        Configuration options for the flag

      Returns void

      Command.defineFlag('connection', { type: 'string', required: true })
      Command.defineFlag('force', { type: 'boolean' })
      Command.defineFlag('tags', { type: 'array' })
    • Validate the yargs parsed output against the command.

      Parameters

      Returns void

      const parsed = { args: ['value'], flags: { force: true }, unknownFlags: [] }
      MyCommand.validate(parsed)
    • Hydrate command by setting class properties from the parsed output

      Returns void

      command.hydrate()
      console.log(command.name) // Argument value
      console.log(command.force) // Flag value
    • The run method should include the implementation for the command.

      Parameters

      • ..._: any[]

        Additional arguments (not used in base implementation)

      Returns Promise<any>

      async run() {
      this.logger.info('Running my command')
      return 'Command completed'
      }
    • Executes the command by running the command's run method.

      Returns Promise<any>

      const result = await command.exec()
      console.log('Exit code:', command.exitCode)
    • JSON representation of the command

      Returns {
          commandName: string;
          options: CommandOptions;
          args: any[];
          flags: { [argName: string]: any };
          error: any;
          result: any;
          exitCode: undefined | number;
      }

      const json = command.toJSON()
      console.log(json.commandName, json.exitCode)
    • Assert the command exits with a given exit code

      Parameters

      • code: number

        The expected exit code

      Returns void

      command.assertExitCode(0) // Assert successful execution
      command.assertExitCode(1) // Assert failure
    • Assert the command does not exit with a given exit code

      Parameters

      • code: number

        The exit code that should not be used

      Returns void

      command.assertNotExitCode(1) // Assert no failure
      
    • Assert the command exits with zero exit code

      Returns void

      command.assertSucceeded() // Assert success
      
    • Assert the command exits with non-zero exit code

      Returns void

      command.assertFailed() // Assert failure
      
    • Assert command logs the expected message

      Parameters

      • message: string

        The expected log message

      • Optionalstream: "stdout" | "stderr"

        Optional stream to check ('stdout' or 'stderr')

      Returns void

      command.assertLog('Command executed successfully')
      command.assertLog('Error occurred', 'stderr')
    • Assert command logs a message matching the given regex

      Parameters

      • matchingRegex: RegExp

        The regex pattern to match against log messages

      • Optionalstream: "stdout" | "stderr"

        Optional stream to check ('stdout' or 'stderr')

      Returns void

      command.assertLogMatches(/^Command.*completed$/)
      command.assertLogMatches(/error/i, 'stderr')
    • Assert the command prints a table with the expected rows to stdout

      Parameters

      • rows: string[][]

        The expected table rows as arrays of strings

      Returns void

      command.assertTableRows([
      ['Name', 'Age'],
      ['John', '25'],
      ['Jane', '30']
      ])