Skip to content

Commit a867ee9

Browse files
committed
feat(openapi-python): log codegen and write time
1 parent 6cdd106 commit a867ee9

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

packages/openapi-python/src/createClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export async function createClient({
166166
eventParser.timeEnd();
167167

168168
const eventGenerator = logger.timeEvent('generator');
169-
await generateOutput(context);
169+
const { codegenMs, fileCount, writeMs } = await generateOutput(context);
170170
eventGenerator.timeEnd();
171171

172172
const eventPostprocess = logger.timeEvent('postprocess');
@@ -179,7 +179,7 @@ export async function createClient({
179179
? `./${path.relative(process.env.INIT_CWD, config.output.path)}`
180180
: config.output.path;
181181
console.log(
182-
`${jobPrefix}${colors.green('✅ Done!')} Your output is in ${colors.cyanBright(outputPath)}`,
182+
`${jobPrefix}${colors.green('✅ Done!')} Your output is in ${colors.cyanBright(outputPath)} ${colors.gray(`(${fileCount} files, codegen ${(codegenMs / 1000).toFixed(2)}s, write ${(writeMs / 1000).toFixed(2)}s)`)}`,
183183
);
184184
}
185185
}

packages/openapi-python/src/generate/output.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'node:fs';
1+
import fsPromises from 'node:fs/promises';
22
import path from 'node:path';
33

44
import type { Context } from '@hey-api/shared';
@@ -8,12 +8,19 @@ import { getTypedConfig } from '../config/utils';
88
import { getClientPlugin } from '../plugins/@hey-api/client-core/utils';
99
import { generateClientBundle } from './client';
1010

11-
export async function generateOutput(context: Context): Promise<void> {
11+
export async function generateOutput(
12+
context: Context,
13+
): Promise<{ codegenMs: number; fileCount: number; writeMs: number }> {
1214
const outputPath = path.resolve(context.config.output.path);
1315

1416
if (context.config.output.clean) {
15-
if (fs.existsSync(outputPath)) {
16-
fs.rmSync(outputPath, { force: true, recursive: true });
17+
if (
18+
await fsPromises
19+
.access(outputPath)
20+
.then(() => true)
21+
.catch(() => false)
22+
) {
23+
await fsPromises.rm(outputPath, { force: true, recursive: true });
1724
}
1825
}
1926

@@ -32,6 +39,7 @@ export async function generateOutput(context: Context): Promise<void> {
3239
});
3340
}
3441

42+
const codegenStart = Date.now();
3543
for (const plugin of context.registerPlugins()) {
3644
await plugin.run();
3745
}
@@ -43,32 +51,46 @@ export async function generateOutput(context: Context): Promise<void> {
4351
await intent.run(ctx);
4452
}
4553

54+
let fileCount = 0;
55+
const writes: Promise<void>[] = [];
4656
for (const file of context.gen.render()) {
4757
const filePath = path.resolve(outputPath, file.path);
4858
const dir = path.dirname(filePath);
4959
if (!context.config.dryRun) {
50-
fs.mkdirSync(dir, { recursive: true });
51-
fs.writeFileSync(filePath, file.content, { encoding: 'utf8' });
60+
writes.push(
61+
fsPromises
62+
.mkdir(dir, { recursive: true })
63+
.then(() => fsPromises.writeFile(filePath, file.content, { encoding: 'utf8' })),
64+
);
5265
}
66+
fileCount++;
5367
}
68+
const codegenMs = Date.now() - codegenStart;
69+
70+
const writeStart = Date.now();
71+
await Promise.all(writes);
5472

5573
const { source } = context.config.output;
5674
if (source.enabled) {
5775
const sourcePath = source.path === null ? undefined : path.resolve(outputPath, source.path);
5876
if (!context.config.dryRun && sourcePath && sourcePath !== outputPath) {
59-
fs.mkdirSync(sourcePath, { recursive: true });
77+
await fsPromises.mkdir(sourcePath, { recursive: true });
6078
}
6179
const serialized = await source.serialize(context.spec);
6280
// TODO: handle yaml (convert before writing)
6381
if (!context.config.dryRun && sourcePath) {
64-
fs.writeFileSync(
82+
await fsPromises.writeFile(
6583
path.resolve(sourcePath, `${source.fileName}.${source.extension}`),
6684
serialized,
6785
{ encoding: 'utf8' },
6886
);
87+
fileCount++;
6988
}
7089
if (source.callback) {
7190
await source.callback(serialized);
7291
}
7392
}
93+
94+
const writeMs = Date.now() - writeStart;
95+
return { codegenMs, fileCount, writeMs };
7496
}

0 commit comments

Comments
 (0)