Skip to content

Commit

Permalink
adding alias types
Browse files Browse the repository at this point in the history
  • Loading branch information
marioke committed Mar 17, 2023
1 parent d32860c commit 1794550
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-code-generator",
"version": "0.0.7",
"version": "0.0.8",
"description": "",
"scripts": {
"build": "tsc",
Expand Down
40 changes: 40 additions & 0 deletions src/AliasTypeBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BuilderBase } from "./BuilderBase";
import { StringBuilder } from "./StringBuilder";

export class AliasTypeBuilder extends BuilderBase {
private readonly name: string;
private readonly type: string;
private export?: boolean;

public constructor(name: string, type: string) {
super();
if (!this.isValidObjectName(name)) {
throw new Error(`Invalid name for type: ${name}`);
}
this.name = name;
this.type = type;
}

public getName(): string {
return this.name;
}

public withExport(): this {
this.export = true;
return this;
}

public toArray(): Array<string> {
const builder = new StringBuilder();
const parts = [];
if (this.export) {
parts.push("export");
}
parts.push("type");
parts.push(this.name);
parts.push("=");
parts.push(this.type);
builder.append(parts.join(" "));
return builder.toArray();
}
}
26 changes: 26 additions & 0 deletions src/CodeDocument.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { AliasTypeBuilder } from "./AliasTypeBuilder";
import { ClassBuilder } from "./ClassBuilder";
import { InterfaceBuilder } from "./InterfaceBuilder";
import { StringBuilder } from "./StringBuilder";
import { TypeBuilder } from "./TypeBuilder";

export class CodeDocument {
private classes = new Array<ClassBuilder>();
private interfaces = new Array<InterfaceBuilder>();
private types = new Array<TypeBuilder>();
private aliasTypes = new Array<AliasTypeBuilder>();
private headComments: Array<string> = [];
private imports = new Array<Import>();

Expand Down Expand Up @@ -48,6 +52,16 @@ export class CodeDocument {
return this;
}

public addType(type: TypeBuilder): this {
this.types.push(type);
return this;
}

public addAliasType(type: AliasTypeBuilder): this {
this.aliasTypes.push(type);
return this;
}

public toString(): string {
const builder = new StringBuilder();

Expand All @@ -69,6 +83,18 @@ export class CodeDocument {
builder.addNewline();
}

if (this.aliasTypes.length > 0) {
this.aliasTypes.forEach((type) => {
builder.appendLines(type.toArray()).addNewline();
});
}

if (this.types.length > 0) {
this.types.forEach((type) => {
builder.appendLines(type.toArray()).addNewline();
});
}

this.interfaces.forEach((interfaceObj) => {
builder.appendLines(interfaceObj.toArray()).addNewline();
});
Expand Down

0 comments on commit 1794550

Please sign in to comment.