Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Garrett Mills 2021-06-02 22:36:25 -05:00
parent 82e7a1f299
commit 1d5056b753
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
149 changed files with 6104 additions and 3114 deletions

3
.eslintignore Normal file
View File

@ -0,0 +1,3 @@
node_modules
lib
dist

113
.eslintrc.json Normal file
View File

@ -0,0 +1,113 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single",
{
"allowTemplateLiterals": true
}
],
"semi": [
"error",
"never"
],
"no-console": "error",
"curly": "error",
"eqeqeq": "error",
"guard-for-in": "error",
"no-alert": "error",
"no-caller": "error",
"no-constructor-return": "error",
"no-eval": "error",
"no-implicit-coercion": "error",
"no-implied-eval": "error",
"no-invalid-this": "error",
"no-return-await": "error",
"no-throw-literal": "error",
"no-useless-call": "error",
"radix": "error",
"yoda": "error",
"@typescript-eslint/no-shadow": "error",
"brace-style": "error",
"camelcase": "error",
"comma-dangle": [
"error",
"always-multiline"
],
"comma-spacing": [
"error",
{
"before": false,
"after": true
}
],
"comma-style": [
"error",
"last"
],
"computed-property-spacing": [
"error",
"never"
],
"eol-last": "error",
"func-call-spacing": [
"error",
"never"
],
"keyword-spacing": [
"error",
{
"before": true,
"after": true
}
],
"lines-between-class-members": "error",
"max-params": [
"error",
4
],
"new-parens": [
"error",
"always"
],
"newline-per-chained-call": "error",
"no-trailing-spaces": "error",
"no-underscore-dangle": "error",
"no-unneeded-ternary": "error",
"no-whitespace-before-property": "error",
"object-property-newline": "error",
"prefer-exponentiation-operator": "error",
"prefer-object-spread": "error",
"spaced-comment": [
"error",
"always"
],
"prefer-const": "error",
"@typescript-eslint/no-explicit-any": "off"
}
}

1338
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -38,10 +38,13 @@
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prebuild": "pnpm run lint",
"build": "tsc",
"app": "tsc && node lib/index.js",
"prepare": "pnpm run build",
"docs:build": "typedoc --options typedoc.json"
"docs:build": "typedoc --options typedoc.json",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint --fix . --ext .ts"
},
"files": [
"lib/**/*"
@ -53,5 +56,10 @@
"url": "https://code.garrettmills.dev/extollo/lib"
},
"author": "garrettmills <shout@garrettmills.dev>",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.27.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,10 @@
import {Injectable, Inject} from "../di"
import {infer, ErrorWithContext} from "../util"
import {CLIOption} from "./directive/options/CLIOption"
import {PositionalOption} from "./directive/options/PositionalOption";
import {FlagOption} from "./directive/options/FlagOption";
import {AppClass} from "../lifecycle/AppClass";
import {Logging} from "../service/Logging";
import {Injectable, Inject} from '../di'
import {infer, ErrorWithContext} from '../util'
import {CLIOption} from './directive/options/CLIOption'
import {PositionalOption} from './directive/options/PositionalOption'
import {FlagOption} from './directive/options/FlagOption'
import {AppClass} from '../lifecycle/AppClass'
import {Logging} from '../service/Logging'
/**
* Type alias for a definition of a command-line option.
@ -35,7 +35,7 @@ export abstract class Directive extends AppClass {
protected readonly logging!: Logging
/** Parsed option values. */
private _optionValues: any
private optionValues: any
/**
* Get the keyword or array of keywords that will specify this directive.
@ -84,8 +84,8 @@ export abstract class Directive extends AppClass {
* @param optionValues
* @private
*/
private _setOptionValues(optionValues: any) {
this._optionValues = optionValues;
private setOptionValues(optionValues: any) {
this.optionValues = optionValues
}
/**
@ -93,9 +93,9 @@ export abstract class Directive extends AppClass {
* @param name
* @param defaultValue
*/
public option(name: string, defaultValue?: any) {
if ( name in this._optionValues ) {
return this._optionValues[name]
public option(name: string, defaultValue?: unknown): any {
if ( name in this.optionValues ) {
return this.optionValues[name]
}
return defaultValue
@ -110,20 +110,28 @@ export abstract class Directive extends AppClass {
*
* @param argv
*/
async invoke(argv: string[]) {
async invoke(argv: string[]): Promise<void> {
const options = this.getResolvedOptions()
if ( this.didRequestUsage(argv) ) {
// @ts-ignore
const positionalArguments: PositionalOption<any>[] = options.filter(opt => opt instanceof PositionalOption)
const positionalArguments: PositionalOption<any>[] = []
options.forEach(opt => {
if ( opt instanceof PositionalOption ) {
positionalArguments.push(opt)
}
})
// @ts-ignore
const flagArguments: FlagOption<any>[] = options.filter(opt => opt instanceof FlagOption)
const flagArguments: FlagOption<any>[] = []
options.forEach(opt => {
if ( opt instanceof FlagOption ) {
flagArguments.push(opt)
}
})
const positionalDisplay: string = positionalArguments.map(x => `<${x.getArgumentName()}>`).join(' ')
const flagDisplay: string = flagArguments.length ? ' [...flags]' : ''
console.log([
this.nativeOutput([
'',
`DIRECTIVE: ${this.getMainKeyword()} - ${this.getDescription()}`,
'',
@ -131,7 +139,7 @@ export abstract class Directive extends AppClass {
].join('\n'))
if ( positionalArguments.length ) {
console.log([
this.nativeOutput([
'',
`POSITIONAL ARGUMENTS:`,
...(positionalArguments.map(arg => {
@ -141,7 +149,7 @@ export abstract class Directive extends AppClass {
}
if ( flagArguments.length ) {
console.log([
this.nativeOutput([
'',
`FLAGS:`,
...(flagArguments.map(arg => {
@ -152,34 +160,34 @@ export abstract class Directive extends AppClass {
const help = this.getHelpText()
if ( help ) {
console.log('\n' + help)
this.nativeOutput('\n' + help)
}
console.log('\n')
this.nativeOutput('\n')
} else {
try {
const optionValues = this.parseOptions(options, argv)
this._setOptionValues(optionValues)
this.setOptionValues(optionValues)
await this.handle(argv)
} catch (e) {
console.error(e.message)
this.nativeOutput(e.message)
if ( e instanceof OptionValidationError ) {
// expecting, value, requirements
if ( e.context.expecting ) {
console.error(` - Expecting: ${e.context.expecting}`)
this.nativeOutput(` - Expecting: ${e.context.expecting}`)
}
if ( e.context.requirements && Array.isArray(e.context.requirements) ) {
for ( const req of e.context.requirements ) {
console.error(` - ${req}`)
this.nativeOutput(` - ${req}`)
}
}
if ( e.context.value ) {
console.error(` - ${e.context.value}`)
this.nativeOutput(` - ${e.context.value}`)
}
}
console.error('\nUse --help for more info.')
this.nativeOutput('\nUse --help for more info.')
}
}
}
@ -217,9 +225,11 @@ export abstract class Directive extends AppClass {
* Returns true if the given keyword should invoke this directive.
* @param name
*/
public matchesKeyword(name: string) {
public matchesKeyword(name: string): boolean {
let kws = this.getKeywords()
if ( !Array.isArray(kws) ) kws = [kws]
if ( !Array.isArray(kws) ) {
kws = [kws]
}
return kws.includes(name)
}
@ -227,7 +237,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as success text.
* @param output
*/
success(output: any) {
success(output: unknown): void {
this.logging.success(output, true)
}
@ -235,7 +245,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as error text.
* @param output
*/
error(output: any) {
error(output: unknown): void {
this.logging.error(output, true)
}
@ -243,7 +253,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as warning text.
* @param output
*/
warn(output: any) {
warn(output: unknown): void {
this.logging.warn(output, true)
}
@ -251,7 +261,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as info text.
* @param output
*/
info(output: any) {
info(output: unknown): void {
this.logging.info(output, true)
}
@ -259,7 +269,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as debugging text.
* @param output
*/
debug(output: any) {
debug(output: unknown): void {
this.logging.debug(output, true)
}
@ -267,7 +277,7 @@ export abstract class Directive extends AppClass {
* Print the given output to the log as verbose text.
* @param output
*/
verbose(output: any) {
verbose(output: unknown): void {
this.logging.verbose(output, true)
}
@ -275,7 +285,7 @@ export abstract class Directive extends AppClass {
* Get the flag option that signals help. Usually, this is named 'help'
* and supports the flags '--help' and '-?'.
*/
getHelpOption() {
getHelpOption(): FlagOption<any> {
return new FlagOption('--help', '-?', 'usage information about this directive')
}
@ -283,12 +293,21 @@ export abstract class Directive extends AppClass {
* Process the raw CLI arguments using an array of option class instances to build
* a mapping of option names to provided values.
*/
parseOptions(options: CLIOption<any>[], args: string[]) {
// @ts-ignore
let positionalArguments: PositionalOption<any>[] = options.filter(cls => cls instanceof PositionalOption)
parseOptions(options: CLIOption<any>[], args: string[]): {[key: string]: any} {
let positionalArguments: PositionalOption<any>[] = []
options.forEach(opt => {
if ( opt instanceof PositionalOption ) {
positionalArguments.push(opt)
}
})
const flagArguments: FlagOption<any>[] = []
options.forEach(opt => {
if ( opt instanceof FlagOption ) {
flagArguments.push(opt)
}
})
// @ts-ignore
const flagArguments: FlagOption<any>[] = options.filter(cls => cls instanceof FlagOption)
const optionValue: any = {}
flagArguments.push(this.getHelpOption())
@ -325,7 +344,7 @@ export abstract class Directive extends AppClass {
const flagArgument = flagArguments.filter(x => x.shortFlag === value)
if ( flagArgument.length < 1 ) {
throw new OptionValidationError(`Unknown flag argument: ${value}`, {
value
value,
})
} else {
if ( flagArgument[0].argumentDescription ) {
@ -350,7 +369,7 @@ export abstract class Directive extends AppClass {
} else {
if ( positionalArguments.length < 1 ) {
throw new OptionValidationError(`Unknown positional argument: ${value}`, {
value
value,
})
} else {
const inferredValue = infer(value)
@ -368,13 +387,13 @@ export abstract class Directive extends AppClass {
if ( expectingFlagArgument ) {
throw new OptionValidationError(`Missing argument for flag: ${positionalFlagName}`, {
expecting: positionalFlagName
expecting: positionalFlagName,
})
}
if ( positionalArguments.length > 0 ) {
throw new OptionValidationError(`Missing required argument: ${positionalArguments[0].getArgumentName()}`, {
expecting: positionalArguments[0].getArgumentName()
expecting: positionalArguments[0].getArgumentName(),
})
}
@ -430,14 +449,18 @@ export abstract class Directive extends AppClass {
* Determines if, at any point in the arguments, the help option's short or long flag appears.
* @returns {boolean} - true if the help flag appeared
*/
didRequestUsage(argv: string[]) {
const help_option = this.getHelpOption()
didRequestUsage(argv: string[]): boolean {
const helpOption = this.getHelpOption()
for ( const arg of argv ) {
if ( arg.trim() === help_option.longFlag || arg.trim() === help_option.shortFlag ) {
if ( arg.trim() === helpOption.longFlag || arg.trim() === helpOption.shortFlag ) {
return true
}
}
return false
}
protected nativeOutput(...outputs: any[]): void {
console.log(...outputs) // eslint-disable-line no-console
}
}

View File

@ -1,8 +1,8 @@
import {Directive} from "../Directive"
import {CommandLineApplication} from "../service"
import {Injectable} from "../../di"
import {ErrorWithContext} from "../../util"
import {Unit} from "../../lifecycle/Unit";
import {Directive} from '../Directive'
import {CommandLineApplication} from '../service'
import {Injectable} from '../../di'
import {ErrorWithContext} from '../../util'
import {Unit} from '../../lifecycle/Unit'
/**
* A directive that starts the framework's final target normally.

View File

@ -1,7 +1,7 @@
import {Directive} from "../Directive"
import * as colors from "colors/safe"
import {Directive} from '../Directive'
import * as colors from 'colors/safe'
import * as repl from 'repl'
import {DependencyKey} from "../../di";
import {DependencyKey} from '../../di'
/**
* Launch an interactive REPL shell from within the application.
@ -9,7 +9,7 @@ import {DependencyKey} from "../../di";
*/
export class ShellDirective extends Directive {
protected options: any = {
welcome: `powered by Extollo, © ${(new Date).getFullYear()} Garrett Mills\nAccess your application using the "app" global.`,
welcome: `powered by Extollo, © ${(new Date()).getFullYear()} Garrett Mills\nAccess your application using the "app" global.`,
prompt: `${colors.blue('(')}extollo${colors.blue(') ➤ ')}`,
}
@ -38,7 +38,7 @@ export class ShellDirective extends Directive {
}
await new Promise<void>(res => {
console.log(this.options.welcome)
this.nativeOutput(this.options.welcome)
this.repl = repl.start(this.options.prompt)
Object.assign(this.repl.context, state)
this.repl.on('exit', () => res())

View File

@ -1,8 +1,8 @@
import {Directive, OptionDefinition} from "../Directive"
import {PositionalOption} from "./options/PositionalOption"
import {CommandLine} from "../service"
import {Inject, Injectable} from "../../di";
import {ErrorWithContext} from "../../util";
import {Directive, OptionDefinition} from '../Directive'
import {PositionalOption} from './options/PositionalOption'
import {CommandLine} from '../service'
import {Inject, Injectable} from '../../di'
import {ErrorWithContext} from '../../util'
/**
* Create a new file based on a template registered with the CommandLine service.
@ -46,11 +46,11 @@ export class TemplateDirective extends Directive {
'',
...(registeredTemplates.map(template => {
return ` - ${template.name}: ${template.description}`
}).all())
}).all()),
].join('\n')
}
async handle(argv: string[]) {
async handle(): Promise<void> {
const templateName: string = this.option('template_name')
const destinationName: string = this.option('file_name')

View File

@ -1,7 +1,7 @@
import {Directive} from "../Directive"
import {Injectable, Inject} from "../../di"
import {padRight} from "../../util"
import {CommandLine} from "../service"
import {Directive} from '../Directive'
import {Injectable, Inject} from '../../di'
import {padRight} from '../../util'
import {CommandLine} from '../service'
/**
* Directive that prints the help message and usage information about
@ -20,7 +20,7 @@ export class UsageDirective extends Directive {
return 'print information about available commands'
}
public handle(argv: string[]): void | Promise<void> {
public handle(): void | Promise<void> {
const directiveStrings = this.cli.getDirectives()
.map(cls => this.make<Directive>(cls))
.map<[string, string]>(dir => {
@ -30,15 +30,15 @@ export class UsageDirective extends Directive {
const maxLen = directiveStrings.max<number>(x => x[0].length)
const printStrings = directiveStrings.map(grp => {
return [padRight(grp[0], maxLen + 1), grp[1]]
})
return [padRight(grp[0], maxLen + 1), grp[1]]
})
.map(grp => {
return ` ${grp[0]}: ${grp[1]}`
})
.toArray()
console.log(this.cli.getASCIILogo())
console.log([
this.nativeOutput(this.cli.getASCIILogo())
this.nativeOutput([
'',
'Welcome to Extollo! Specify a command to get started.',
'',
@ -48,7 +48,7 @@ export class UsageDirective extends Directive {
'',
'For usage information about a particular command, pass the --help flag.',
'-------------------------------------------',
`powered by Extollo, © ${(new Date).getFullYear()} Garrett Mills`,
`powered by Extollo, © ${(new Date()).getFullYear()} Garrett Mills`,
].join('\n'))
}
}

View File

@ -9,200 +9,207 @@ export abstract class CLIOption<T> {
* @type {boolean}
* @private
*/
protected _useWhitelist: boolean = false
protected useWhitelist = false
/**
* Do we use the blacklist?
* @type {boolean}
* @private
*/
protected _useBlacklist: boolean = false
protected useBlacklist = false
/**
* Do we use the less-than comparison?
* @type {boolean}
* @private
*/
protected _useLessThan: boolean = false
protected useLessThan = false
/**
* Do we use the greater-than comparison?
* @type {boolean}
* @private
*/
protected _useGreaterThan: boolean = false
protected useGreaterThan = false
/**
* Do we use the equality operator?
* @type {boolean}
* @private
*/
protected _useEquality: boolean = false
protected useEquality = false
/**
* Is this option optional?
* @type {boolean}
* @private
*/
protected _optional: boolean = false
protected isOptional = false
/**
* Whitelisted values.
* @type {Array<*>}
* @private
*/
protected _whitelist: T[] = []
protected whitelistItems: T[] = []
/**
* Blacklisted values.
* @type {Array<*>}
* @private
*/
protected _blacklist: T[] = []
protected blacklistItems: T[] = []
/**
* Value to be compared in less than.
* @type {*}
* @private
*/
protected _lessThanValue?: T
protected lessThanValue?: T
/**
* If true, the less than will be less than or equal to.
* @type {boolean}
* @private
*/
protected _lessThanBit: boolean = false
protected lessThanBit = false
/**
* Value to be compared in greater than.
* @type {*}
* @private
*/
protected _greaterThanValue?: T
protected greaterThanValue?: T
/**
* If true, the greater than will be greater than or equal to.
* @type {boolean}
* @private
*/
protected _greateerThanBit: boolean = false
protected greaterThanBit = false
/**
* The value to be used to check equality.
* @type {*}
* @private
*/
protected _equalityValue?: T
protected equalityValue?: T
/**
* Whitelist the specified item or items and enable the whitelist.
* @param {...*} items - the items to whitelist
*/
whitelist(...items: T[]) {
this._useWhitelist = true
items.forEach(item => this._whitelist.push(item))
whitelist(...items: T[]): this {
this.useWhitelist = true
items.forEach(item => this.whitelistItems.push(item))
return this
}
/**
* Blacklist the specified item or items and enable the blacklist.
* @param {...*} items - the items to blacklist
*/
blacklist(...items: T[]) {
this._useBlacklist = true
items.forEach(item => this._blacklist.push(item))
blacklist(...items: T[]): this {
this.useBlacklist = true
items.forEach(item => this.blacklistItems.push(item))
return this
}
/**
* Specifies the value to be used in less-than comparison and enables less-than comparison.
* @param {*} value
*/
lessThan(value: T) {
this._useLessThan = true
this._lessThanValue = value
lessThan(value: T): this {
this.useLessThan = true
this.lessThanValue = value
return this
}
/**
* Specifies the value to be used in less-than or equal-to comparison and enables that comparison.
* @param {*} value
*/
lessThanOrEqualTo(value: T) {
this._lessThanBit = true
lessThanOrEqualTo(value: T): this {
this.lessThanBit = true
this.lessThan(value)
return this
}
/**
* Specifies the value to be used in greater-than comparison and enables that comparison.
* @param {*} value
*/
greaterThan(value: T) {
this._useGreaterThan = true
this._greaterThanValue = value
greaterThan(value: T): this {
this.useGreaterThan = true
this.greaterThanValue = value
return this
}
/**
* Specifies the value to be used in greater-than or equal-to comparison and enables that comparison.
* @param {*} value
*/
greaterThanOrEqualTo(value: T) {
this._greateerThanBit = true
greaterThanOrEqualTo(value: T): this {
this.greaterThanBit = true
this.greaterThan(value)
return this
}
/**
* Specifies the value to be used in equality comparison and enables that comparison.
* @param {*} value
*/
equals(value: T) {
this._useEquality = true
this._equalityValue = value
equals(value: T): this {
this.useEquality = true
this.equalityValue = value
return this
}
/**
* Checks if the specified value passes the configured comparisons.
* @param {*} value
* @param value
* @returns {boolean}
*/
validate(value: any) {
let is_valid = true
if ( this._useEquality ) {
is_valid = is_valid && (this._equalityValue === value)
validate(value: T): boolean {
let isValid = true
if ( this.useEquality ) {
isValid = isValid && (this.equalityValue === value)
}
if ( this._useLessThan && typeof this._lessThanValue !== 'undefined' ) {
if ( this._lessThanBit ) {
is_valid = is_valid && (value <= this._lessThanValue)
if ( this.useLessThan && typeof this.lessThanValue !== 'undefined' ) {
if ( this.lessThanBit ) {
isValid = isValid && (value <= this.lessThanValue)
} else {
is_valid = is_valid && (value < this._lessThanValue)
isValid = isValid && (value < this.lessThanValue)
}
}
if ( this._useGreaterThan && typeof this._greaterThanValue !== 'undefined' ) {
if ( this._greateerThanBit ) {
is_valid = is_valid && (value >= this._greaterThanValue)
if ( this.useGreaterThan && typeof this.greaterThanValue !== 'undefined' ) {
if ( this.greaterThanBit ) {
isValid = isValid && (value >= this.greaterThanValue)
} else {
is_valid = is_valid && (value > this._greaterThanValue)
isValid = isValid && (value > this.greaterThanValue)
}
}
if ( this._useWhitelist ) {
is_valid = is_valid && this._whitelist.some(x => {
if ( this.useWhitelist ) {
isValid = isValid && this.whitelistItems.some(x => {
return x === value
})
}
if ( this._useBlacklist ) {
is_valid = is_valid && !(this._blacklist.some(x => x === value))
if ( this.useBlacklist ) {
isValid = isValid && !(this.blacklistItems.some(x => x === value))
}
return is_valid
return isValid
}
/**
* Sets the Option as optional.
*/
optional() {
this._optional = true
optional(): this {
this.isOptional = true
return this
}
@ -216,27 +223,27 @@ export abstract class CLIOption<T> {
* Get an array of strings denoting the human-readable requirements for this option to be valid.
* @returns {Array<string>}
*/
getRequirementDisplays() {
getRequirementDisplays(): string[] {
const clauses = []
if ( this._useBlacklist ) {
clauses.push(`must not be one of: ${this._blacklist.map(x => String(x)).join(', ')}`)
if ( this.useBlacklist ) {
clauses.push(`must not be one of: ${this.blacklistItems.map(x => String(x)).join(', ')}`)
}
if ( this._useWhitelist ) {
clauses.push(`must be one of: ${this._whitelist.map(x => String(x)).join(', ')}`)
if ( this.useWhitelist ) {
clauses.push(`must be one of: ${this.whitelistItems.map(x => String(x)).join(', ')}`)
}
if ( this._useGreaterThan ) {
clauses.push(`must be greater than${this._greateerThanBit ? ' or equal to' : ''}: ${String(this._greaterThanValue)}`)
if ( this.useGreaterThan ) {
clauses.push(`must be greater than${this.greaterThanBit ? ' or equal to' : ''}: ${String(this.greaterThanValue)}`)
}
if ( this._useLessThan ) {
clauses.push(`must be less than${this._lessThanBit ? ' or equal to' : ''}: ${String(this._lessThanValue)}`)
if ( this.useLessThan ) {
clauses.push(`must be less than${this.lessThanBit ? ' or equal to' : ''}: ${String(this.lessThanValue)}`)
}
if ( this._useEquality ) {
clauses.push(`must be equal to: ${String(this._equalityValue)}`)
if ( this.useEquality ) {
clauses.push(`must be equal to: ${String(this.equalityValue)}`)
}
return clauses

View File

@ -1,4 +1,4 @@
import {CLIOption} from "./CLIOption"
import {CLIOption} from './CLIOption'
/**
* Non-positional, flag-based CLI option.
@ -24,8 +24,10 @@ export class FlagOption<T> extends CLIOption<T> {
* Description of the argument required by this flag.
* If this is set, the flag will expect a positional argument to follow as a param.
*/
public readonly argumentDescription?: string
) { super() }
public readonly argumentDescription?: string,
) {
super()
}
/**
* Get the referential name for this option.
@ -33,7 +35,7 @@ export class FlagOption<T> extends CLIOption<T> {
* be found, the short flag (without the '-') is used.
* @returns {string}
*/
getArgumentName() {
getArgumentName(): string {
if ( this.longFlag ) {
return this.longFlag.replace('--', '')
} else if ( this.shortFlag ) {

View File

@ -1,4 +1,4 @@
import {CLIOption} from "./CLIOption"
import {CLIOption} from './CLIOption'
/**
* A positional CLI option. Defined without a flag.
@ -19,14 +19,16 @@ export class PositionalOption<T> extends CLIOption<T> {
/**
* A usage message describing this parameter.
*/
public readonly message: string = ''
) { super() }
public readonly message: string = '',
) {
super()
}
/**
* Gets the name of the option.
* @returns {string}
*/
getArgumentName () {
getArgumentName(): string {
return this.name
}
}

View File

@ -1,16 +1,16 @@
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";
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
@ -27,28 +27,30 @@ export class CommandLine extends Unit {
/** Templates registered with the CLI command. These can be created with the TemplateDirective. */
protected templates: Collection<Template> = new Collection<Template>()
constructor() { super() }
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)
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() {
public isCLI(): boolean {
return this.app().hasUnit(CommandLineApplication)
}
/**
* Returns a string containing the Extollo ASCII logo.
*/
public getASCIILogo() {
public getASCIILogo(): string {
return ` _
/ /\\ ______ _ _ _
/ / \\ | ____| | | | | |
@ -64,24 +66,26 @@ export class CommandLine extends Unit {
* the directive available for use on the CLI.
* @param directiveClass
*/
public registerDirective(directiveClass: Instantiable<Directive>) {
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>) {
public hasDirective(directiveClass: Instantiable<Directive>): boolean {
return this.directives.includes(directiveClass)
}
/**
* Get a collection of all registered directives.
*/
public getDirectives() {
public getDirectives(): Collection<Instantiable<Directive>> {
return this.directives.clone()
}
@ -90,21 +94,23 @@ export class CommandLine extends Unit {
* available for use with the TemplateDirective service.
* @param template
*/
public registerTemplate(template: 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) {
return !!this.templates.firstWhere('name', '=', name)
public hasTemplate(name: string): boolean {
return Boolean(this.templates.firstWhere('name', '=', name))
}
/**
@ -118,7 +124,7 @@ export class CommandLine extends Unit {
/**
* Get a collection of all registered templates.
*/
public getTemplates() {
public getTemplates(): Collection<Template> {
return this.templates.clone()
}
}

View File

@ -1,12 +1,12 @@
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";
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
@ -18,12 +18,12 @@ export class CommandLineApplication extends Unit {
private static replacement?: typeof Unit
/** Set the replaced unit. */
public static setReplacement(unitClass?: typeof Unit) {
public static setReplacement(unitClass?: typeof Unit): void {
this.replacement = unitClass
}
/** Get the replaced unit. */
public static getReplacement() {
public static getReplacement(): typeof Unit | undefined {
return this.replacement
}
@ -33,9 +33,11 @@ export class CommandLineApplication extends Unit {
@Inject()
protected readonly logging!: Logging
constructor() { super() }
constructor() {
super()
}
public async up() {
public async up(): Promise<void> {
this.cli.registerDirective(UsageDirective)
this.cli.registerDirective(ShellDirective)
this.cli.registerDirective(TemplateDirective)
@ -50,7 +52,7 @@ export class CommandLineApplication extends Unit {
await match.invoke(argv.slice(1))
} else {
const usage = this.make<UsageDirective>(UsageDirective)
await usage.handle(process.argv)
await usage.handle()
}
}
}

View File

@ -1,22 +1,21 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* A template that generates a new configuration file in the app/configs directory.
*/
const config_template: Template = {
const templateConfig: Template = {
name: 'config',
fileSuffix: '.config.ts',
description: 'Create a new config file.',
baseAppPath: ['configs'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
render() {
return `import { env } from '@extollo/lib'
export default {
key: env('VALUE_ENV_VAR', 'default value'),
}
`
}
},
}
export { config_template }
export { templateConfig }

View File

@ -1,17 +1,15 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* Template that generates a new controller in the app/http/controllers directory.
*/
const controller_template: Template = {
const templateController: 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"
render(name: string) {
return `import {Controller, view, Inject, Injectable} from '@extollo/lib'
/**
* ${name} Controller
@ -25,7 +23,7 @@ export class ${name} extends Controller {
}
}
`
}
},
}
export { controller_template }
export { templateController }

View File

@ -1,17 +1,15 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* Template that generates a new Directive class in the app/directives directory.
*/
const directive_template: Template = {
const templateDirective: Template = {
name: 'directive',
fileSuffix: '.directive.ts',
description: 'Create a new Directive class which adds functionality to the ./ex command.',
baseAppPath: ['directives'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Directive, OptionDefinition} from "@extollo/cli"
import {Injectable} from "@extollo/di"
render(name: string) {
return `import {Directive, OptionDefinition, Injectable} from '@extollo/lib'
/**
* ${name} Directive
@ -37,7 +35,7 @@ export class ${name}Directive extends Directive {
}
}
`
}
},
}
export { directive_template }
export { templateDirective }

View File

@ -1,17 +1,15 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* Template that generates a new middleware class in app/http/middlewares.
*/
const middleware_template: Template = {
const templateMiddleware: 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"
render(name: string) {
return `import {Middleware, Injectable} from '@extollo/lib'
/**
* ${name} Middleware
@ -25,7 +23,7 @@ export class ${name} extends Middleware {
}
}
`
}
},
}
export { middleware_template }
export { templateMiddleware }

View File

@ -1,16 +1,15 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* Template that generates a new route definition file in app/http/routes.
*/
const routes_template: Template = {
const templateRoutes: Template = {
name: 'routes',
fileSuffix: '.routes.ts',
description: 'Create a file for route definitions.',
baseAppPath: ['http', 'routes'],