@@ -2,7 +2,7 @@ import * as c from 'chalk';
2
2
import * as fs from 'fs' ;
3
3
import * as YAML from 'js-yaml' ;
4
4
import { DocumentGenerator } from './DocumentGenerator' ;
5
- import { IConfigType } from './types' ;
5
+ import { IConfigType , ILog } from './types' ;
6
6
import { merge } from './utils' ;
7
7
8
8
export class ServerlessOpenApiDocumentation {
@@ -62,6 +62,10 @@ export class ServerlessOpenApiDocumentation {
62
62
} ;
63
63
}
64
64
65
+ log : ILog = ( ...str : string [ ] ) => {
66
+ process . stdout . write ( str . join ( ' ' ) ) ;
67
+ }
68
+
65
69
/**
66
70
* Processes CLI input by reading the input from serverless
67
71
* @returns config IConfigType
@@ -83,22 +87,26 @@ export class ServerlessOpenApiDocumentation {
83
87
config . file = this . serverless . processedInput . options . output ||
84
88
( ( config . format === 'yaml' ) ? 'openapi.yml' : 'openapi.json' ) ;
85
89
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 ) } ",` ,
90
94
`indentation: "${ c . bold . red ( String ( config . indent ) ) } "\n\n` ,
91
- ) ;
95
+ ) ;
96
+
92
97
return config ;
93
98
}
94
99
95
100
/**
96
101
* Generates OpenAPI Documentation based on serverless configuration and functions
97
102
*/
98
103
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' ) ) ;
100
105
// 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
+ } ) ;
102
110
103
111
// Map function configurations
104
112
const funcConfigs = this . serverless . service . getAllFunctions ( ) . map ( ( functionName ) => {
@@ -107,13 +115,13 @@ export class ServerlessOpenApiDocumentation {
107
115
} ) ;
108
116
109
117
// Add Paths to OpenAPI Output from Function Configuration
110
- dg . addPathsFromFunctionConfig ( funcConfigs ) ;
118
+ generator . addPathsFromFunctionConfig ( funcConfigs ) ;
111
119
112
120
// Process CLI Input options
113
121
const config = this . processCliInput ( ) ;
114
122
115
123
// Generate the resulting OpenAPI Object
116
- const outputObject = dg . generate ( ) ;
124
+ const outputObject = generator . generate ( ) ;
117
125
118
126
// Output the OpenAPI document to the correct format
119
127
let outputContent = '' ;
@@ -129,6 +137,6 @@ export class ServerlessOpenApiDocumentation {
129
137
130
138
// Write to disk
131
139
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` ) ;
133
141
}
134
142
}
0 commit comments