Start CLI and db schema queries
This commit is contained in:
@@ -2,6 +2,7 @@ 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'
|
||||
import {parseArgs} from '../../lib/src/external/std.ts'
|
||||
|
||||
@Unit()
|
||||
export default class CLIAppUnit extends LifecycleUnit {
|
||||
@@ -13,6 +14,24 @@ export default class CLIAppUnit extends LifecycleUnit {
|
||||
public async up() {
|
||||
this.logger.verbose(`Handling CLI invocation...`)
|
||||
const args = Deno.args
|
||||
console.log('args', {args})
|
||||
const parsed = parseArgs(args)
|
||||
console.log('args', {args, parsed})
|
||||
|
||||
const keyword = parsed._[0] || ''
|
||||
const directive = this.cli.get_directive_by_keyword(String(keyword))
|
||||
|
||||
this.logger.verbose(`Parsed directive: "${keyword}"`)
|
||||
|
||||
if ( !directive ) {
|
||||
const help = this.cli.get_directive_by_keyword('help')
|
||||
if ( !help ) throw new Error('Usage directive not registered!')
|
||||
|
||||
if ( keyword ) this.logger.error(`Invalid directive keyword: ${keyword}`)
|
||||
await help.prepare(args, parsed)
|
||||
await help.invoke()
|
||||
} else {
|
||||
await directive.prepare(args, parsed)
|
||||
await directive.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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'
|
||||
import {AboutDirective} from './directive/AboutDirective.ts'
|
||||
|
||||
@Unit()
|
||||
export default class CLIUnit extends LifecycleUnit {
|
||||
@@ -11,5 +12,6 @@ export default class CLIUnit extends LifecycleUnit {
|
||||
|
||||
public async up() {
|
||||
this.cli.register_directive(this.make(UsageDirective))
|
||||
this.cli.register_directive(this.make(AboutDirective))
|
||||
}
|
||||
}
|
||||
|
||||
30
cli/src/directive/AboutDirective.ts
Normal file
30
cli/src/directive/AboutDirective.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import {Directive} from './Directive.ts'
|
||||
import {Injectable} from '../../../di/src/decorator/Injection.ts'
|
||||
import {CLIService} from '../service/CLI.service.ts'
|
||||
|
||||
@Injectable()
|
||||
export class AboutDirective extends Directive {
|
||||
public readonly keyword = 'about'
|
||||
public readonly help = 'Display information about Daton'
|
||||
|
||||
constructor(
|
||||
protected readonly cli: CLIService,
|
||||
) { super() }
|
||||
|
||||
public async invoke() {
|
||||
if ( this.cli.show_logo() ) {
|
||||
console.log('')
|
||||
console.log(this.cli.get_logo())
|
||||
}
|
||||
|
||||
[
|
||||
'',
|
||||
'Daton is an opinionated application framework written for Deno. It provides a rich library of utilities, an ORM, dependency injector, routing stack, and logic for controllers, models, middleware, and configuration.',
|
||||
'',
|
||||
`Daton was created by and is © ${(new Date).getFullYear()} Garrett Mills. It is licensed for use by the terms of the MIT license.`,
|
||||
'',
|
||||
'Source code: https://code.garrettmills.dev/garrettmills/daton',
|
||||
'',
|
||||
].map(x => console.log(x))
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,25 @@
|
||||
import AppClass from '../../../lib/src/lifecycle/AppClass.ts'
|
||||
import {Logging} from '../../../lib/src/service/logging/Logging.ts'
|
||||
import {parseArgs} from '../../../lib/src/external/std.ts'
|
||||
|
||||
export type ParsedArguments = { [key: string]: string | number | Array<string | number> }
|
||||
|
||||
export abstract class Directive extends AppClass {
|
||||
public abstract readonly keyword: string
|
||||
public abstract readonly help: string
|
||||
|
||||
protected argv: string[] = []
|
||||
protected parsed_argv: ParsedArguments = {}
|
||||
|
||||
static options() {
|
||||
return []
|
||||
}
|
||||
|
||||
public prepare(argv: string[], parsed_arguments: ParsedArguments) {
|
||||
this.argv = argv
|
||||
this.parsed_argv = parsed_arguments
|
||||
}
|
||||
|
||||
public abstract invoke(): any
|
||||
|
||||
success(message: any) {
|
||||
|
||||
@@ -1,10 +1,47 @@
|
||||
import {Directive} from './Directive.ts'
|
||||
import {CLIService} from "../service/CLI.service.ts";
|
||||
import {Injectable} from "../../../di/src/decorator/Injection.ts";
|
||||
|
||||
@Injectable()
|
||||
export class UsageDirective extends Directive {
|
||||
public readonly keyword = 'help'
|
||||
public readonly help = 'Display usage information'
|
||||
|
||||
constructor(
|
||||
protected readonly cli: CLIService,
|
||||
) { super() }
|
||||
|
||||
public async invoke() {
|
||||
console.log('Hello, from Daton CLI.')
|
||||
console.log('')
|
||||
if ( this.cli.show_logo() ) {
|
||||
console.log(this.cli.get_logo())
|
||||
console.log('')
|
||||
}
|
||||
|
||||
console.log('Welcome to the Daton CLI. This tool can help you interact with your Daton application.\n')
|
||||
console.log('To get started, specify one of the directives below. You can always specify the --help option to get more information about a directive.')
|
||||
console.log('')
|
||||
|
||||
const usages = this.cli.get_directives().map((directive: Directive): string[] => {
|
||||
return [directive.keyword, directive.help]
|
||||
})
|
||||
|
||||
const pad_length = usages.max(grp => grp[0].length) + 2
|
||||
|
||||
const padded_usages = usages.map(grp => {
|
||||
const keyword = grp[0]
|
||||
if ( keyword.length < pad_length ) {
|
||||
const pad = Array(pad_length - keyword.length).fill(' ').join('')
|
||||
return [`${pad}${keyword}`, grp[1]]
|
||||
}
|
||||
|
||||
return grp
|
||||
})
|
||||
|
||||
padded_usages.each(grp => {
|
||||
console.log(`${grp[0]} : ${grp[1]}`)
|
||||
})
|
||||
|
||||
console.log('')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ export class CLIService extends AppClass {
|
||||
protected readonly logger: Logging,
|
||||
) { super() }
|
||||
|
||||
|
||||
/**
|
||||
* Find a registered directive using its keyword, if one exists.
|
||||
* @param {string} keyword
|
||||
@@ -57,4 +56,17 @@ export class CLIService extends AppClass {
|
||||
this.logger.verbose(`Registering CLI directive with keyword: ${directive.keyword}`)
|
||||
this.directives.push(directive)
|
||||
}
|
||||
|
||||
public show_logo() {
|
||||
return true
|
||||
}
|
||||
|
||||
public get_logo() {
|
||||
return `██████╗ █████╗ ████████╗ ██████╗ ███╗ ██╗
|
||||
██╔══██╗██╔══██╗╚══██╔══╝██╔═══██╗████╗ ██║
|
||||
██║ ██║███████║ ██║ ██║ ██║██╔██╗ ██║
|
||||
██║ ██║██╔══██║ ██║ ██║ ██║██║╚██╗██║
|
||||
██████╔╝██║ ██║ ██║ ╚██████╔╝██║ ╚████║
|
||||
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user