|
1 |
| -import * as chalk from 'chalk'; |
| 1 | +import chalk from 'chalk'; |
2 | 2 | import * as fs from 'fs';
|
3 | 3 | import * as YAML from 'js-yaml';
|
| 4 | +import _ = require('lodash'); |
| 5 | +import * as Serverless from 'serverless'; |
4 | 6 | import { inspect } from 'util';
|
| 7 | + |
5 | 8 | import { DefinitionGenerator } from './DefinitionGenerator';
|
6 |
| -import { IDefinitionType, ILog, Format, IDefinitionConfig } from './types'; |
7 |
| -import * as Serverless from 'serverless'; |
8 |
| -import _ = require('lodash'); |
| 9 | +import { Format, IDefinitionConfig, IDefinitionType, ILog } from './types'; |
9 | 10 |
|
10 |
| -interface Options { |
11 |
| - indent: number, |
12 |
| - format: Format, |
13 |
| - output: string, |
| 11 | +interface IOptions { |
| 12 | + indent: number; |
| 13 | + format: Format; |
| 14 | + output: string; |
14 | 15 | }
|
15 | 16 |
|
16 |
| -interface ProcessedInput { |
17 |
| - options: Options; |
| 17 | +interface IProcessedInput { |
| 18 | + options: IOptions; |
18 | 19 | }
|
19 | 20 |
|
20 |
| -interface CustomVars { |
| 21 | +interface ICustomVars { |
21 | 22 | documentation: IDefinitionConfig;
|
22 | 23 | }
|
23 | 24 |
|
24 |
| -interface Service { |
25 |
| - custom: CustomVars; |
| 25 | +interface IService { |
| 26 | + custom: ICustomVars; |
26 | 27 | }
|
27 | 28 |
|
28 |
| -interface Variables { |
29 |
| - service: Service |
| 29 | +interface IVariables { |
| 30 | + service: IService; |
30 | 31 | }
|
31 | 32 |
|
32 |
| -interface FullServerless extends Serverless { |
33 |
| - variables: Variables; |
34 |
| - processedInput: ProcessedInput; |
| 33 | +interface IFullServerless extends Serverless { |
| 34 | + variables: IVariables; |
| 35 | + processedInput: IProcessedInput; |
35 | 36 | }
|
36 | 37 |
|
37 | 38 | export class ServerlessOpenApiDocumentation {
|
38 | 39 | public hooks;
|
39 | 40 | public commands;
|
40 | 41 | /** Serverless Instance */
|
41 |
| - private serverless: FullServerless; |
| 42 | + private serverless: IFullServerless; |
42 | 43 |
|
43 | 44 | /** Serverless Service Custom vars */
|
44 |
| - private customVars: CustomVars; |
| 45 | + private customVars: ICustomVars; |
45 | 46 |
|
46 | 47 | /**
|
47 | 48 | * Constructor
|
48 | 49 | * @param serverless
|
49 | 50 | * @param options
|
50 | 51 | */
|
51 |
| - constructor (serverless: FullServerless, options) { |
| 52 | + constructor (serverless: IFullServerless, options) { |
52 | 53 |
|
53 | 54 | // pull the serverless instance into our class vars
|
54 | 55 | this.serverless = serverless;
|
@@ -93,37 +94,6 @@ export class ServerlessOpenApiDocumentation {
|
93 | 94 | process.stdout.write(str.join(' '));
|
94 | 95 | }
|
95 | 96 |
|
96 |
| - /** |
97 |
| - * Processes CLI input by reading the input from serverless |
98 |
| - * @returns config IConfigType |
99 |
| - */ |
100 |
| - private processCliInput (): IDefinitionType { |
101 |
| - const config: IDefinitionType = { |
102 |
| - format: Format.yaml, |
103 |
| - file: 'openapi.yml', |
104 |
| - indent: 2, |
105 |
| - }; |
106 |
| - |
107 |
| - config.indent = this.serverless.processedInput.options.indent || 2; |
108 |
| - config.format = this.serverless.processedInput.options.format || Format.yaml; |
109 |
| - |
110 |
| - if ([Format.yaml, Format.json].indexOf(config.format) < 0) { |
111 |
| - throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"'); |
112 |
| - } |
113 |
| - |
114 |
| - config.file = this.serverless.processedInput.options.output || |
115 |
| - ((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json'); |
116 |
| - |
117 |
| - this.log( |
118 |
| - `${chalk.bold.green('[OPTIONS]')}`, |
119 |
| - `format: "${chalk.bold.red(config.format)}",`, |
120 |
| - `output file: "${chalk.bold.red(config.file)}",`, |
121 |
| - `indentation: "${chalk.bold.red(String(config.indent))}"\n\n`, |
122 |
| - ); |
123 |
| - |
124 |
| - return config; |
125 |
| - } |
126 |
| - |
127 | 97 | /**
|
128 | 98 | * Generates OpenAPI Documentation based on serverless configuration and functions
|
129 | 99 | */
|
@@ -188,4 +158,35 @@ export class ServerlessOpenApiDocumentation {
|
188 | 158 |
|
189 | 159 | this.log(`${chalk.bold.green('[OUTPUT]')} To "${chalk.bold.red(config.file)}"\n`);
|
190 | 160 | }
|
| 161 | + |
| 162 | + /** |
| 163 | + * Processes CLI input by reading the input from serverless |
| 164 | + * @returns config IConfigType |
| 165 | + */ |
| 166 | + private processCliInput (): IDefinitionType { |
| 167 | + const config: IDefinitionType = { |
| 168 | + format: Format.yaml, |
| 169 | + file: 'openapi.yml', |
| 170 | + indent: 2, |
| 171 | + }; |
| 172 | + |
| 173 | + config.indent = this.serverless.processedInput.options.indent || 2; |
| 174 | + config.format = this.serverless.processedInput.options.format || Format.yaml; |
| 175 | + |
| 176 | + if ([Format.yaml, Format.json].indexOf(config.format) < 0) { |
| 177 | + throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"'); |
| 178 | + } |
| 179 | + |
| 180 | + config.file = this.serverless.processedInput.options.output || |
| 181 | + ((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json'); |
| 182 | + |
| 183 | + this.log( |
| 184 | + `${chalk.bold.green('[OPTIONS]')}`, |
| 185 | + `format: "${chalk.bold.red(config.format)}",`, |
| 186 | + `output file: "${chalk.bold.red(config.file)}",`, |
| 187 | + `indentation: "${chalk.bold.red(String(config.indent))}"\n\n`, |
| 188 | + ); |
| 189 | + |
| 190 | + return config; |
| 191 | + } |
191 | 192 | }
|
0 commit comments