Browse Source

Setup eslint and enforce rules

orm-types
Garrett Mills 1 year ago
parent
commit
1d5056b753
Signed by: garrettmills GPG Key ID: D2BF5FBA8298F246
  1. 3
      .eslintignore
  2. 113
      .eslintrc.json
  3. 1338
      package-lock.json
  4. 12
      package.json
  5. 1025
      pnpm-lock.yaml
  6. 123
      src/cli/Directive.ts
  7. 10
      src/cli/directive/RunDirective.ts
  8. 10
      src/cli/directive/ShellDirective.ts
  9. 14
      src/cli/directive/TemplateDirective.ts
  10. 20
      src/cli/directive/UsageDirective.ts
  11. 133
      src/cli/directive/options/CLIOption.ts
  12. 10
      src/cli/directive/options/FlagOption.ts
  13. 10
      src/cli/directive/options/PositionalOption.ts
  14. 66
      src/cli/service/CommandLine.ts
  15. 30
      src/cli/service/CommandLineApplication.ts
  16. 11
      src/cli/templates/config.ts
  17. 14
      src/cli/templates/controller.ts
  18. 14
      src/cli/templates/directive.ts
  19. 14
      src/cli/templates/middleware.ts
  20. 13
      src/cli/templates/routes.ts
  21. 14
      src/cli/templates/unit.ts
  22. 122
      src/di/Container.ts
  23. 24
      src/di/ScopedContainer.ts
  24. 53
      src/di/decorator/injection.ts
  25. 2
      src/di/error/DuplicateFactoryKeyError.ts
  26. 2
      src/di/error/InvalidDependencyKeyError.ts
  27. 12
      src/di/factory/AbstractFactory.ts
  28. 16
      src/di/factory/ClosureFactory.ts
  29. 22
      src/di/factory/Factory.ts
  30. 10
      src/di/factory/NamedFactory.ts
  31. 22
      src/di/factory/SingletonFactory.ts
  32. 20
      src/di/types.ts
  33. 14
      src/forms/FormRequest.ts
  34. 31
      src/forms/Validator.ts
  35. 4
      src/forms/middleware.ts
  36. 216
      src/forms/rules/arrays.ts
  37. 110
      src/forms/rules/inference.ts
  38. 319
      src/forms/rules/numeric.ts
  39. 272
      src/forms/rules/presence.ts
  40. 371
      src/forms/rules/strings.ts
  41. 12
      src/forms/templates/form.ts
  42. 8
      src/forms/unit/Forms.ts
  43. 13
      src/http/Controller.ts
  44. 4
      src/http/HTTPError.ts
  45. 36
      src/http/kernel/HTTPCookieJar.ts
  46. 41
      src/http/kernel/HTTPKernel.ts
  47. 12
      src/http/kernel/HTTPKernelModule.ts
  48. 14
      src/http/kernel/module/AbstractResolvedRouteHandlerHTTPModule.ts
  49. 20
      src/http/kernel/module/ExecuteResolvedRouteHandlerHTTPModule.ts
  50. 18
      src/http/kernel/module/ExecuteResolvedRoutePostflightHTTPModule.ts
  51. 18
      src/http/kernel/module/ExecuteResolvedRoutePreflightHTTPModule.ts
  52. 20
      src/http/kernel/module/InjectSessionHTTPModule.ts
  53. 16
      src/http/kernel/module/MountActivatedRouteHTTPModule.ts
  54. 40
      src/http/kernel/module/ParseIncomingBodyHTTPModule.ts
  55. 12
      src/http/kernel/module/PersistSessionHTTPModule.ts
  56. 14
      src/http/kernel/module/PoweredByHeaderInjectionHTTPModule.ts
  57. 18
      src/http/kernel/module/SetSessionCookieHTTPModule.ts
  58. 65
      src/http/lifecycle/Request.ts
  59. 130
      src/http/lifecycle/Response.ts
  60. 14
      src/http/response/DehydratedStateResponseFactory.ts
  61. 54
      src/http/response/ErrorResponseFactory.ts
  62. 10
      src/http/response/HTMLResponseFactory.ts
  63. 6
      src/http/response/HTTPErrorResponseFactory.ts
  64. 14
      src/http/response/JSONResponseFactory.ts
  65. 6
      src/http/response/ResponseFactory.ts
  66. 10
      src/http/response/StringResponseFactory.ts
  67. 14
      src/http/response/TemporaryRedirectResponseFactory.ts
  68. 16
      src/http/response/ViewResponseFactory.ts
  69. 24
      src/http/response/api.ts
  70. 6
      src/http/routing/ActivatedRoute.ts
  71. 15
      src/http/routing/Middleware.ts
  72. 136
      src/http/routing/Route.ts
  73. 30
      src/http/routing/RouteGroup.ts
  74. 54
      src/http/session/MemorySession.ts
  75. 10
      src/http/session/Session.ts
  76. 37
      src/http/session/SessionFactory.ts
  77. 18
      src/i18n/modules/InjectRequestLocale.ts
  78. 20
      src/i18n/service/Internationalization.ts
  79. 74
      src/i18n/service/Locale.ts
  80. 16
      src/i18n/template/locale.ts
  81. 26
      src/lifecycle/AppClass.ts
  82. 81
      src/lifecycle/Application.ts
  83. 24
      src/lifecycle/RunLevelErrorHandler.ts
  84. 6
      src/lifecycle/Unit.ts
  85. 21
      src/orm/DatabaseService.ts
  86. 205
      src/orm/builder/AbstractBuilder.ts
  87. 18
      src/orm/builder/Builder.ts
  88. 10
      src/orm/builder/result/AbstractResultIterable.ts
  89. 6
      src/orm/builder/result/ResultCollection.ts
  90. 20
      src/orm/builder/result/ResultIterable.ts
  91. 12
      src/orm/connection/Connection.ts
  92. 27
      src/orm/connection/PostgresConnection.ts
  93. 105
      src/orm/dialect/PostgreSQLDialect.ts
  94. 16
      src/orm/dialect/SQLDialect.ts
  95. 16
      src/orm/model/Field.ts
  96. 226
      src/orm/model/Model.ts
  97. 16
      src/orm/model/ModelBuilder.ts
  98. 31
      src/orm/model/ModelResultIterable.ts
  99. 23
      src/orm/services/Database.ts
  100. 18
      src/orm/services/Models.ts

3
.eslintignore

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

113
.eslintrc.json

@ -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

File diff suppressed because it is too large

12
package.json

@ -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"
}
}

1025
pnpm-lock.yaml

File diff suppressed because it is too large

123
src/cli/Directive.ts

@ -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
}
}

10
src/cli/directive/RunDirective.ts

@ -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.

10
src/cli/directive/ShellDirective.ts

@ -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())

14
src/cli/directive/TemplateDirective.ts

@ -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')

20
src/cli/directive/UsageDirective.ts

@ -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'))
}
}

133
src/cli/directive/options/CLIOption.ts

@ -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

10
src/cli/directive/options/FlagOption.ts

@ -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 ) {

10
src/cli/directive/options/PositionalOption.ts

@ -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
}
}

66
src/cli/service/CommandLine.ts

@ -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()
}
}

30
src/cli/service/CommandLineApplication.ts

@ -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()
}
}
}

11
src/cli/templates/config.ts

@ -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 }

14
src/cli/templates/controller.ts

@ -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 }

14
src/cli/templates/directive.ts

@ -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 }

14
src/cli/templates/middleware.ts

@ -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 }

13
src/cli/templates/routes.ts

@ -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'],
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
return `import {Route} from "@extollo/lib"
render(name: string) {
return `import {Route} from '@extollo/lib'
/*
* ${name} Routes
@ -20,7 +19,7 @@ const routes_template: Template = {
`
}
},
}
export { routes_template }
export { templateRoutes }

14
src/cli/templates/unit.ts

@ -1,17 +1,15 @@
import {Template} from "../Template"
import {UniversalPath} from "../../util"
import {Template} from '../Template'
/**
* Template that generates a new application unit class in app/units.
*/
const unit_template: Template = {
const templateUnit: 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"
render(name: string) {
return `import {Singleton, Inject, Unit, Logging} from '@extollo/lib'
/**
* ${name} Unit
@ -32,7 +30,7 @@ export class ${name} extends Unit {
}
}
`
}
},
}
export { unit_template }
export { templateUnit }

122
src/di/Container.ts

@ -1,14 +1,14 @@
import {DependencyKey, InstanceRef, Instantiable, isInstantiable, StaticClass} from "./types";
import {AbstractFactory} from "./factory/AbstractFactory";
import {collect, Collection, globalRegistry, logIfDebugging} from "../util";
import {Factory} from "./factory/Factory";
import {DuplicateFactoryKeyError} from "./error/DuplicateFactoryKeyError";
import {ClosureFactory} from "./factory/ClosureFactory";
import NamedFactory from "./factory/NamedFactory";
import SingletonFactory from "./factory/SingletonFactory";
import {InvalidDependencyKeyError} from "./error/InvalidDependencyKeyError";
export type MaybeFactory = AbstractFactory | undefined
import {DependencyKey, InstanceRef, Instantiable, isInstantiable} from './types'
import {AbstractFactory} from './factory/AbstractFactory'
import {collect, Collection, globalRegistry, logIfDebugging} from '../util'
import {Factory} from './factory/Factory'
import {DuplicateFactoryKeyError} from './error/DuplicateFactoryKeyError'