ability to generate models with default exports

(instead of name exports)
This commit is contained in:
Kononnable 2019-12-25 20:35:38 +01:00
parent dc4208ad0f
commit 88d3947d32
6 changed files with 69 additions and 8 deletions

View File

@ -19,6 +19,7 @@ export default interface IGenerationOptions {
strictMode: "none" | "?" | "!";
skipSchema: boolean;
indexFile: boolean;
exportType: "named" | "default";
}
export function getDefaultGenerationOptions(): IGenerationOptions {
const generationOptions: IGenerationOptions = {
@ -36,7 +37,8 @@ export function getDefaultGenerationOptions(): IGenerationOptions {
relationIds: false,
strictMode: "none",
skipSchema: false,
indexFile: false
indexFile: false,
exportType: "named"
};
return generationOptions;
}

View File

@ -211,6 +211,14 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions): void {
return retVal;
}
);
Handlebars.registerHelper("defaultExport", () =>
generationOptions.exportType === "default" ? "default" : ""
);
Handlebars.registerHelper("localImport", (entityName: string) =>
generationOptions.exportType === "default"
? entityName
: `{${entityName}}`
);
Handlebars.registerHelper("strictMode", () =>
generationOptions.strictMode !== "none"
? generationOptions.strictMode

View File

@ -260,6 +260,11 @@ function checkYargsParameters(options: options): options {
boolean: true,
default: options.generationOptions.indexFile,
describe: "Generate index file"
},
defaultExport: {
boolean: true,
default: options.generationOptions.exportType === "default",
describe: "Generate index file"
}
});
@ -293,6 +298,9 @@ function checkYargsParameters(options: options): options {
options.generationOptions.pluralizeNames = !argv.disablePluralization;
options.generationOptions.strictMode = argv.strictMode as IGenerationOptions["strictMode"];
options.generationOptions.indexFile = argv.index;
options.generationOptions.exportType = argv.defaultExport
? "default"
: "named";
return options;
}
@ -513,6 +521,13 @@ async function useInquirer(options: options): Promise<options> {
name: "Generate index file",
value: "index",
checked: options.generationOptions.indexFile
},
{
name: "Prefer default exports",
value: "defaultExport",
checked:
options.generationOptions.exportType ===
"default"
}
],
message: "Available customizations",
@ -567,6 +582,11 @@ async function useInquirer(options: options): Promise<options> {
"constructor"
);
options.generationOptions.indexFile = customizations.includes("index");
options.generationOptions.exportType = customizations.includes(
"defaultExport"
)
? "default"
: "named";
if (customizations.includes("namingStrategy")) {
const namingStrategyPath = (

View File

@ -2,7 +2,7 @@
@Index("{{name}}",[{{#columns}}"{{toPropertyName .}}",{{/columns~}}],{ {{json options}} })
{{/inline}}
{{#*inline "Import"}}
import { {{toEntityName .}} } from './{{toFileName .}}'
import {{localImport (toEntityName .)}} from './{{toFileName .}}'
{{/inline}}
{{#*inline "Column"}}
{{#generated}}@PrimaryGeneratedColumn({ type:"{{type}}", {{/generated}}{{^generated}}@Column("{{type}}",{ {{#primary}}primary:{{primary}},{{/primary}}{{/generated}}{{json options}}{{#default}},default: {{.}},{{/default}} })
@ -33,7 +33,7 @@ import { {{toEntityName .}} } from './{{toFileName .}}'
{{#*inline "Entity"}}
{{#indices}}{{> Index}}{{/indices~}}
@Entity("{{sqlName}}"{{#schema}} ,{schema:"{{.}}"{{#if ../database}}, database:"{{../database}}"{{/if}} } {{/schema}})
export class {{toEntityName tscName}}{{#activeRecord}} extends BaseEntity{{/activeRecord}} {
export {{defaultExport}} class {{toEntityName tscName}}{{#activeRecord}} extends BaseEntity{{/activeRecord}} {
{{#columns}}{{> Column}}{{/columns~}}
{{#relations}}{{> Relation}}{{/relations~}}

View File

@ -1,3 +1,5 @@
{{#entities~}}
export { {{toEntityName tscName}} } from './{{toFileName tscName}}'
{{/entities~}}
import {{localImport (toEntityName tscName)}} from './{{toFileName tscName}}'
{{/entities}}
export { {{#entities}}{{toEntityName tscName}},{{/entities~}} }

View File

@ -666,7 +666,7 @@ describe("Model customization phase", async () => {
})
})
describe("index file generation", () => {
it("enabled", async () => {
it("named export", async () => {
const data = generateSampleData();
const generationOptions = generateGenerationOptions();
@ -687,8 +687,36 @@ describe("Model customization phase", async () => {
const indexFileContent = fs
.readFileSync(path.resolve(filesGenPath, "Index.ts"))
.toString();
expect(indexFileContent).to.contain('export { Post } from "./Post";');
expect(indexFileContent).to.contain('export { PostAuthor } from "./PostAuthor";');
expect(indexFileContent).to.contain('import { PostAuthor } from "./PostAuthor');
expect(indexFileContent).to.contain('import { Post } from "./Post');
expect(indexFileContent).to.contain('export { PostAuthor, Post }');
compileGeneratedModel(generationOptions.resultsPath, [""]);
})
it("default export", async () => {
const data = generateSampleData();
const generationOptions = generateGenerationOptions();
generationOptions.indexFile = true;
generationOptions.exportType = "default"
clearGenerationDir();
const customizedModel = modelCustomizationPhase(
data,
generationOptions,
{}
);
modelGenerationPhase(
getDefaultConnectionOptions(),
generationOptions,
customizedModel
);
const filesGenPath = path.resolve(resultsPath, "entities");
const indexFileContent = fs
.readFileSync(path.resolve(filesGenPath, "Index.ts"))
.toString();
expect(indexFileContent).to.contain('import PostAuthor from "./PostAuthor');
expect(indexFileContent).to.contain('import Post from "./Post');
expect(indexFileContent).to.contain('export { PostAuthor, Post }');
compileGeneratedModel(generationOptions.resultsPath, [""]);
})
it("disabled", async () => {
@ -713,4 +741,5 @@ describe("Model customization phase", async () => {
compileGeneratedModel(generationOptions.resultsPath, [""]);
})
})
});