diff --git a/schemas/config.json b/schemas/config.json index f2b9311..7e6cb1d 100644 --- a/schemas/config.json +++ b/schemas/config.json @@ -29,6 +29,9 @@ "entry": { "type": "string" }, + "entryTypeName": { + "type": "string" + }, "exclude": { "items": { "type": "string" diff --git a/src/cli/commands/generate.ts b/src/cli/commands/generate.ts index 84e11f5..edc425d 100644 --- a/src/cli/commands/generate.ts +++ b/src/cli/commands/generate.ts @@ -1,7 +1,7 @@ import type Yargs from "yargs"; import * as fs from "fs"; import * as path from "path"; -import {Project, InterfaceDeclaration} from "ts-morph"; +import {Project, InterfaceDeclaration, SourceFile} from "ts-morph"; import * as TJS from "typescript-json-schema"; import {makeLogger} from "../../log"; import config, {Config} from "../config"; @@ -20,6 +20,16 @@ type ResourcesMeta = Config["resources"] & { }; }; +function findEntryTypeName(sourceFile: SourceFile): string { + const types = sourceFile.getTypeAliases(); + for (let t of types) { + if (t.isExported() && t.getType().isUnion()) { + return t.getName(); + } + } + throw new Error("No exported union type alias found"); +} + export const generateCommand = (yargs: typeof Yargs) => { return yargs.command( "generate", @@ -40,7 +50,11 @@ export const generateCommand = (yargs: typeof Yargs) => { const dependenciesMap: {[key: string]: InterfaceDeclaration} = {}; - const mainType = mainSource.getTypeAliasOrThrow("Services").getType(); + const typeAliasName = config.resources?.entryTypeName ?? findEntryTypeName(mainSource); + + log.info("Using exported type", typeAliasName); + + const mainType = mainSource.getTypeAliasOrThrow(typeAliasName).getType(); const declaredServices = ( mainType.isUnion() @@ -86,7 +100,7 @@ export const generateCommand = (yargs: typeof Yargs) => { const program = TJS.getProgramFromFiles(project.getSourceFiles().map((x) => x.getFilePath())); log.debug("generating schema"); - let schema = TJS.generateSchema(program, "Services", { + let schema = TJS.generateSchema(program, typeAliasName, { excludePrivate: true, propOrder: true, ignoreErrors: true, diff --git a/src/cli/config.ts b/src/cli/config.ts index 2dbc31e..361a43e 100644 --- a/src/cli/config.ts +++ b/src/cli/config.ts @@ -14,6 +14,7 @@ export interface Config { [name: string]: string; }; resources?: { + entryTypeName?: string; include?: string; exclude?: string[]; output: ConfigOutputs;