Make file extensions on Canonical resources runtime-agnostic

This commit is contained in:
Garrett Mills 2022-07-08 20:27:18 -05:00
parent cf0ae260dc
commit 2ff9354538
8 changed files with 23 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@extollo/lib",
"version": "0.9.50",
"version": "0.10.0",
"description": "The framework library that lifts up your code.",
"main": "lib/index.js",
"types": "lib/index.d.ts",

View File

@ -18,7 +18,7 @@ export class Migrations extends CanonicalInstantiable<Migration> {
protected canonicalItem = 'migration'
protected suffix = '.migration.js'
protected suffix = '.migration'
async up(): Promise<void> {
if ( await this.path.exists() ) {

View File

@ -17,7 +17,7 @@ export class Models extends CanonicalStatic<Model<any>, Instantiable<Model<any>>
protected canonicalItem = 'model'
protected suffix = '.model.js'
protected suffix = '.model'
public async up(): Promise<void> {
await super.up()

View File

@ -8,6 +8,7 @@ import {Inject} from '../di'
import * as nodePath from 'path'
import {Unit} from '../lifecycle/Unit'
import {isCanonicalReceiver} from '../support/CanonicalReceiver'
import {env} from '../lifecycle/Application'
/**
* Interface describing a definition of a single canonical item loaded from the app.
@ -70,9 +71,13 @@ export abstract class Canonical<T> extends Unit {
/**
* The file suffix of files in the base path that should be loaded.
* This should EXCLUDE `.js` or `.ts`. These file extensions are determined
* by the framework depending on the Node runtime.
* @example `.service`
* @example `.middleware`
* @type string
*/
protected suffix = '.js'
protected suffix = ''
/**
* The singular, programmatic name of one of these canonical items.
@ -113,6 +118,14 @@ export abstract class Canonical<T> extends Unit {
}
}
/**
* Get the suffix that files must have to be loaded by this service.
* Determines the runtime file extension based on the Node runtime.
*/
public getScriptSuffix(): string {
return `${this.suffix}${env('NODE_SCRIPT_SUFFIX', '.js')}`
}
/**
* Return an array of all loaded canonical names.
*/
@ -247,7 +260,7 @@ export abstract class Canonical<T> extends Unit {
public async up(): Promise<void> {
if ( await this.path.exists() ) {
for await ( const entry of this.path.walk() ) {
if ( !entry.endsWith(this.suffix) ) {
if ( !entry.endsWith(this.getScriptSuffix()) ) {
this.logging.debug(`Skipping file with invalid suffix: ${entry}`)
continue
}
@ -292,7 +305,7 @@ export abstract class Canonical<T> extends Unit {
.split('')
.reverse()
.join('')
.substr(this.suffix.length)
.substr(this.getScriptSuffix().length)
.split('')
.reverse()
.join('')

View File

@ -12,7 +12,7 @@ export class Config extends CanonicalRecursive {
protected appPath: string[] = ['configs']
protected suffix = '.config.js'
protected suffix = '.config'
protected canonicalItem = 'config'

View File

@ -12,7 +12,7 @@ export class Controllers extends CanonicalStatic<Instantiable<Controller>, Contr
protected canonicalItem = 'controller'
protected suffix = '.controller.js'
protected suffix = '.controller'
public async initCanonicalItem(definition: CanonicalDefinition): Promise<StaticClass<Instantiable<Controller>, Controller>> {
const item = await super.initCanonicalItem(definition)

View File

@ -12,7 +12,7 @@ export class Middlewares extends CanonicalStatic<Middleware, Instantiable<Middle
protected canonicalItem = 'middleware'
protected suffix = '.middleware.js'
protected suffix = '.middleware'
public async initCanonicalItem(definition: CanonicalDefinition): Promise<StaticClass<Middleware, Instantiable<Middleware>>> {
const item = await super.initCanonicalItem(definition)

View File

@ -12,5 +12,5 @@ export class Queueables extends CanonicalStatic<Queueables, Instantiable<Queueab
protected canonicalItem = 'job'
protected suffix = '.job.js'
protected suffix = '.job'
}