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,10 +1,10 @@
import {Request} from "../../http/lifecycle/Request";
import {Injectable} from "../../di"
import {Locale} from "../service/Locale";
import {HTTPKernelModule} from "../../http/kernel/HTTPKernelModule";
import {HTTPKernel} from "../../http/kernel/HTTPKernel";
import {InjectSessionHTTPModule} from "../../http/kernel/module/InjectSessionHTTPModule";
import {Session} from "../../http/session/Session";
import {Request} from '../../http/lifecycle/Request'
import {Injectable} from '../../di'
import {Locale} from '../service/Locale'
import {HTTPKernelModule} from '../../http/kernel/HTTPKernelModule'
import {HTTPKernel} from '../../http/kernel/HTTPKernel'
import {InjectSessionHTTPModule} from '../../http/kernel/module/InjectSessionHTTPModule'
import {Session} from '../../http/session/Session'
/**
* An HTTP kernel module that adds the Locale service to the request container.
@@ -14,7 +14,7 @@ export class InjectRequestLocale extends HTTPKernelModule {
public executeWithBlockingWriteback = true
/** Register this kernel module to the given kernel. */
public static register(kernel: HTTPKernel) {
public static register(kernel: HTTPKernel): void {
kernel.register(this).after(InjectSessionHTTPModule)
}
@@ -23,7 +23,7 @@ export class InjectRequestLocale extends HTTPKernelModule {
* service into the Request container based on said locale.
* @param request
*/
public async apply(request: Request) {
public async apply(request: Request): Promise<Request> {
const session = <Session> request.make(Session)
const locale = <Locale> request.make(Locale)

View File

@@ -1,11 +1,11 @@
import {Singleton, Inject} from "../../di"
import {CommandLine} from "../../cli"
import {InjectRequestLocale} from "../modules/InjectRequestLocale"
import {locale_template} from "../template/locale"
import {Unit} from "../../lifecycle/Unit";
import {HTTPKernel} from "../../http/kernel/HTTPKernel";
import {Config} from "../../service/Config";
import {Logging} from "../../service/Logging";
import {Singleton, Inject} from '../../di'
import {CommandLine} from '../../cli'
import {InjectRequestLocale} from '../modules/InjectRequestLocale'
import {templateLocale} from '../template/locale'
import {Unit} from '../../lifecycle/Unit'
import {HTTPKernel} from '../../http/kernel/HTTPKernel'
import {Config} from '../../service/Config'
import {Logging} from '../../service/Logging'
/**
* Application unit to register @extollo/i18n resources.
@@ -30,9 +30,9 @@ export class Internationalization extends Unit {
* You can set the "locale.enable" config property to `false` to disable
* the InjectRequestLocale HTTP kernel module.
*/
up() {
up(): void {
this.logging.debug(`Registering locale template with CLI...`)
this.cli.registerTemplate(locale_template)
this.cli.registerTemplate(templateLocale)
if ( this.config.get('locale.enable', true) ) {
this.kernel.register(InjectRequestLocale).before()

View File

@@ -1,8 +1,8 @@
import {Injectable} from "../../di"
import {ErrorWithContext} from "../../util"
import * as pluralize from "pluralize"
import {AppClass} from "../../lifecycle/AppClass";
import {Config} from "../../service/Config";
import {Injectable} from '../../di'
import {ErrorWithContext} from '../../util'
import * as pluralize from 'pluralize'
import {AppClass} from '../../lifecycle/AppClass'
import {Config} from '../../service/Config'
/**
* Type name for the standalone localization helper function that can be passed around
@@ -15,7 +15,7 @@ export type LocaleHelper = (phrase: string, { plural, fallback, interp }: {plura
*/
@Injectable()
export class Locale extends AppClass {
protected get config() {
protected get config(): Config {
// For some reason, was having issues with this not injecting properly.
// TODO convert this back to @Inject() and solve that bug
return this.app().make<Config>(Config)
@@ -27,7 +27,7 @@ export class Locale extends AppClass {
* @example en_US means "lang:en_US" config scope
* @example es_MX means "lang:es_MX" config scope
*/
protected locale?: string
protected locale?: string,
) {
super()
if ( !this.locale ) {
@@ -39,7 +39,7 @@ export class Locale extends AppClass {
* Get the default locale that should be assigned if none is specified in the session.
* @return string
*/
getDefaultLocale() {
getDefaultLocale(): string {
return this.config.get('locale.default', 'en_US')
}
@@ -47,7 +47,7 @@ export class Locale extends AppClass {
* Set the preferred locale for lookups by this service.
* @param locale
*/
setLocale(locale: string) {
setLocale(locale: string): void {
this.locale = locale
}
@@ -56,7 +56,9 @@ export class Locale extends AppClass {
*/
helper(): LocaleHelper {
return (phrase: string, { plural, fallback, interp }: {plural?: number, fallback?: string, interp?: {[key: string]: any}} = {}) => {
return this.get(phrase, {plural, fallback, interp})
return this.get(phrase, {plural,
fallback,
interp})
}
}
@@ -131,11 +133,14 @@ export class Locale extends AppClass {
* @param interp
*/
get(phrase: string, { plural, fallback, interp }: {plural?: number, fallback?: string, interp?: {[key: string]: any}} = {}): string {
const scope = phrase.split(':').reverse().slice(1).reverse().join(':')
const scope = phrase.split(':').reverse()
.slice(1)
.reverse()
.join(':')
const specific = phrase.split(':').reverse()[0]
const load = `${this.locale}${scope ? ':' + scope : ''}.${specific}`
const translated = this.load(load)
const is_plural = plural && plural !== 1
const isPlural = plural && plural !== 1
if ( !translated ) {
return fallback ?? specific
@@ -144,13 +149,13 @@ export class Locale extends AppClass {
let ret = ''
if ( typeof translated === 'object' ) {
if ( is_plural && translated.many ) {
if ( isPlural && translated.many ) {
ret = translated.many
} else if ( is_plural && translated.one ) {
} else if ( isPlural && translated.one ) {
ret = pluralize(translated.one, plural)
} else if ( !is_plural && translated.one ) {
} else if ( !isPlural && translated.one ) {
ret = translated.one
} else if ( !is_plural && translated.many ) {
} else if ( !isPlural && translated.many ) {
ret = pluralize(translated.many, 1)
} else {
throw new ErrorWithContext(`Invalid translation config for ${phrase}. Must provide 'one' or 'many' keys.`, {
@@ -162,7 +167,7 @@ export class Locale extends AppClass {
})
}
} else if ( typeof translated === 'string' ) {
ret = pluralize(translated, is_plural ? 5 : 1)
ret = pluralize(translated, isPlural ? 5 : 1)
} else {
throw new ErrorWithContext(`Invalid translation object for ${phrase}.`, {
locale: this.locale,
@@ -175,6 +180,10 @@ export class Locale extends AppClass {
if ( interp ) {
for ( const key in interp ) {
if ( !Object.prototype.hasOwnProperty.call(interp, key) ) {
continue
}
const rex = new RegExp(`:${key}:`, 'g')
ret = ret.replace(rex, interp[key])
}
@@ -188,19 +197,32 @@ export class Locale extends AppClass {
* @param locale
* @protected
*/
protected load(locale: string) {
const subloc = locale.split(':').slice(1).join(':')
const specific_loc = locale.split(':').reverse()[0].split('.').slice(1).join('.')
protected load(locale: string): {[key: string]: string} | string {
const subloc = locale.split(':').slice(1)
.join(':')
const specificLocale = locale.split(':').reverse()[0].split('.').slice(1)
.join('.')
let common: any = this.config.get(`lang:common${subloc ? ':' + subloc : ''}${specific_loc ? '.' + specific_loc : ''}`, undefined)
let common: any = this.config.get(`lang:common${subloc ? ':' + subloc : ''}${specificLocale ? '.' + specificLocale : ''}`, undefined)
let specific: any = this.config.get(`lang:${locale}`, undefined)
if ( typeof specific === 'string' ) return specific
if ( typeof common === 'string' && typeof specific === 'undefined' ) return common
if ( typeof specific === 'string' ) {
return specific
}
if ( !common ) common = {}
if ( !specific ) specific = {}
if ( typeof common === 'string' && typeof specific === 'undefined' ) {
return common
}
return {...common, ...specific}
if ( !common ) {
common = {}
}
if ( !specific ) {
specific = {}
}
return {...common,
...specific}
}
}

View File

@@ -1,21 +1,21 @@
import {Template} from "../../cli"
import {UniversalPath} from "../../util"
import {Container} from "../../di"
import {Config} from "../../service/Config";
import {Template} from '../../cli'
import {Container} from '../../di'
import {Config} from '../../service/Config'
/**
* CLI template that generates a new locale file.
* Automatically adds placeholder entries for phrases that exist in the
* associated common locale file.
*/
const locale_template: Template = {
const templateLocale: Template = {
name: 'locale',
fileSuffix: '.config.ts',
description: 'Create a new config file that specifies translations for a locale.',
baseAppPath: ['configs', 'lang'],
render: (name: string, fullCanonicalName: string, targetFilePath: UniversalPath) => {
render: (name: string, fullCanonicalName: string) => {
const config = <Config> Container.getContainer().make(Config)
const subloc = fullCanonicalName.split(':').slice(1).join(':')
const subloc = fullCanonicalName.split(':').slice(1)
.join(':')
const common: any = config.get(`lang:common${subloc ? ':' + subloc : ''}`, {})
return `import {env} from '@extollo/lib'
@@ -27,4 +27,4 @@ ${Object.keys(common).map(key => ' ' + key + ': \'\',\n')}
},
}
export { locale_template }
export { templateLocale }