fork of hey-api/openapi-ts because I need some additional things
1import fs from 'node:fs';
2import path from 'node:path';
3
4import type { Context } from '@hey-api/shared';
5import { IntentContext } from '@hey-api/shared';
6
7import { getTypedConfig } from '../config/utils';
8import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
9import { generateClientBundle } from './client';
10
11export async function generateOutput(context: Context): Promise<void> {
12 const outputPath = path.resolve(context.config.output.path);
13
14 if (context.config.output.clean) {
15 if (fs.existsSync(outputPath)) {
16 fs.rmSync(outputPath, { force: true, recursive: true });
17 }
18 }
19
20 const config = getTypedConfig(context);
21
22 const client = getClientPlugin(config);
23 if ('bundle' in client.config && client.config.bundle && !config.dryRun) {
24 // not proud of this one
25 // @ts-expect-error
26 config._FRAGILE_CLIENT_BUNDLE_RENAMED = generateClientBundle({
27 meta: {
28 importFileExtension: config.output.importFileExtension,
29 },
30 outputPath,
31 // @ts-expect-error
32 plugin: client,
33 project: context.gen,
34 });
35 }
36
37 for (const plugin of context.registerPlugins()) {
38 await plugin.run();
39 }
40
41 context.gen.plan();
42
43 const ctx = new IntentContext(context.spec);
44 for (const intent of context.intents) {
45 await intent.run(ctx);
46 }
47
48 for (const file of context.gen.render()) {
49 const filePath = path.resolve(outputPath, file.path);
50 const dir = path.dirname(filePath);
51 if (!context.config.dryRun) {
52 fs.mkdirSync(dir, { recursive: true });
53 fs.writeFileSync(filePath, file.content, { encoding: 'utf8' });
54 }
55 }
56
57 const { source } = context.config.output;
58 if (source.enabled) {
59 const sourcePath = source.path === null ? undefined : path.resolve(outputPath, source.path);
60 if (!context.config.dryRun && sourcePath && sourcePath !== outputPath) {
61 fs.mkdirSync(sourcePath, { recursive: true });
62 }
63 const serialized = await source.serialize(context.spec);
64 // TODO: handle yaml (convert before writing)
65 if (!context.config.dryRun && sourcePath) {
66 fs.writeFileSync(
67 path.resolve(sourcePath, `${source.fileName}.${source.extension}`),
68 serialized,
69 { encoding: 'utf8' },
70 );
71 }
72 if (source.callback) {
73 await source.callback(serialized);
74 }
75 }
76}