Start basic CLI framework
This commit is contained in:
18
cli/src/CLIAppUnit.ts
Normal file
18
cli/src/CLIAppUnit.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import LifecycleUnit from '../../lib/src/lifecycle/Unit.ts'
|
||||
import {CLIService} from './service/CLI.service.ts'
|
||||
import {Unit} from '../../lib/src/lifecycle/decorators.ts'
|
||||
import {Logging} from '../../lib/src/service/logging/Logging.ts'
|
||||
|
||||
@Unit()
|
||||
export default class CLIAppUnit extends LifecycleUnit {
|
||||
constructor(
|
||||
protected readonly cli: CLIService,
|
||||
protected readonly logger: Logging,
|
||||
) { super() }
|
||||
|
||||
public async up() {
|
||||
this.logger.verbose(`Handling CLI invocation...`)
|
||||
const args = Deno.args
|
||||
console.log('args', {args})
|
||||
}
|
||||
}
|
||||
15
cli/src/CLIUnit.ts
Normal file
15
cli/src/CLIUnit.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import LifecycleUnit from '../../lib/src/lifecycle/Unit.ts'
|
||||
import {CLIService} from './service/CLI.service.ts'
|
||||
import {Unit} from '../../lib/src/lifecycle/decorators.ts'
|
||||
import {UsageDirective} from './directive/UsageDirective.ts'
|
||||
|
||||
@Unit()
|
||||
export default class CLIUnit extends LifecycleUnit {
|
||||
constructor(
|
||||
protected readonly cli: CLIService,
|
||||
) { super() }
|
||||
|
||||
public async up() {
|
||||
this.cli.register_directive(this.make(UsageDirective))
|
||||
}
|
||||
}
|
||||
37
cli/src/directive/Directive.ts
Normal file
37
cli/src/directive/Directive.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import AppClass from '../../../lib/src/lifecycle/AppClass.ts'
|
||||
import {Logging} from '../../../lib/src/service/logging/Logging.ts'
|
||||
|
||||
export abstract class Directive extends AppClass {
|
||||
public abstract readonly keyword: string
|
||||
public abstract readonly help: string
|
||||
|
||||
static options() {
|
||||
return []
|
||||
}
|
||||
|
||||
public abstract invoke(): any
|
||||
|
||||
success(message: any) {
|
||||
this.make(Logging).success(message, true)
|
||||
}
|
||||
|
||||
error(message: any) {
|
||||
this.make(Logging).error(message, true)
|
||||
}
|
||||
|
||||
warn(message: any) {
|
||||
this.make(Logging).warn(message, true)
|
||||
}
|
||||
|
||||
info(message: any) {
|
||||
this.make(Logging).info(message, true)
|
||||
}
|
||||
|
||||
debug(message: any) {
|
||||
this.make(Logging).debug(message, true)
|
||||
}
|
||||
|
||||
verbose(message: any) {
|
||||
this.make(Logging).verbose(message, true)
|
||||
}
|
||||
}
|
||||
10
cli/src/directive/UsageDirective.ts
Normal file
10
cli/src/directive/UsageDirective.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import {Directive} from './Directive.ts'
|
||||
|
||||
export class UsageDirective extends Directive {
|
||||
public readonly keyword = 'help'
|
||||
public readonly help = 'Display usage information'
|
||||
|
||||
public async invoke() {
|
||||
console.log('Hello, from Daton CLI.')
|
||||
}
|
||||
}
|
||||
0
cli/src/mod.ts
Normal file
0
cli/src/mod.ts
Normal file
60
cli/src/service/CLI.service.ts
Normal file
60
cli/src/service/CLI.service.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import AppClass from '../../../lib/src/lifecycle/AppClass.ts'
|
||||
import {Service} from '../../../di/src/decorator/Service.ts'
|
||||
import {Collection} from '../../../lib/src/collection/Collection.ts'
|
||||
import {Directive} from '../directive/Directive.ts'
|
||||
import {Logging} from '../../../lib/src/service/logging/Logging.ts'
|
||||
|
||||
/**
|
||||
* Error thrown when a directive registered with the same keyword as
|
||||
* an existing directive.
|
||||
* @extends Error
|
||||
*/
|
||||
export class DuplicateCLIDirectiveError extends Error {
|
||||
constructor(keyword: string) {
|
||||
super(`A CLI directive with the keyword "${keyword}" already exists.`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Service for registering and managing CLI directives.
|
||||
* @extends AppClass
|
||||
*/
|
||||
@Service()
|
||||
export class CLIService extends AppClass {
|
||||
protected directives: Collection<Directive> = new Collection<Directive>()
|
||||
|
||||
constructor(
|
||||
protected readonly logger: Logging,
|
||||
) { super() }
|
||||
|
||||
|
||||
/**
|
||||
* Find a registered directive using its keyword, if one exists.
|
||||
* @param {string} keyword
|
||||
* @return Directive | undefined
|
||||
*/
|
||||
public get_directive_by_keyword(keyword: string): Directive | undefined {
|
||||
return this.directives.firstWhere('keyword', '=', keyword)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of all registered directives.
|
||||
* @return Collection<Directive>
|
||||
*/
|
||||
public get_directives() {
|
||||
return this.directives.clone()
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a directive with the service.
|
||||
* @param {Directive} directive
|
||||
*/
|
||||
public register_directive(directive: Directive) {
|
||||
if ( this.get_directive_by_keyword(directive.keyword) ) {
|
||||
throw new DuplicateCLIDirectiveError(directive.keyword)
|
||||
}
|
||||
|
||||
this.logger.verbose(`Registering CLI directive with keyword: ${directive.keyword}`)
|
||||
this.directives.push(directive)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user