Import other modules into monorepo
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-06-01 20:59:40 -05:00
parent 26d54033af
commit 9be9c44a32
138 changed files with 11544 additions and 139 deletions

View File

@@ -0,0 +1,124 @@
import {Singleton, Instantiable, Inject} from "../../di"
import {Collection} from "../../util"
import {CommandLineApplication} from "./CommandLineApplication"
import {Directive} from "../Directive"
import {Template} from "../Template"
import {directive_template} from "../templates/directive"
import {unit_template} from "../templates/unit";
import {controller_template} from "../templates/controller";
import {middleware_template} from "../templates/middleware";
import {routes_template} from "../templates/routes";
import {config_template} 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() {
this.registerTemplate(directive_template)
this.registerTemplate(unit_template)
this.registerTemplate(controller_template)
this.registerTemplate(middleware_template)
this.registerTemplate(routes_template)
this.registerTemplate(config_template)
}
/**
* Returns true if the application was started from the command line.
*/
public isCLI() {
return this.app().hasUnit(CommandLineApplication)
}
/**
* Returns a string containing the Extollo ASCII logo.
*/
public getASCIILogo() {
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>) {
if ( !this.directives.includes(directiveClass) ) {
this.directives.push(directiveClass)
}
}
/**
* Returns true if the given directive is registered with this service.
* @param directiveClass
*/
public hasDirective(directiveClass: Instantiable<Directive>) {
return this.directives.includes(directiveClass)
}
/**
* Get a collection of all registered directives.
*/
public getDirectives() {
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) {
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}`)
}
}
/**
* Returns true if a template with the given name exists.
* @param name
*/
public hasTemplate(name: string) {
return !!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() {
return this.templates.clone()
}
}

View File

@@ -0,0 +1,56 @@
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";
/**
* 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)
}
}
}

2
src/cli/service/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './CommandLine'
export * from './CommandLineApplication'