Documentation
    Preparing search index...

    Class Kernel<Command>

    The Ace kernel manages the registration and execution of commands.

    The kernel is the main entry point of a console application, and is tailored for a standard CLI environment.

    const kernel = Kernel.create()

    kernel.defineFlag('help', {
    type: 'boolean',
    alias: 'h',
    description: 'Display help for the given command. When no command is given display help for the list command'
    })
    kernel.on('help', async (command, $kernel, options) => {
    options.args.unshift(command.commandName)
    await new HelpCommand($kernel, options, kernel.ui, kernel.prompt).exec()
    return true
    })

    kernel.addLoader(new FsLoader('./commands'))

    kernel.info.set('binary', 'node ace')
    kernel.info.set('Framework version', '9.1')
    kernel.info.set('App version', '1.1.1')

    await kernel.handle(process.argv.slice(2))

    Type Parameters

    Index

    Constructors

    Properties

    errorHandler: { render(error: unknown, kernel: Kernel<any>): Promise<any> } = ...

    The error handler for rendering exceptions

    commandExecutor: ExecutorContract<typeof BaseCommand> = ...

    The default executor for creating command instances and running them

    defaultCommand: typeof BaseCommand = ListCommand

    The default command to use when creating kernel instance via static create method

    exitCode?: number

    The exit code for the kernel. The exit code is inferred from the main command when not set explicitly

    ui: {} = ...

    The UI primitives to use within commands

    prompt: Prompt = ...

    Instance of prompt to display CLI prompts. We share a single instance with all the commands. This allows trapping prompts for commands executed internally

    info: Map<string, AllowedInfoValues> = ...

    CLI info map containing metadata about the application

    Accessors

    • get flags(): ({ name: string } & Flag)[]

      List of global flags available across all commands

      Returns ({ name: string } & Flag)[]

      kernel.flags // [{ name: 'help', type: 'boolean', ... }]
      

    Methods

    • Listen for CLI options and execute an action. Only one listener can be defined per option.

      The callbacks are only executed for the main command

      Parameters

      Returns this

    • Define a global flag that is applicable for all commands

      Parameters

      • name: string

        The flag name

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

        Flag configuration options

      Returns void

      kernel.defineFlag('verbose', { type: 'boolean', description: 'Enable verbose output' })
      
    • Register a commands loader. The commands will be collected by all loaders.

      In case multiple loaders return a single command, the command from the most recent loader will be used.

      Parameters

      Returns this

      kernel.addLoader(new FsLoader('./commands'))
      kernel.addLoader(() => import('./lazy-loader').then(m => new m.LazyLoader()))
    • Register alias for a command name

      Parameters

      • alias: string

        The alias name

      • command: string

        The command name (can include arguments)

      Returns this

      kernel.addAlias('m', 'make:model')
      kernel.addAlias('migrate:fresh', 'migration:rollback --to=0 && migration:run')
    • Check if a command or an alias is registered with kernel

      Parameters

      • commandName: string

      Returns boolean

    • Get the current state of the kernel.

      Returns "booted" | "idle" | "running" | "completed"

    • Get a list of commands for a specific namespace. All non-namespaces commands will be returned if no namespace is defined.

      Parameters

      • Optionalnamespace: string

      Returns CommandMetaData[]

    • Returns a reference for the default command. The return value is the default command constructor

      Returns Command

    • Returns reference to the main command

      Returns undefined | InstanceType<Command>

    • Returns an array of aliases registered.

      • Call getCommandAliases method to get aliases for a given command
      • Call getAliasCommand to get the command or a given alias

      Returns string[]

    • Returns an array of aliases for a given command

      Parameters

      • commandName: string

      Returns string[]

    • Returns a list of namespaces. The list is sorted alphabetically by the namespace name

      Returns string[]

    • Returns an array of command and aliases name suggestions for a given keyword.

      Parameters

      • keyword: string

      Returns string[]

    • Returns an array of namespaces suggestions for a given keyword.

      Parameters

      • keyword: string

      Returns string[]

    • Loads commands from all the registered loaders. The "addLoader" method must be called before calling the "load" method.

      Returns Promise<void>

    • Execute a command. The second argument is an array of command-line arguments (without the command name)

      Type Parameters

      Parameters

      • commandName: string

        The name of the command to execute

      • argv: string[]

        Array of command-line arguments

      Returns Promise<InstanceType<T>>

      await kernel.exec('make:model', ['User', '--migration'])
      
    • Creates a command instance by parsing and validating the command-line arguments

      Type Parameters

      Parameters

      • command: T

        The command class to instantiate

      • argv: string | string[]

        Command-line arguments as string or array

      Returns Promise<InstanceType<T>>

      const commandInstance = await kernel.create(MyCommand, ['--verbose', 'arg1'])
      
    • Handle process argv and execute the command. Calling this method makes kernel own the process and register SIGNAL listeners

      Parameters

      • argv: string[]

        Array of command-line arguments from process.argv

      Returns Promise<void>

      await kernel.handle(process.argv.slice(2))
      
    • A named function that returns true. To be used by flag listeners

      Returns boolean