remove unused import declarations

This commit is contained in:
Kononnable 2019-11-07 22:03:01 +01:00
parent 35c7e35f74
commit 1057b665ac
2 changed files with 18 additions and 2 deletions

View File

@ -55,13 +55,30 @@ export default function modelGenerationPhase(
`${casedFileName}.ts`
);
const rendered = compliedTemplate(element);
const formatted = Prettier.format(rendered, { parser: "typescript" });
const withImportStatements = removeUnusedImports(rendered);
const formatted = Prettier.format(withImportStatements, {
parser: "typescript"
});
fs.writeFileSync(resultFilePath, formatted, {
encoding: "UTF-8",
flag: "w"
});
});
}
function removeUnusedImports(rendered: string) {
const openBracketIndex = rendered.indexOf("{") + 1;
const closeBracketIndex = rendered.indexOf("}");
const imports = rendered
.substring(openBracketIndex, closeBracketIndex)
.split(",");
const restOfEntityDefinition = rendered.substring(closeBracketIndex);
const distinctImports = imports.filter(
v => restOfEntityDefinition.indexOf(v) !== -1
);
return `${rendered.substring(0, openBracketIndex)}${distinctImports.join(
","
)}${restOfEntityDefinition}`;
}
function createHandlebarsHelpers(generationOptions: IGenerationOptions): void {
Handlebars.registerHelper("json", context => {

View File

@ -38,7 +38,6 @@ export class {{toEntityName tscName}}{{#activeRecord}} extends BaseEntity{{/acti
{{#if generateConstructor}}{{>Constructor entityName=tscName}}{{/if~}}
}
{{/inline}}
import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne,OneToMany,OneToOne,PrimaryColumn,PrimaryGeneratedColumn,RelationId} from "typeorm";
{{#fileImports}}{{> Import}}{{/fileImports~}}