1- import fs from 'node:fs' ;
1+ import fsPromises from 'node:fs/promises ' ;
22import path from 'node:path' ;
33
44import type { Context } from '@hey-api/shared' ;
@@ -8,12 +8,19 @@ import { getTypedConfig } from '../config/utils';
88import { getClientPlugin } from '../plugins/@hey-api/client-core/utils' ;
99import { 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