From 2ff935453814e3f130f7cd658ce6efb8d81a619d Mon Sep 17 00:00:00 2001 From: garrettmills Date: Fri, 8 Jul 2022 20:27:18 -0500 Subject: [PATCH] Make file extensions on Canonical resources runtime-agnostic --- package.json | 2 +- src/orm/services/Migrations.ts | 2 +- src/orm/services/Models.ts | 2 +- src/service/Canonical.ts | 19 ++++++++++++++++--- src/service/Config.ts | 2 +- src/service/Controllers.ts | 2 +- src/service/Middlewares.ts | 2 +- src/service/Queueables.ts | 2 +- 8 files changed, 23 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 13f3386..87e0aa0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/orm/services/Migrations.ts b/src/orm/services/Migrations.ts index ce5cbea..cdc8e65 100644 --- a/src/orm/services/Migrations.ts +++ b/src/orm/services/Migrations.ts @@ -18,7 +18,7 @@ export class Migrations extends CanonicalInstantiable { protected canonicalItem = 'migration' - protected suffix = '.migration.js' + protected suffix = '.migration' async up(): Promise { if ( await this.path.exists() ) { diff --git a/src/orm/services/Models.ts b/src/orm/services/Models.ts index 10719f8..630060b 100644 --- a/src/orm/services/Models.ts +++ b/src/orm/services/Models.ts @@ -17,7 +17,7 @@ export class Models extends CanonicalStatic, Instantiable> protected canonicalItem = 'model' - protected suffix = '.model.js' + protected suffix = '.model' public async up(): Promise { await super.up() diff --git a/src/service/Canonical.ts b/src/service/Canonical.ts index c8842fd..27f6424 100644 --- a/src/service/Canonical.ts +++ b/src/service/Canonical.ts @@ -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 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 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 extends Unit { public async up(): Promise { 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 extends Unit { .split('') .reverse() .join('') - .substr(this.suffix.length) + .substr(this.getScriptSuffix().length) .split('') .reverse() .join('') diff --git a/src/service/Config.ts b/src/service/Config.ts index 30aee95..ca9514f 100644 --- a/src/service/Config.ts +++ b/src/service/Config.ts @@ -12,7 +12,7 @@ export class Config extends CanonicalRecursive { protected appPath: string[] = ['configs'] - protected suffix = '.config.js' + protected suffix = '.config' protected canonicalItem = 'config' diff --git a/src/service/Controllers.ts b/src/service/Controllers.ts index c846b07..e8122ce 100644 --- a/src/service/Controllers.ts +++ b/src/service/Controllers.ts @@ -12,7 +12,7 @@ export class Controllers extends CanonicalStatic, Contr protected canonicalItem = 'controller' - protected suffix = '.controller.js' + protected suffix = '.controller' public async initCanonicalItem(definition: CanonicalDefinition): Promise, Controller>> { const item = await super.initCanonicalItem(definition) diff --git a/src/service/Middlewares.ts b/src/service/Middlewares.ts index e67a4e3..84113b1 100644 --- a/src/service/Middlewares.ts +++ b/src/service/Middlewares.ts @@ -12,7 +12,7 @@ export class Middlewares extends CanonicalStatic>> { const item = await super.initCanonicalItem(definition) diff --git a/src/service/Queueables.ts b/src/service/Queueables.ts index 5a4d625..84a99ba 100644 --- a/src/service/Queueables.ts +++ b/src/service/Queueables.ts @@ -12,5 +12,5 @@ export class Queueables extends CanonicalStatic