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 header: config.output.header,
28 outputPath,
29 // @ts-expect-error
30 plugin: client,
31 project: context.gen,
32 });
33 }
34
35 for (const plugin of context.registerPlugins()) {
36 await plugin.run();
37 }
38
39 context.gen.plan();
40
41 const ctx = new IntentContext(context.spec);
42 for (const intent of context.intents) {
43 await intent.run(ctx);
44 }
45
46 for (const file of context.gen.render()) {
47 const filePath = path.resolve(outputPath, file.path);
48 const dir = path.dirname(filePath);
49 if (!context.config.dryRun) {
50 fs.mkdirSync(dir, { recursive: true });
51 fs.writeFileSync(filePath, file.content, { encoding: 'utf8' });
52 }
53 }
54
55 const { source } = context.config.output;
56 if (source.enabled) {
57 const sourcePath = source.path === null ? undefined : path.resolve(outputPath, source.path);
58 if (!context.config.dryRun && sourcePath && sourcePath !== outputPath) {
59 fs.mkdirSync(sourcePath, { recursive: true });
60 }
61 const serialized = await source.serialize(context.spec);
62 // TODO: handle yaml (convert before writing)
63 if (!context.config.dryRun && sourcePath) {
64 fs.writeFileSync(
65 path.resolve(sourcePath, `${source.fileName}.${source.extension}`),
66 serialized,
67 { encoding: 'utf8' },
68 );
69 }
70 if (source.callback) {
71 await source.callback(serialized);
72 }
73 }
74}