Skip to content

Commit

Permalink
eManPrague#41 Allow to specify custom folder for Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Kopecký committed Jul 29, 2021
1 parent 826d4ee commit 8ed9bf0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ Custom configuration is expected to be a JSON file with the following structure:
```ts
export interface IConfig {
api: string;
entitiesPath?: string;
enumsPath?: string;
observable?: ObservableConfig;
enums?: "enum" | "string";
dates?: "native" | "date-fns";
Expand Down
6 changes: 5 additions & 1 deletion packages/generator/src/openapi/fileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ export default class FileGenerator {
Handlebars.registerPartial("generatedEntityHeader", await this.readTemplate("generatedEntityHeader"));

const directory = this.project.createDirectory(this.config.entitiesPath);
let enumDirectory = directory;
if (this.config.enumsPath) {
enumDirectory = this.project.createDirectory(this.config.enumsPath);
}
const enumWriter =
this.config.enums === "enum" ? new EnumWriter(directory, templates) : new StringLiteralWriter(directory, templates);
this.config.enums === "enum" ? new EnumWriter(enumDirectory, templates) : new StringLiteralWriter(enumDirectory, templates);
const objectWriter = new ObjectEntityWriter(directory, this.config, templates);
const unionWriter = new UnionEntityWriter(directory, templates);

Expand Down
1 change: 1 addition & 0 deletions packages/generator/src/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IConfig {

entitiesPath: string;
repositoriesPath: string;
enumsPath?: string;

validation?: boolean;
conversion?: boolean;
Expand Down
51 changes: 48 additions & 3 deletions packages/generator/src/openapi/writers/objectEntityWriter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import camelCase from "lodash/camelCase";
import uniq from "lodash/uniq";
import path from "path";
import { Directory, SourceFile } from "ts-morph";
import GeneratorBase from "../../generatorBase";
import ObservableFormatter from "../formatters/observableFormatter";
Expand Down Expand Up @@ -47,18 +48,43 @@ export default class ObjectEntityWriter {

private createFile(fileName: string, definition: ObjectEntity, baseClass: ObjectEntity | undefined) {
const decoratorImports = this.getPropertyDecoratorsImports(definition.properties);
const entitiesToImport = definition.properties.filter(x => x.type.isImportRequired).map(x => x.type.getTypeName());
const propertiesToImport = definition.properties.filter(x => x.type.isImportRequired);

interface SplitImports {
enumsToImport: Array<string>;
entitiesToImport: Array<string>;
}

const { entitiesToImport, enumsToImport } = propertiesToImport.reduce(
(accumulator: SplitImports, property) => {
const typeName = property.type.getTypeName();
if (typeName) {
if (property.type.type instanceof Enum) {
accumulator.enumsToImport.push(typeName);
} else {
accumulator.entitiesToImport.push(typeName);
}
}
return accumulator;
},
{ entitiesToImport: [], enumsToImport: [] }
);

if (baseClass) {
entitiesToImport.push(baseClass.name);
}

const entityImports = uniq(entitiesToImport)
.sort()
.map(x => `import ${x} from "./${camelCase(x)}";`);
.map((x: string) => `import ${x} from "./${camelCase(x)}";`);

const pathToEnums = this.getPathToEnums();
const enumsImports = uniq(enumsToImport)
.sort()
.map((x: string) => `import ${x} from "${pathToEnums}/${camelCase(x)}";`);

const result = this.templates.objectEntityFile({
imports: [...decoratorImports, ...entityImports],
imports: [...decoratorImports, ...entityImports, ...enumsImports],
content: () => this.getEntityContent(definition, baseClass),
entity: definition,
baseClass,
Expand All @@ -67,6 +93,25 @@ export default class ObjectEntityWriter {
return this.parentDirectory.createSourceFile(fileName, result, { overwrite: true });
}

getPathToEnums() {
function convertPath(windowsPath: string) {
return windowsPath
.replace(/^\\\\\?\\/, "")
.replace(/\\/g, "/")
.replace(/\/\/+/g, "/");
}

if (this.config.enumsPath && this.config.entitiesPath) {
const relativePath = convertPath(path.relative(this.config.entitiesPath, this.config.enumsPath));
if (!relativePath.startsWith(".")) {
return `./${relativePath}`;
}
return relativePath;
}

return ".";
}

getPropertyDecoratorsImports(properties: EntityProperty[]) {
const result = new Set<string>();

Expand Down

0 comments on commit 8ed9bf0

Please sign in to comment.