@@ -2,7 +2,7 @@ import * as c from 'chalk';
22import * as fs from 'fs' ;
33import * as YAML from 'js-yaml' ;
44import { DocumentGenerator } from './DocumentGenerator' ;
5- import { IConfigType } from './types' ;
5+ import { IConfigType , ILog } from './types' ;
66import { merge } from './utils' ;
77
88export class ServerlessOpenApiDocumentation {
@@ -62,6 +62,10 @@ export class ServerlessOpenApiDocumentation {
6262 } ;
6363 }
6464
65+ log : ILog = ( ...str : string [ ] ) => {
66+ process . stdout . write ( str . join ( ' ' ) ) ;
67+ }
68+
6569 /**
6670 * Processes CLI input by reading the input from serverless
6771 * @returns config IConfigType
@@ -83,22 +87,26 @@ export class ServerlessOpenApiDocumentation {
8387 config . file = this . serverless . processedInput . options . output ||
8488 ( ( config . format === 'yaml' ) ? 'openapi.yml' : 'openapi.json' ) ;
8589
86- process . stdout . write (
87- `${ c . bold . green ( '[OPTIONS]' ) } ` +
88- `format: "${ c . bold . red ( config . format ) } ", ` +
89- `output file: "${ c . bold . red ( config . file ) } ", ` +
90+ this . log (
91+ `${ c . bold . green ( '[OPTIONS]' ) } ` ,
92+ `format: "${ c . bold . red ( config . format ) } ",` ,
93+ `output file: "${ c . bold . red ( config . file ) } ",` ,
9094 `indentation: "${ c . bold . red ( String ( config . indent ) ) } "\n\n` ,
91- ) ;
95+ ) ;
96+
9297 return config ;
9398 }
9499
95100 /**
96101 * Generates OpenAPI Documentation based on serverless configuration and functions
97102 */
98103 private generate ( ) {
99- process . stdout . write ( c . bold . underline ( 'OpenAPI v3 Documentation Generator\n\n' ) ) ;
104+ this . log ( c . bold . underline ( 'OpenAPI v3 Documentation Generator\n\n' ) ) ;
100105 // Instantiate DocumentGenerator
101- const dg = new DocumentGenerator ( this . customVars . documentation ) ;
106+ const generator = new DocumentGenerator ( {
107+ serviceDescriptor : this . customVars . documentation ,
108+ log : this . log ,
109+ } ) ;
102110
103111 // Map function configurations
104112 const funcConfigs = this . serverless . service . getAllFunctions ( ) . map ( ( functionName ) => {
@@ -107,13 +115,13 @@ export class ServerlessOpenApiDocumentation {
107115 } ) ;
108116
109117 // Add Paths to OpenAPI Output from Function Configuration
110- dg . addPathsFromFunctionConfig ( funcConfigs ) ;
118+ generator . addPathsFromFunctionConfig ( funcConfigs ) ;
111119
112120 // Process CLI Input options
113121 const config = this . processCliInput ( ) ;
114122
115123 // Generate the resulting OpenAPI Object
116- const outputObject = dg . generate ( ) ;
124+ const outputObject = generator . generate ( ) ;
117125
118126 // Output the OpenAPI document to the correct format
119127 let outputContent = '' ;
@@ -129,6 +137,6 @@ export class ServerlessOpenApiDocumentation {
129137
130138 // Write to disk
131139 fs . writeFileSync ( config . file , outputContent ) ;
132- process . stdout . write ( `${ c . bold . green ( '[SUCCESS]' ) } Output file to "${ c . bold . red ( config . file ) } "\n` ) ;
140+ this . log ( `${ c . bold . green ( '[SUCCESS]' ) } Output file to "${ c . bold . red ( config . file ) } "\n` ) ;
133141 }
134142}
0 commit comments