From 27d9e9af6c10b98d687d874e2e64626377bebe98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Mon, 5 Jun 2023 15:34:47 +0200 Subject: [PATCH] feat: convert file paths to imports --- lib/plugin/metadata-loader.ts | 11 +++----- .../visitors/controller-class.visitor.ts | 26 ++++++++++++++----- lib/plugin/visitors/model-class.visitor.ts | 22 ++++++++++++---- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lib/plugin/metadata-loader.ts b/lib/plugin/metadata-loader.ts index 085f7dbc7..bad4f9315 100644 --- a/lib/plugin/metadata-loader.ts +++ b/lib/plugin/metadata-loader.ts @@ -15,14 +15,11 @@ export class MetadataLoader { } } - private applyMetadata(models: Record) { - Object.keys(models).forEach((path) => { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const fileRef = require(path); - - Object.keys(models[path]).forEach((key) => { + private applyMetadata(meta: Record) { + meta.forEach(([fileRef, fileMeta]) => { + Object.keys(fileMeta).forEach((key) => { const clsRef = fileRef[key]; - clsRef[METADATA_FACTORY_NAME] = () => models[path][key]; + clsRef[METADATA_FACTORY_NAME] = () => fileMeta[key]; }); }); } diff --git a/lib/plugin/visitors/controller-class.visitor.ts b/lib/plugin/visitors/controller-class.visitor.ts index 5b0771e25..e2f4a966a 100644 --- a/lib/plugin/visitors/controller-class.visitor.ts +++ b/lib/plugin/visitors/controller-class.visitor.ts @@ -27,8 +27,20 @@ export class ControllerClassVisitor extends AbstractFileVisitor { Record > = {}; - get collectedMetadata(): Record> { - return this._collectedMetadata; + get collectedMetadata(): Array< + [ts.CallExpression, Record] + > { + const metadataWithImports = []; + Object.keys(this._collectedMetadata).forEach((filePath) => { + const metadata = this._collectedMetadata[filePath]; + const importExpr = ts.factory.createCallExpression( + ts.factory.createIdentifier('require'), + undefined, + [ts.factory.createStringLiteral(filePath)] + ); + metadataWithImports.push([importExpr, metadata]); + }); + return metadataWithImports; } visit( @@ -62,19 +74,19 @@ export class ControllerClassVisitor extends AbstractFileVisitor { sourceFile.fileName ); - if (!this.collectedMetadata[filePath]) { - this.collectedMetadata[filePath] = {}; + if (!this._collectedMetadata[filePath]) { + this._collectedMetadata[filePath] = {}; } const parent = node.parent as ts.ClassDeclaration; const clsName = parent.name?.getText(); if (clsName) { - if (!this.collectedMetadata[filePath][clsName]) { - this.collectedMetadata[filePath][clsName] = {}; + if (!this._collectedMetadata[filePath][clsName]) { + this._collectedMetadata[filePath][clsName] = {}; } Object.assign( - this.collectedMetadata[filePath][clsName], + this._collectedMetadata[filePath][clsName], metadata ); } diff --git a/lib/plugin/visitors/model-class.visitor.ts b/lib/plugin/visitors/model-class.visitor.ts index 4a1eb9ef0..599ce411f 100644 --- a/lib/plugin/visitors/model-class.visitor.ts +++ b/lib/plugin/visitors/model-class.visitor.ts @@ -31,8 +31,20 @@ type ClassMetadata = Record; export class ModelClassVisitor extends AbstractFileVisitor { private readonly _collectedMetadata: Record = {}; - get collectedMetadata(): Record { - return this._collectedMetadata; + get collectedMetadata(): Array< + [ts.CallExpression, Record] + > { + const metadataWithImports = []; + Object.keys(this._collectedMetadata).forEach((filePath) => { + const metadata = this._collectedMetadata[filePath]; + const importExpr = ts.factory.createCallExpression( + ts.factory.createIdentifier('require'), + undefined, + [ts.factory.createStringLiteral(filePath)] + ); + metadataWithImports.push([importExpr, metadata]); + }); + return metadataWithImports; } visit( @@ -163,11 +175,11 @@ export class ModelClassVisitor extends AbstractFileVisitor { options.pathToSource, sourceFile.fileName ); - if (!this.collectedMetadata[filePath]) { - this.collectedMetadata[filePath] = {}; + if (!this._collectedMetadata[filePath]) { + this._collectedMetadata[filePath] = {}; } const attributeKey = node.name.getText(); - this.collectedMetadata[filePath][attributeKey] = returnValue; + this._collectedMetadata[filePath][attributeKey] = returnValue; return; }