|
| 1 | +import { Application } from "../Application"; |
| 2 | +import type { Argument } from "commander"; |
| 3 | +import { ConsoleKernel } from "./ConsoleKernel"; |
| 4 | +import { XGeneric } from "@h3ravel/support"; |
| 5 | + |
| 6 | +export class ConsoleCommand { |
| 7 | + constructor(protected app: Application, protected kernel: ConsoleKernel) { } |
| 8 | + |
| 9 | + /** |
| 10 | + * The name and signature of the console command. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected signature!: string; |
| 15 | + |
| 16 | + /** |
| 17 | + * A dictionary of signatures or what not. |
| 18 | + * |
| 19 | + * @var object |
| 20 | + */ |
| 21 | + protected dictionary: Record<string, any> = {} |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected description?: string; |
| 29 | + |
| 30 | + /** |
| 31 | + * The console command input. |
| 32 | + * |
| 33 | + * @var object |
| 34 | + */ |
| 35 | + private input: XGeneric<{ options: Record<string, any>, arguments: Record<string, any> }> = { |
| 36 | + options: {}, |
| 37 | + arguments: {}, |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Execute the console command. |
| 42 | + */ |
| 43 | + public async handle (..._args: any[]): Promise<void> { } |
| 44 | + |
| 45 | + setApplication (app: Application) { |
| 46 | + this.app = app |
| 47 | + } |
| 48 | + |
| 49 | + setInput (options: XGeneric, args: string[], regArgs: readonly Argument[], dictionary: Record<string, any>) { |
| 50 | + this.dictionary = dictionary |
| 51 | + this.input.options = options |
| 52 | + this.input.arguments = regArgs |
| 53 | + .map((e, i) => ({ [e.name()]: args[i] })) |
| 54 | + .reduce((e, x) => Object.assign(e, x), {}) |
| 55 | + } |
| 56 | + |
| 57 | + getSignature () { |
| 58 | + return this.signature |
| 59 | + } |
| 60 | + |
| 61 | + getDescription () { |
| 62 | + return this.description |
| 63 | + } |
| 64 | + |
| 65 | + option (key: string, def?: any) { |
| 66 | + return this.input.options[key] ?? def |
| 67 | + } |
| 68 | + |
| 69 | + options (key?: string) { |
| 70 | + if (key) { |
| 71 | + return this.input.options[key] |
| 72 | + } |
| 73 | + return this.input.options |
| 74 | + } |
| 75 | + |
| 76 | + argument (key: string, def?: any) { |
| 77 | + return this.input.arguments[key] ?? def |
| 78 | + } |
| 79 | + |
| 80 | + arguments () { |
| 81 | + return this.input.arguments |
| 82 | + } |
| 83 | +} |
0 commit comments