Standard libraries that lift up your code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
4.1 KiB

import {Singleton, Instantiable, Inject} from '../../di'
import {Collection} from '../../util'
import {CommandLineApplication} from './CommandLineApplication'
import {Directive} from '../Directive'
import {Template} from '../Template'
import {templateDirective} from '../templates/directive'
import {templateUnit} from '../templates/unit'
import {templateController} from '../templates/controller'
import {templateMiddleware} from '../templates/middleware'
import {templateRoutes} from '../templates/routes'
import {templateConfig} from '../templates/config'
import {Unit} from '../../lifecycle/Unit'
import {Logging} from '../../service/Logging'
/**
* Service for managing directives, templates, and other resources related
* to the command line utilities.
*/
@Singleton()
export class CommandLine extends Unit {
@Inject()
protected readonly logging!: Logging
/** Directive classes registered with the CLI command. */
protected directives: Collection<Instantiable<Directive>> = new Collection<Instantiable<Directive>>()
/** Templates registered with the CLI command. These can be created with the TemplateDirective. */
protected templates: Collection<Template> = new Collection<Template>()
constructor() {
super()
}
async up(): Promise<void> {
this.registerTemplate(templateDirective)
this.registerTemplate(templateUnit)
this.registerTemplate(templateController)
this.registerTemplate(templateMiddleware)
this.registerTemplate(templateRoutes)
this.registerTemplate(templateConfig)
}
/**
* Returns true if the application was started from the command line.
*/
public isCLI(): boolean {
return this.app().hasUnit(CommandLineApplication)
}
/**
* Returns a string containing the Extollo ASCII logo.
*/
public getASCIILogo(): string {
return ` _
/ /\\ ______ _ _ _
/ / \\ | ____| | | | | |
/ / /\\ \\ | |__ __ _| |_ ___ | | | ___
/ / /\\ \\ \\ | __| \\ \\/ / __/ _ \\| | |/ _ \\
/ / / \\ \\_\\ | |____ > <| || (_) | | | (_) |
\\/_/ \\/_/ |______/_/\\_\\\\__\\___/|_|_|\\___/
`
}
/**
* Register a Directive class with this service. This will make
* the directive available for use on the CLI.
* @param directiveClass
*/
public registerDirective(directiveClass: Instantiable<Directive>): this {
if ( !this.directives.includes(directiveClass) ) {
this.directives.push(directiveClass)
}
return this
}
/**
* Returns true if the given directive is registered with this service.
* @param directiveClass
*/
public hasDirective(directiveClass: Instantiable<Directive>): boolean {
return this.directives.includes(directiveClass)
}
/**
* Get a collection of all registered directives.
*/
public getDirectives(): Collection<Instantiable<Directive>> {
return this.directives.clone()
}
/**
* Register the given template with this service. This makes the template
* available for use with the TemplateDirective service.
* @param template
*/
public registerTemplate(template: Template): this {
if ( !this.templates.firstWhere('name', '=', template.name) ) {
this.templates.push(template)
} else {
this.logging.warn(`Duplicate template will not be registered: ${template.name}`)
this.logging.debug(`Duplicate template registered at: ${(new Error()).stack}`)
}
return this
}
/**
* Returns true if a template with the given name exists.
* @param name
*/
public hasTemplate(name: string): boolean {
return Boolean(this.templates.firstWhere('name', '=', name))
}
/**
* Returns the template with the given name, if one exists.
* @param name
*/
public getTemplate(name: string): Template | undefined {
return this.templates.firstWhere('name', '=', name)
}
/**
* Get a collection of all registered templates.
*/
public getTemplates(): Collection<Template> {
return this.templates.clone()
}
}