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

This commit is contained in:
2021-06-02 22:36:25 -05:00
parent 82e7a1f299
commit 1d5056b753
149 changed files with 6104 additions and 3114 deletions

View File

@@ -1,4 +1,4 @@
import {Container} from '../di';
import {Container} from '../di'
import {
ErrorWithContext,
globalRegistry,
@@ -7,14 +7,14 @@ import {
PathLike,
StandardLogger,
universalPath,
UniversalPath
} from '../util';
UniversalPath,
} from '../util'
import {Logging} from '../service/Logging';
import {RunLevelErrorHandler} from "./RunLevelErrorHandler";
import {Unit, UnitStatus} from "./Unit";
import * as dotenv from 'dotenv';
import {CacheFactory} from "../support/cache/CacheFactory";
import {Logging} from '../service/Logging'
import {RunLevelErrorHandler} from './RunLevelErrorHandler'
import {Unit, UnitStatus} from './Unit'
import * as dotenv from 'dotenv'
import {CacheFactory} from '../support/cache/CacheFactory'
/**
* Helper function that resolves and infers environment variable values.
@@ -24,7 +24,7 @@ import {CacheFactory} from "../support/cache/CacheFactory";
* @param key
* @param defaultValue
*/
export function env(key: string, defaultValue?: any): any {
export function env(key: string, defaultValue?: unknown): any {
return Application.getApplication().env(key, defaultValue)
}
@@ -107,7 +107,7 @@ export class Application extends Container {
* If true, the "Starting Extollo..." messages will always
* be logged.
*/
public forceStartupMessage: boolean = true
public forceStartupMessage = true
constructor() {
super()
@@ -129,21 +129,21 @@ export class Application extends Container {
* Returns true if the given unit class is registered with the application.
* @param unitClass
*/
public hasUnit(unitClass: typeof Unit) {
public hasUnit(unitClass: typeof Unit): boolean {
return this.applicationUnits.includes(unitClass)
}
/**
* Return a UniversalPath to the root of the application.
*/
get root() {
get root(): UniversalPath {
return this.basePath.concat()
}
/**
* Returns a UniversalPath to the `app/` directory in the application.
*/
get appRoot() {
get appRoot(): UniversalPath {
return this.basePath.concat('app')
}
@@ -151,7 +151,7 @@ export class Application extends Container {
* Resolve a path relative to the root of the application.
* @param parts
*/
path(...parts: PathLike[]) {
path(...parts: PathLike[]): UniversalPath {
return this.basePath.concat(...parts)
}
@@ -159,14 +159,14 @@ export class Application extends Container {
* Resolve a path relative to the `app/` directory in the application.
* @param parts
*/
appPath(...parts: PathLike[]) {
appPath(...parts: PathLike[]): UniversalPath {
return this.basePath.concat('app', ...parts)
}
/**
* Get an instance of the RunLevelErrorHandler.
*/
get errorHandler() {
get errorHandler(): (e: Error) => void {
const rleh: RunLevelErrorHandler = this.make<RunLevelErrorHandler>(RunLevelErrorHandler)
return rleh.handle
}
@@ -186,7 +186,7 @@ export class Application extends Container {
* @param absolutePathToApplicationRoot
* @param applicationUnits
*/
scaffold(absolutePathToApplicationRoot: string, applicationUnits: (typeof Unit)[]) {
scaffold(absolutePathToApplicationRoot: string, applicationUnits: (typeof Unit)[]): void {
this.baseDir = absolutePathToApplicationRoot
this.basePath = universalPath(absolutePathToApplicationRoot)
this.applicationUnits = applicationUnits
@@ -203,32 +203,30 @@ export class Application extends Container {
* Initialize the logger and load the logging level from the environment.
* @protected
*/
protected setupLogging() {
protected setupLogging(): void {
const standard: StandardLogger = this.make<StandardLogger>(StandardLogger)
const logging: Logging = this.make<Logging>(Logging)
logging.registerLogger(standard)
logging.verbose('Attempting to load logging level from the environment...')
try {
logging.verbose('Attempting to load logging level from the environment...')
const envLevel = this.env('EXTOLLO_LOGGING_LEVEL')
logging.verbose(`Read logging level: ${envLevel}`)
const envLevel = this.env('EXTOLLO_LOGGING_LEVEL')
logging.verbose(`Read logging level: ${envLevel}`)
if ( isLoggingLevel(envLevel) ) {
logging.verbose('Logging level is valid.')
logging.level = envLevel
logging.debug(`Set logging level from environment: ${envLevel}`)
}
} catch(e) {}
if ( isLoggingLevel(envLevel) ) {
logging.verbose('Logging level is valid.')
logging.level = envLevel
logging.debug(`Set logging level from environment: ${envLevel}`)
}
}
/**
* Initialize the environment variable library and read from the `.env` file.
* @protected
*/
protected bootstrapEnvironment() {
protected bootstrapEnvironment(): void {
dotenv.config({
path: this.basePath.concat('.env').toLocal
path: this.basePath.concat('.env').toLocal,
})
}
@@ -238,14 +236,14 @@ export class Application extends Container {
* @param key
* @param defaultValue
*/
public env(key: string, defaultValue?: any): any {
public env(key: string, defaultValue?: unknown): any {
return infer(process.env[key] ?? '') ?? defaultValue
}
/**
* Run the application by starting all units in order, then stopping them in reverse order.
*/
async run() {
async run(): Promise<void> {
try {
await this.up()
await this.down()
@@ -257,7 +255,7 @@ export class Application extends Container {
/**
* Start all units in the application, one at a time, in order.
*/
async up() {
async up(): Promise<void> {
const logging: Logging = this.make<Logging>(Logging)
logging.info('Starting Extollo...', this.forceStartupMessage)
@@ -271,12 +269,14 @@ export class Application extends Container {
/**
* Stop all units in the application, one at a time, in reverse order.
*/
async down() {
async down(): Promise<void> {
const logging: Logging = this.make<Logging>(Logging)
logging.info('Stopping Extollo...', this.forceStartupMessage)
for ( const unit of [...this.instantiatedUnits].reverse() ) {
if ( !unit ) continue
if ( !unit ) {
continue
}
await this.stopUnit(unit)
}
}
@@ -285,7 +285,7 @@ export class Application extends Container {
* Start a single unit, setting its status.
* @param unit
*/
public async startUnit(unit: Unit) {
public async startUnit(unit: Unit): Promise<void> {
const logging: Logging = this.make<Logging>(Logging)
try {
@@ -296,8 +296,7 @@ export class Application extends Container {
logging.info(`Started ${unit.constructor.name}.`)
} catch (e) {
unit.status = UnitStatus.Error
console.log(e)
throw this.errorWrapContext(e, {unit_name: unit.constructor.name})
throw this.errorWrapContext(e, {unitName: unit.constructor.name})
}
}
@@ -305,7 +304,7 @@ export class Application extends Container {
* Stop a single unit, setting its status.
* @param unit
*/
public async stopUnit(unit: Unit) {
public async stopUnit(unit: Unit): Promise<void> {
const logging: Logging = this.make<Logging>(Logging)
try {
@@ -316,7 +315,7 @@ export class Application extends Container {
logging.info(`Stopped ${unit.constructor.name}.`)
} catch (e) {
unit.status = UnitStatus.Error
throw this.errorWrapContext(e, {unit_name: unit.constructor.name})
throw this.errorWrapContext(e, {unitName: unit.constructor.name})
}
}
}