Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {ViewEngine} from "./ViewEngine"
|
||||
import {Injectable} from "../di"
|
||||
import * as pug from "pug"
|
||||
import {ViewEngine} from './ViewEngine'
|
||||
import {Injectable} from '../di'
|
||||
import * as pug from 'pug'
|
||||
|
||||
/**
|
||||
* Implementation of the ViewEngine class that renders Pug/Jade templates.
|
||||
@@ -16,9 +16,13 @@ export class PugViewEngine extends ViewEngine {
|
||||
|
||||
public renderByName(templateName: string, locals: { [p: string]: any }): string | Promise<string> {
|
||||
let compiled = this.compileCache[templateName]
|
||||
if ( compiled ) return compiled(locals)
|
||||
if ( compiled ) {
|
||||
return compiled(locals)
|
||||
}
|
||||
|
||||
if ( !templateName.endsWith('.pug') ) templateName += '.pug'
|
||||
if ( !templateName.endsWith('.pug') ) {
|
||||
templateName += '.pug'
|
||||
}
|
||||
const filePath = this.path.concat(...templateName.split(':'))
|
||||
compiled = pug.compileFile(filePath.toLocal, this.getOptions())
|
||||
|
||||
@@ -30,7 +34,7 @@ export class PugViewEngine extends ViewEngine {
|
||||
* Get the object of options passed to Pug's compile methods.
|
||||
* @protected
|
||||
*/
|
||||
protected getOptions() {
|
||||
protected getOptions(): pug.Options {
|
||||
return {
|
||||
basedir: this.path.toLocal,
|
||||
debug: this.debug,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import {AppClass} from "../lifecycle/AppClass"
|
||||
import {Config} from "../service/Config"
|
||||
import {Container} from "../di"
|
||||
import {UniversalPath} from "../util"
|
||||
import {AppClass} from '../lifecycle/AppClass'
|
||||
import {Config} from '../service/Config'
|
||||
import {Container} from '../di'
|
||||
import {UniversalPath} from '../util'
|
||||
|
||||
/**
|
||||
* Abstract base class for rendering views via different view engines.
|
||||
*/
|
||||
export abstract class ViewEngine extends AppClass {
|
||||
protected readonly config: Config
|
||||
|
||||
protected readonly debug: boolean
|
||||
|
||||
constructor() {
|
||||
|
||||
@@ -5,20 +5,21 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY
|
||||
} from "../di"
|
||||
import {Collection, ErrorWithContext} from "../util"
|
||||
import {Logging} from "../service/Logging";
|
||||
import {Config} from "../service/Config";
|
||||
import {ViewEngine} from "./ViewEngine";
|
||||
import {PugViewEngine} from "./PugViewEngine";
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, StaticClass, Instantiable,
|
||||
} from '../di'
|
||||
import {Collection, ErrorWithContext} from '../util'
|
||||
import {Logging} from '../service/Logging'
|
||||
import {Config} from '../service/Config'
|
||||
import {ViewEngine} from './ViewEngine'
|
||||
import {PugViewEngine} from './PugViewEngine'
|
||||
|
||||
/**
|
||||
* Dependency factory whose token matches the abstract ViewEngine class, but produces
|
||||
* a particular ViewEngine implementation based on the configuration.
|
||||
*/
|
||||
export class ViewEngineFactory extends AbstractFactory {
|
||||
export class ViewEngineFactory extends AbstractFactory<ViewEngine> {
|
||||
protected readonly logging: Logging
|
||||
|
||||
protected readonly config: Config
|
||||
|
||||
constructor() {
|
||||
@@ -27,17 +28,19 @@ export class ViewEngineFactory extends AbstractFactory {
|
||||
this.config = Container.getContainer().make<Config>(Config)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): ViewEngine {
|
||||
return new (this.getViewEngineClass())
|
||||
produce(): ViewEngine {
|
||||
return new (this.getViewEngineClass())()
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
match(something: unknown): boolean {
|
||||
return something === ViewEngine
|
||||
}
|
||||
|
||||
getDependencyKeys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getViewEngineClass())
|
||||
if ( meta ) return meta
|
||||
if ( meta ) {
|
||||
return meta
|
||||
}
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
@@ -47,7 +50,9 @@ export class ViewEngineFactory extends AbstractFactory {
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
if ( loadedMeta ) meta.concat(loadedMeta)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
currentToken = Object.getPrototypeOf(currentToken)
|
||||
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||||
|
||||
@@ -58,13 +63,13 @@ export class ViewEngineFactory extends AbstractFactory {
|
||||
* Using the config, get the implementation of the ViewEngine that should be used in the application.
|
||||
* @protected
|
||||
*/
|
||||
protected getViewEngineClass() {
|
||||
protected getViewEngineClass(): StaticClass<ViewEngine, Instantiable<ViewEngine>> {
|
||||
const ViewEngineClass = this.config.get('server.view_engine.driver', PugViewEngine)
|
||||
|
||||
if ( !isInstantiable(ViewEngineClass) || !(ViewEngineClass.prototype instanceof ViewEngine) ) {
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.ViewEngine');
|
||||
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.ViewEngine')
|
||||
e.context = {
|
||||
config_key: 'server.view_engine.driver',
|
||||
configKey: 'server.view_engine.driver',
|
||||
class: ViewEngineClass.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user