Skip to content

Commit

Permalink
feat: separate between input and output type imports
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann committed Apr 21, 2021
1 parent 20bbbc4 commit c17ff55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
56 changes: 46 additions & 10 deletions src/Handlers/BaseHandler/BaseFileGenerator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ export class BaseFileGenerator {
});
}

addInputTypeImports({ sourceFile, types, model }: { model?: string; sourceFile: SourceFile; types: string[] }): void {
types.sort(comparePrimitiveValues).forEach((type) => {
let moduleSpecifier = "";
const currentModel = this._baseParser.getModelName(type);

if (model && model === currentModel) {
moduleSpecifier = `.`;
}

if (model && currentModel && model !== currentModel) {
moduleSpecifier = `../../${currentModel}/${this._config.paths.inputTypes}`;
}

if (!model && !currentModel) {
moduleSpecifier = `.`;
}

if (model && !currentModel) {
moduleSpecifier = `../../${this._config.paths.shared}/${this._config.paths.inputTypes}`;
}

sourceFile.addImportDeclaration({
moduleSpecifier: `${moduleSpecifier}/${type}`,
namedImports: [type],
});
});
}

// eslint-disable-next-line class-methods-use-this
addJsonImports({ sourceFile }: { sourceFile: SourceFile }): void {
sourceFile.addImportDeclaration({
Expand Down Expand Up @@ -107,7 +135,15 @@ export class BaseFileGenerator {
nestJSImportDeclaration.addNamedImports(Array.from(new Set(imports)).sort(comparePrimitiveValues));
}

addTypeImports({ sourceFile, types, model }: { model?: string; sourceFile: SourceFile; types: string[] }): void {
addOutputTypeImports({
sourceFile,
types,
model,
}: {
model?: string;
sourceFile: SourceFile;
types: string[];
}): void {
types.sort(comparePrimitiveValues).forEach((type) => {
let moduleSpecifier = "";
const currentModel = this._baseParser.getModelName(type);
Expand All @@ -116,17 +152,17 @@ export class BaseFileGenerator {
moduleSpecifier = `.`;
}

if (model && currentModel && model !== currentModel) {
moduleSpecifier = `../../${currentModel}/${this._config.paths.inputTypes}`;
}
// if (model && currentModel && model !== currentModel) {
// moduleSpecifier = `../../${currentModel}/${this._config.paths.inputTypes}`;
// }

if (!model && !currentModel) {
moduleSpecifier = `.`;
}
// if (!model && !currentModel) {
// moduleSpecifier = `.`;
// }

if (model && !currentModel) {
moduleSpecifier = `../../${this._config.paths.shared}/${this._config.paths.inputTypes}`;
}
// if (model && !currentModel) {
// moduleSpecifier = `../../${this._config.paths.shared}/${this._config.paths.inputTypes}`;
// }

sourceFile.addImportDeclaration({
moduleSpecifier: `${moduleSpecifier}/${type}`,
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/InputTypeHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class InputTypeHandler extends BaseHandler {
}

if (inputTypeImports && inputTypeImports.length > 0) {
this.baseFileGenerator.addTypeImports({
this.baseFileGenerator.addInputTypeImports({
model,
sourceFile,
types: inputTypeImports,
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/OutputTypeHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class OutputTypeHandler extends BaseHandler {
}

if (outputTypeImports && outputTypeImports.length > 0) {
this.baseFileGenerator.addTypeImports({
this.baseFileGenerator.addOutputTypeImports({
model,
sourceFile,
types: outputTypeImports,
Expand Down

0 comments on commit c17ff55

Please sign in to comment.