Add common templates

This commit is contained in:
Garrett Mills 2021-03-21 20:15:09 -05:00
parent cae260b9ce
commit d1013f1a00
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
7 changed files with 146 additions and 2 deletions

View File

@ -5,6 +5,11 @@ import {CommandLineApplication} from "./CommandLineApplication"
import {Directive} from "../Directive" import {Directive} from "../Directive"
import {Template} from "../Template" import {Template} from "../Template"
import {directive_template} from "../templates/directive" 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";
@Singleton() @Singleton()
export class CommandLine extends Unit { export class CommandLine extends Unit {
@ -18,6 +23,11 @@ export class CommandLine extends Unit {
async up() { async up() {
this.registerTemplate(directive_template) this.registerTemplate(directive_template)
this.registerTemplate(unit_template)
this.registerTemplate(controller_template)
this.registerTemplate(middleware_template)
this.registerTemplate(routes_template)
this.registerTemplate(config_template)
} }
public isCLI() { public isCLI() {

19
src/templates/config.ts Normal file
View File

@ -0,0 +1,19 @@
import {Template} from "../Template"
import {UniversalPath} from "@extollo/util"
const config_template: Template = {
name: 'config',
fileSuffix: '.config.ts',
description: 'Create a new config file.',
baseAppPath: ['configs'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import { env } from '@extollo/lib'
export default {
key: env('VALUE_ENV_VAR', 'default value'),
}
`
}
}
export { config_template }

View File

@ -0,0 +1,28 @@
import {Template} from "../Template"
import {UniversalPath} from "@extollo/util"
const controller_template: Template = {
name: 'controller',
fileSuffix: '.controller.ts',
description: 'Create a controller class that can be used to handle requests.',
baseAppPath: ['http', 'controllers'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Controller, view} from "@extollo/lib"
import {Inject, Injectable} from "@extollo/di"
/**
* ${name} Controller
* ------------------------------------
* Put some description here.
*/
@Injectable()
export class ${name} extends Controller {
public ${name.toLowerCase()}() {
return view('${name.toLowerCase()}')
}
}
`
}
}
export { controller_template }

View File

@ -0,0 +1,28 @@
import {Template} from "../Template"
import {UniversalPath} from "@extollo/util"
const middleware_template: Template = {
name: 'middleware',
fileSuffix: '.middleware.ts',
description: 'Create a middleware class that can be applied to routes.',
baseAppPath: ['http', 'middlewares'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Middleware} from "@extollo/lib"
import {Injectable} from "@extollo/di"
/**
* ${name} Middleware
* --------------------------------------------
* Put some description here.
*/
@Injectable()
export class ${name} extends Middleware {
public async apply() {
}
}
`
}
}
export { middleware_template }

23
src/templates/routes.ts Normal file
View File

@ -0,0 +1,23 @@
import {Template} from "../Template"
import {UniversalPath} from "@extollo/util"
const routes_template: Template = {
name: 'routes',
fileSuffix: '.routes.ts',
description: 'Create a file for route definitions.',
baseAppPath: ['http', 'routes'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Route} from "@extollo/lib"
/*
* ${name} Routes
* -------------------------------
* Put some description here.
*/
`
}
}
export { routes_template }

35
src/templates/unit.ts Normal file
View File

@ -0,0 +1,35 @@
import {Template} from "../Template"
import {UniversalPath} from "@extollo/util"
const unit_template: Template = {
name: 'unit',
fileSuffix: '.ts',
description: 'Create a service unit that will start and stop with your application.',
baseAppPath: ['units'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Singleton, Inject} from "@extollo/di"
import {Unit, Logging} from "@extollo/lib"
/**
* ${name} Unit
* ---------------------------------------
* Put some description here.
*/
@Singleton()
export class ${name} extends Unit {
@Inject()
protected readonly logging!: Logging
public async up() {
this.logging.info('${name} has started!')
}
public async down() {
this.logging.info('${name} has stopped!')
}
}
`
}
}
export { unit_template }

View File

@ -1,8 +1,9 @@
`{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es5", "target": "es5",
"sourceMap": true "sourceMap": true,
"experimentalDecorators": true
}, },
"exclude": [ "exclude": [
"node_modules" "node_modules"