Skip to content

Commit

Permalink
feat: convert file paths to imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 5, 2023
1 parent 982922c commit 27d9e9a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
11 changes: 4 additions & 7 deletions lib/plugin/metadata-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ export class MetadataLoader {
}
}

private applyMetadata(models: Record<string, any>) {
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<string, any>) {
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];
});
});
}
Expand Down
26 changes: 19 additions & 7 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
Record<string, ClassMetadata>
> = {};

get collectedMetadata(): Record<string, Record<string, ClassMetadata>> {
return this._collectedMetadata;
get collectedMetadata(): Array<
[ts.CallExpression, Record<string, ClassMetadata>]
> {
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(
Expand Down Expand Up @@ -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
);
}
Expand Down
22 changes: 17 additions & 5 deletions lib/plugin/visitors/model-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ type ClassMetadata = Record<string, ts.ObjectLiteralExpression>;
export class ModelClassVisitor extends AbstractFileVisitor {
private readonly _collectedMetadata: Record<string, ClassMetadata> = {};

get collectedMetadata(): Record<string, ClassMetadata> {
return this._collectedMetadata;
get collectedMetadata(): Array<
[ts.CallExpression, Record<string, ClassMetadata>]
> {
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(
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 27d9e9a

Please sign in to comment.