|
| 1 | +import { File, GeneratorNotificationService } from "@fern-api/base-generator"; |
| 2 | +import { RelativeFilePath } from "@fern-api/fs-utils"; |
| 3 | +import { AbstractJavaGeneratorCli } from "@fern-api/java-base"; |
| 4 | + |
| 5 | +import { FernGeneratorExec } from "@fern-fern/generator-exec-sdk"; |
| 6 | +import { Endpoint } from "@fern-fern/generator-exec-sdk/api"; |
| 7 | +import { IntermediateRepresentation } from "@fern-fern/ir-sdk/api"; |
| 8 | + |
| 9 | +import { SdkCustomConfigSchema } from "./SdkCustomConfig"; |
| 10 | +import { SdkGeneratorContext } from "./SdkGeneratorContext"; |
| 11 | + |
| 12 | +export class SdkGeneratorCLI extends AbstractJavaGeneratorCli<SdkCustomConfigSchema, SdkGeneratorContext> { |
| 13 | + protected constructContext({ |
| 14 | + ir, |
| 15 | + customConfig, |
| 16 | + generatorConfig, |
| 17 | + generatorNotificationService |
| 18 | + }: { |
| 19 | + ir: IntermediateRepresentation; |
| 20 | + customConfig: SdkCustomConfigSchema; |
| 21 | + generatorConfig: FernGeneratorExec.GeneratorConfig; |
| 22 | + generatorNotificationService: GeneratorNotificationService; |
| 23 | + }): SdkGeneratorContext { |
| 24 | + return new SdkGeneratorContext(ir, generatorConfig, customConfig, generatorNotificationService); |
| 25 | + } |
| 26 | + |
| 27 | + protected parseCustomConfigOrThrow(customConfig: unknown): SdkCustomConfigSchema { |
| 28 | + const parsed = customConfig != null ? SdkCustomConfigSchema.parse(customConfig) : undefined; |
| 29 | + if (parsed != null) { |
| 30 | + return parsed; |
| 31 | + } |
| 32 | + return {}; |
| 33 | + } |
| 34 | + |
| 35 | + protected async publishPackage(context: SdkGeneratorContext): Promise<void> { |
| 36 | + await this.generate(context); |
| 37 | + } |
| 38 | + |
| 39 | + protected async writeForGithub(context: SdkGeneratorContext): Promise<void> { |
| 40 | + await this.generate(context); |
| 41 | + } |
| 42 | + |
| 43 | + protected async writeForDownload(context: SdkGeneratorContext): Promise<void> { |
| 44 | + await this.generate(context); |
| 45 | + } |
| 46 | + |
| 47 | + protected async generate(context: SdkGeneratorContext): Promise<void> { |
| 48 | + if (context.config.output.snippetFilepath != null) { |
| 49 | + try { |
| 50 | + await this.generateReadme({ |
| 51 | + context, |
| 52 | + // TODO(ajgateno): Support and require snippets |
| 53 | + endpointSnippets: [] |
| 54 | + }); |
| 55 | + } catch (e) { |
| 56 | + context.logger.warn("Failed to generate README.md, this is OK."); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + await context.project.persist(); |
| 61 | + } |
| 62 | + |
| 63 | + private async generateReadme({ |
| 64 | + context, |
| 65 | + endpointSnippets |
| 66 | + }: { |
| 67 | + context: SdkGeneratorContext; |
| 68 | + endpointSnippets: Endpoint[]; |
| 69 | + }): Promise<void> { |
| 70 | + const content = await context.generatorAgent.generateReadme({ context, endpointSnippets }); |
| 71 | + context.project.addRawFiles( |
| 72 | + new File(context.generatorAgent.README_FILENAME, RelativeFilePath.of("."), content) |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments