import { readdir } from 'node:fs/promises' import { join } from 'node:path' import { type Application, enterDeferredScope, enterWorkContext, flushDeferred } from '@elvel/core' import pc from 'picocolors' import type { Command } from './command.ts' import { Output } from './output.ts' import { formatUsage, InputParseError, missingArguments, parseSignature } from './signature.ts' export type CommandConstructor = (new () => Command) & { signature: string description: string holdsProcess?: boolean } /** * Console kernel — the CLI itself. * * Commands are registered by service providers (framework commands) or * discovered from `app/Console/Commands` (application commands). */ export class Kernel { private readonly commands = new Map() private readonly output = new Output() /** * Did the command that just ran leave something running? * * Read by the entry point, which exits the process on a command's behalf. For * `serve` that would kill the server the command had just started, so it asks * first: * * ```ts * const code = await kernel.run() * * if (kernel.holdsProcess) process.exitCode = code * else process.exit(code) * ``` * * Here rather than on the command instance because the instance is private to * `execute()` and gone by the time `run()` answers. */ holdsProcess = false constructor(private readonly app: Application) {} register(...commands: CommandConstructor[]): this { for (const command of commands) { const name = parseSignature(command.signature).name if (name === '') { throw new Error(`Command ${command.name} has an empty signature.`) } this.commands.set(name, command) } return this } /** Auto-discover commands from a directory of modules. */ async loadFrom(directory: string): Promise { let entries: string[] try { entries = await readdir(directory) } catch { return this } for (const entry of entries.sort()) { if (!/\.(ts|js|mts|mjs)$/.test(entry) || entry.endsWith('.d.ts')) continue const module = (await import(join(directory, entry))) as Record for (const exported of Object.values(module)) { if (this.looksLikeCommand(exported)) this.register(exported) } } return this } all(): CommandConstructor[] { return [...this.commands.values()] } /** * Is this command registered at all? * * Asked by commands that orchestrate others — `dev` runs a queue worker and the * scheduler, and neither exists in an application scaffolded with `--kit=none`, * which prunes those packages. Without this it started them anyway and the * first one to exit took the server down with it, reporting * `Command "queue:work" is not defined` as though the developer had typed it. */ has(name: string): boolean { return this.commands.has(name) } async run(argv: string[] = Bun.argv.slice(2)): Promise { const [name, ...rest] = argv if (name === undefined || name === 'list' || name === '--help' || name === '-h') { this.renderList() return 0 } if (name === '--version' || name === '-V') { this.output.line(`Elvel ${this.app.config.get('app.version', '0.0.1')}`) return 0 } const command = this.commands.get(name) if (!command) { this.output.error(`Command "${name}" is not defined.`) this.suggest(name) return 1 } if (rest.includes('--help') || rest.includes('-h')) { this.renderHelp(command) return 0 } /** * `--isolated`: run only if no other copy holds the lock. * * Exits **0** when it cannot, which looks wrong and is not: a deploy runs * `migrate --isolated` on every node, and exactly one of them should do the * work while the others carry on. A failure code there would fail the * deploy on every node but one. */ if (rest.includes('--isolated')) { const released = await this.acquireIsolationLock(name, command) if (!released) return 0 try { return await this.execute( command, rest.filter((token) => token !== '--isolated') ) } finally { await released() } } return this.execute(command, rest) } /** Run one resolved command, with its missing arguments filled in. */ private async execute(command: CommandConstructor, rest: string[]): Promise { this.holdsProcess = false /** * One command, one context, one deferred queue. * * `flushDeferred`'s own docstring said this already happened here — "called * by the http layer once the response is out, and by the console kernel when * a command finishes" — and the second half was not true. So `defer()` inside * a command pushed onto the process-wide array nothing drains: the callback * never ran, the command reported success, and the process exited with the * work still queued. Measured with a command that deferred a line and never * printed it. * * `enterWorkContext` marks this one, so a nested `elvel` call — which * `execute` supports through `Command.bind` — gets its own rather than * inheriting this one's. */ enterWorkContext() const deferred = enterDeferredScope() try { const supplied = await this.promptForMissing(command, rest) const instance = new command().bind(this.app, supplied, (nested, nestedArgv = []) => this.run([nested, ...nestedArgv]) ) const status = await instance.handle() const code = typeof status === 'number' ? status : 0 /** * Only when it actually got going. * * `serve` declares that it holds the process, and it also returns 1 when * the port is taken — nothing is listening then, so nothing is holding the * loop, and staying alive for it would hang the terminal on the one * failure a developer most needs to see reported. */ this.holdsProcess = command.holdsProcess === true && code === 0 return code } catch (error) { if (error instanceof InputParseError) { this.output.error(error.message) this.output.line() this.renderHelp(command) return 1 } this.output.error(error instanceof Error ? error.message : String(error)) if (this.app.hasDebugModeEnabled() && error instanceof Error) { this.output.comment(error.stack ?? '') } return 1 } finally { /** * After the exit code is decided, and whatever it was. * * A command that failed still deferred what it deferred; dropping that work * because the command returned 1 is a second failure hidden behind the * first. Reported through the application's handler rather than swallowed, * and awaited, because deferring a write and then exiting is how the write * does not happen. */ await flushDeferred( (error) => this.app.bound('exception.handler') ? this.app.make('exception.handler').report(error) : this.output.error(error instanceof Error ? error.message : String(error)), deferred ) } } /** * Take the isolation lock, returning how to release it — or nothing. * * The lock lives in the cache, because the point is to exclude a *different * process*, often on a different machine. Without a cache store there is * nothing to exclude with, and saying so beats running anyway while claiming * to be isolated. */ private async acquireIsolationLock( name: string, command: CommandConstructor ): Promise<(() => Promise) | undefined> { if (!(command as { isolatable?: boolean }).isolatable) { this.output.error(`The ${name} command cannot be run with --isolated.`) return undefined } if (!this.app.bound('cache')) { this.output.error('--isolated needs a cache store. Register CacheServiceProvider.') return undefined } const store = this.app.make('cache').store() const key = `elvel:command:${name}` // An expiry, not a bare flag: a process killed mid-command must not lock the // command out for ever, and an hour is longer than any command should be. if (!(await store.add(key, new Date().toISOString(), 3600))) { this.output.comment(`The ${name} command is already running elsewhere.`) return undefined } return async () => { await store.forget(key) } } /** * Ask for the required arguments that were not given. * * `elvel make:model` with nothing after it used to fail with * `missing: "name"`, which is correct and useless — the person already knows * they left it out. Asking is what every generator in every other framework * does, and it is the difference between reading the help and getting on with * it. * * Only when a terminal is attached and `--no-interaction` was not passed: in * CI a prompt is not a question, it is a hang. */ private async promptForMissing(command: CommandConstructor, argv: string[]): Promise { if (argv.includes('--no-interaction') || argv.includes('-n')) { return argv.filter((token) => token !== '--no-interaction' && token !== '-n') } if (!process.stdout.isTTY || !process.stdin.isTTY) return argv const definition = parseSignature(command.signature) const missing = missingArguments(definition, argv) if (missing.length === 0) return argv const answers: string[] = [] const labels = (command as { prompts?: Record }).prompts ?? {} for (const argument of missing) { // The argument's own description makes the better question when there is // one — it is the sentence the author already wrote about it. const question = labels[argument.name] ?? (argument.description ? `${argument.description}?` : `What is the ${argument.name}?`) answers.push(await this.output.ask(question)) } return [...argv, ...answers] } private renderList(): void { this.output.line() this.output.line( `${pc.bold(this.app.config.get('app.name', 'Elvel'))} ${pc.dim( `(${this.app.environment()})` )}` ) this.output.line() this.output.line(pc.bold('Usage:')) this.output.line(' bun elvel [options]') this.output.line() const groups = new Map() for (const command of this.all()) { const name = parseSignature(command.signature).name const group = name.includes(':') ? (name.split(':')[0] as string) : '' const bucket = groups.get(group) ?? [] bucket.push(command) groups.set(group, bucket) } const sorted = [...groups.entries()].sort(([left], [right]) => left.localeCompare(right)) const width = Math.max( ...this.all().map((command) => parseSignature(command.signature).name.length) ) for (const [group, commands] of sorted) { this.output.line(pc.bold(group === '' ? 'Available commands:' : ` ${group}`)) for (const command of commands.sort((left, right) => left.signature.localeCompare(right.signature) )) { const name = parseSignature(command.signature).name this.output.line(` ${pc.green(name.padEnd(width))} ${pc.dim(command.description)}`) } this.output.line() } } private renderHelp(command: CommandConstructor): void { const definition = parseSignature(command.signature) this.output.line() this.output.line(pc.bold('Description:')) this.output.line(` ${command.description}`) this.output.line() this.output.line(pc.bold('Usage:')) this.output.line(` bun elvel ${formatUsage(definition)}`) if (definition.arguments.length > 0) { this.output.line() this.output.line(pc.bold('Arguments:')) const width = Math.max(...definition.arguments.map((argument) => argument.name.length)) for (const argument of definition.arguments) { const suffix = argument.default === undefined ? '' : pc.dim(` [default: ${argument.default}]`) this.output.line( ` ${pc.green(argument.name.padEnd(width))} ${argument.description}${suffix}` ) } } if (definition.options.length > 0) { this.output.line() this.output.line(pc.bold('Options:')) const labels = definition.options.map((option) => { const shortcut = option.shortcut ? `-${option.shortcut}, ` : '' const value = option.acceptsValue ? '=VALUE' : '' return `${shortcut}--${option.name}${value}` }) const width = Math.max(...labels.map((label) => label.length)) definition.options.forEach((option, index) => { this.output.line( ` ${pc.green((labels[index] as string).padEnd(width))} ${option.description}` ) }) } this.output.line() } private suggest(name: string): void { const candidates = this.all() .map((command) => parseSignature(command.signature).name) .filter( (candidate) => candidate.includes(name) || name.includes(candidate.split(':')[0] ?? '') ) if (candidates.length === 0) return this.output.line() this.output.comment('Did you mean one of these?') for (const candidate of candidates.slice(0, 5)) this.output.line(` ${pc.green(candidate)}`) } private looksLikeCommand(value: unknown): value is CommandConstructor { if (typeof value !== 'function') return false const candidate = value as unknown as { signature?: unknown prototype?: { handle?: unknown } } return ( typeof candidate.signature === 'string' && candidate.signature !== '' && typeof candidate.prototype?.handle === 'function' ) } }