|
|
|
import {Unit, Logging} from "@extollo/lib"
|
|
|
|
import {Singleton, Inject} from "@extollo/di"
|
|
|
|
import {CommandLine} from "./CommandLine"
|
|
|
|
import {UsageDirective} from "../directive/UsageDirective";
|
|
|
|
import {Directive} from "../Directive";
|
|
|
|
import {ShellDirective} from "../directive/ShellDirective";
|
|
|
|
import {TemplateDirective} from "../directive/TemplateDirective";
|
|
|
|
import {RunDirective} from "../directive/RunDirective";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit that takes the place of the final unit in the application that handles
|
|
|
|
* invocations from the command line.
|
|
|
|
*/
|
|
|
|
@Singleton()
|
|
|
|
export class CommandLineApplication extends Unit {
|
|
|
|
/** The unit that was replaced by the CLI app. */
|
|
|
|
private static replacement?: typeof Unit
|
|
|
|
|
|
|
|
/** Set the replaced unit. */
|
|
|
|
public static setReplacement(unitClass?: typeof Unit) {
|
|
|
|
this.replacement = unitClass
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the replaced unit. */
|
|
|
|
public static getReplacement() {
|
|
|
|
return this.replacement
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly cli!: CommandLine
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
|
|
|
constructor() { super() }
|
|
|
|
|
|
|
|
public async up() {
|
|
|
|
this.cli.registerDirective(UsageDirective)
|
|
|
|
this.cli.registerDirective(ShellDirective)
|
|
|
|
this.cli.registerDirective(TemplateDirective)
|
|
|
|
this.cli.registerDirective(RunDirective)
|
|
|
|
|
|
|
|
const argv = process.argv.slice(2)
|
|
|
|
const match = this.cli.getDirectives()
|
|
|
|
.map(dirCls => this.make<Directive>(dirCls))
|
|
|
|
.firstWhere(dir => dir.matchesKeyword(argv[0]))
|
|
|
|
|
|
|
|
if ( match ) {
|
|
|
|
await match.invoke(argv.slice(1))
|
|
|
|
} else {
|
|
|
|
const usage = this.make<UsageDirective>(UsageDirective)
|
|
|
|
await usage.handle(process.argv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|