2021-06-03 03:36:25 +00:00
|
|
|
import {Unit} from '../../lifecycle/Unit'
|
|
|
|
import {Logging} from '../../service/Logging'
|
|
|
|
import {Singleton, Inject} from '../../di/decorator/injection'
|
|
|
|
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'
|
2021-06-18 00:35:31 +00:00
|
|
|
import {RoutesDirective} from '../directive/RoutesDirective'
|
|
|
|
import {RouteDirective} from '../directive/RouteDirective'
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public static setReplacement(unitClass?: typeof Unit): void {
|
2021-06-02 01:59:40 +00:00
|
|
|
this.replacement = unitClass
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the replaced unit. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public static getReplacement(): typeof Unit | undefined {
|
2021-06-02 01:59:40 +00:00
|
|
|
return this.replacement
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly cli!: CommandLine
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
constructor() {
|
|
|
|
super()
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public async up(): Promise<void> {
|
2021-06-02 01:59:40 +00:00
|
|
|
this.cli.registerDirective(UsageDirective)
|
|
|
|
this.cli.registerDirective(ShellDirective)
|
|
|
|
this.cli.registerDirective(TemplateDirective)
|
|
|
|
this.cli.registerDirective(RunDirective)
|
2021-06-18 00:35:31 +00:00
|
|
|
this.cli.registerDirective(RoutesDirective)
|
|
|
|
this.cli.registerDirective(RouteDirective)
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
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)
|
2021-06-03 03:36:25 +00:00
|
|
|
await usage.handle()
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|