Skip to content

Commit

Permalink
extend class imports
Browse files Browse the repository at this point in the history
  • Loading branch information
marioke committed Mar 17, 2023
1 parent 497f3a9 commit d32860c
Show file tree
Hide file tree
Showing 2 changed files with 14 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.6",
"version": "0.0.7",
"description": "",
"scripts": {
"build": "tsc",
Expand Down
13 changes: 13 additions & 0 deletions src/CodeDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ export class CodeDocument {
if (this.imports.find((i) => i.name === importObj.name)) {
throw new Error(`Import with name ${importObj.name} already exists`);
}

this.imports.push(importObj);
return this;
}

public addImports(importObjs: Array<Import>): this {
importObjs.forEach((importObj) => {
this.addImport(importObj);
});
return this;
}

public addInterface(interfaceObj: InterfaceBuilder): this {
if (this.interfaces.find((i) => i.getName() === interfaceObj.getName())) {
throw new Error(
Expand Down Expand Up @@ -52,6 +60,10 @@ export class CodeDocument {

if (this.imports.length > 0) {
this.imports.forEach((importObj) => {
if (importObj.type === 'named') {
builder.append(`import { ${importObj.name} } from "${importObj.path}";`);
return;
}
builder.append(`import ${importObj.name} from "${importObj.path}";`);
});
builder.addNewline();
Expand All @@ -72,4 +84,5 @@ export class CodeDocument {
interface Import {
name: string;
path: string;
type?: 'default' | 'named';
}

0 comments on commit d32860c

Please sign in to comment.