1- import * as fs from 'fs' ;
2- import * as recursiveDir from 'mkdirp ' ;
1+ import { exists , mkdir , readFile , writeFile } from 'fs' ;
2+ import { ensureDir } from 'fs-extra ' ;
33import * as Mustache from 'mustache' ;
44import { dirname , join } from 'path' ;
55import { parse as swaggerFile } from 'swagger-parser' ;
@@ -10,8 +10,8 @@ import { createMustacheViewModel } from './parser';
1010
1111export async function generateAPIClient ( swaggerFilePath : string , outputPath : string ) : Promise < void > {
1212 /* Create output folder if not already present */
13- if ( ! await promisify ( fs . exists ) ( outputPath ) ) {
14- await promisify ( fs . mkdir ) ( outputPath ) ;
13+ if ( ! await promisify ( exists ) ( outputPath ) ) {
14+ await ensureDir ( outputPath ) ;
1515 }
1616
1717 const mustacheData = createMustacheViewModel ( await swaggerFile ( swaggerFilePath ) ) ;
@@ -23,43 +23,42 @@ export async function generateAPIClient(swaggerFilePath: string, outputPath: str
2323
2424async function generateClient ( viewContext : MustacheData , outputPath : string ) : Promise < void > {
2525 /* generate main API client class */
26- const clientTemplate = ( await promisify ( fs . readFile ) ( __dirname + ' /../templates/ngx-service.mustache' ) ) . toString ( ) ;
26+ const clientTemplate = ( await promisify ( readFile ) ( ` ${ __dirname } /../templates/ngx-service.mustache` ) ) . toString ( ) ;
2727
2828 const result = Mustache . render ( clientTemplate , viewContext ) ;
2929 const outfile = join ( outputPath , 'api-client.service.ts' ) ;
3030
31- await promisify ( fs . writeFile ) ( outfile , result , 'utf-8' ) ;
31+ await promisify ( writeFile ) ( outfile , result , 'utf-8' ) ;
3232}
3333
3434async function generateModels ( viewContext : MustacheData , outputPath : string ) : Promise < void > {
3535 const outputDir = join ( outputPath , 'models' ) ;
3636 const outIndexFile = join ( outputDir , '/index.ts' ) ;
3737
38- const modelTemplate = ( await promisify ( fs . readFile ) ( __dirname + ' /../templates/ngx-model.mustache' ) ) . toString ( ) ;
39- const modelExportTemplate = ( await promisify ( fs . readFile ) ( __dirname + ' /../templates/ngx-models-export.mustache' ) ) . toString ( ) ;
38+ const modelTemplate = ( await promisify ( readFile ) ( ` ${ __dirname } /../templates/ngx-model.mustache` ) ) . toString ( ) ;
39+ const modelExportTemplate = ( await promisify ( readFile ) ( ` ${ __dirname } /../templates/ngx-models-export.mustache` ) ) . toString ( ) ;
4040
41- if ( ! await promisify ( fs . exists ) ( outputDir ) ) {
42- await promisify ( fs . mkdir ) ( outputDir ) ;
41+ if ( ! await promisify ( exists ) ( outputDir ) ) {
42+ await promisify ( mkdir ) ( outputDir ) ;
4343 }
4444
4545 // generate model export index here
46- fs . writeFileSync ( outIndexFile , Mustache . render ( modelExportTemplate , viewContext ) , 'utf-8' ) ;
46+ await promisify ( writeFile ) ( outIndexFile , Mustache . render ( modelExportTemplate , viewContext ) , 'utf-8' ) ;
4747
4848 // generate API models
49- viewContext . definitions . forEach ( ( definition ) => {
49+ await Promise . all ( viewContext . definitions . map ( async ( definition ) => {
5050 const result = Mustache . render ( modelTemplate , definition ) ;
51- const outfile = join ( outputDir , fileName ( definition . name , definition . isEnum ? 'enum' : 'model' ) + ' .ts' ) ;
51+ const outfile = join ( outputDir , ` ${ fileName ( definition . name , definition . isEnum ? 'enum' : 'model' ) } .ts` ) ;
5252
53- recursiveDir ( dirname ( outfile ) , ( ) => {
54- fs . writeFileSync ( outfile , result , 'utf-8' ) ;
55- } ) ;
56- } ) ;
53+ await ensureDir ( dirname ( outfile ) ) ;
54+ return promisify ( writeFile ) ( outfile , result , 'utf-8' ) ;
55+ } ) ) ;
5756}
5857
5958async function generateModuleExportIndex ( viewContext : MustacheData , outputPath : string ) : Promise < void > {
60- const exportTemplate = ( await promisify ( fs . readFile ) ( __dirname + ' /../templates/ngx-module-export.mustache' ) ) . toString ( ) ;
59+ const exportTemplate = ( await promisify ( readFile ) ( ` ${ __dirname } /../templates/ngx-module-export.mustache` ) ) . toString ( ) ;
6160 const result = Mustache . render ( exportTemplate , viewContext ) ;
6261 const outfile = join ( outputPath , '/index.ts' ) ;
6362
64- fs . writeFileSync ( outfile , result , 'utf-8' ) ;
63+ await promisify ( writeFile ) ( outfile , result , 'utf-8' ) ;
6564}
0 commit comments