|
| 1 | +import * as c from 'chalk'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as YAML from 'js-yaml'; |
| 4 | +import { DocumentGenerator } from './DocumentGenerator'; |
| 5 | +import { IConfigType } from './types'; |
| 6 | +import { merge } from './utils'; |
| 7 | + |
| 8 | +export class ServerlessOpenApiDocumentation { |
| 9 | + public hooks; |
| 10 | + public commands; |
| 11 | + /** Serverless Instance */ |
| 12 | + private serverless; |
| 13 | + /** CLI options */ |
| 14 | + private options; |
| 15 | + /** Serverless Service Custom vars */ |
| 16 | + private customVars; |
| 17 | + |
| 18 | + /** |
| 19 | + * Constructor |
| 20 | + * @param serverless |
| 21 | + * @param options |
| 22 | + */ |
| 23 | + constructor (serverless, options) { |
| 24 | + // pull the serverless instance into our class vars |
| 25 | + this.serverless = serverless; |
| 26 | + // pull the CLI options into our class vars |
| 27 | + this.options = options; |
| 28 | + // Serverless service custom variables |
| 29 | + this.customVars = this.serverless.variables.service.custom; |
| 30 | + |
| 31 | + // Declare the commands this plugin exposes for the Serverless CLI |
| 32 | + this.commands = { |
| 33 | + openapi: { |
| 34 | + commands: { |
| 35 | + generate: { |
| 36 | + lifecycleEvents: [ |
| 37 | + 'serverless', |
| 38 | + ], |
| 39 | + usage: 'Generate OpenAPI v3 Documentation', |
| 40 | + options: { |
| 41 | + output: { |
| 42 | + usage: 'Output file location [default: openapi.yml|json]', |
| 43 | + shortcut: 'o', |
| 44 | + }, |
| 45 | + format: { |
| 46 | + usage: 'OpenAPI file format (yml|json) [default: yml]', |
| 47 | + shortcut: 'f', |
| 48 | + }, |
| 49 | + indent: { |
| 50 | + usage: 'File indentation in spaces[default: 2]', |
| 51 | + shortcut: 'i', |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + // Declare the hooks our plugin is interested in |
| 60 | + this.hooks = { |
| 61 | + 'openapi:generate:serverless': this.generate.bind(this), |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Processes CLI input by reading the input from serverless |
| 67 | + * @returns config IConfigType |
| 68 | + */ |
| 69 | + private processCliInput (): IConfigType { |
| 70 | + const config: IConfigType = { |
| 71 | + format: 'yaml', |
| 72 | + file: 'openapi.yml', |
| 73 | + indent: 2, |
| 74 | + }; |
| 75 | + |
| 76 | + config.indent = this.serverless.processedInput.options.indent || 2; |
| 77 | + config.format = this.serverless.processedInput.options.format || 'yaml'; |
| 78 | + |
| 79 | + if (['yaml', 'json'].indexOf(config.format.toLowerCase()) < 0) { |
| 80 | + throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"'); |
| 81 | + } |
| 82 | + |
| 83 | + config.file = this.serverless.processedInput.options.output || |
| 84 | + ((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json'); |
| 85 | + |
| 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 | + `indentation: "${c.bold.red(String(config.indent))}"\n\n`, |
| 91 | + ); |
| 92 | + return config; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Generates OpenAPI Documentation based on serverless configuration and functions |
| 97 | + */ |
| 98 | + private generate () { |
| 99 | + process.stdout.write(c.bold.underline('OpenAPI v3 Documentation Generator\n\n')); |
| 100 | + // Instantiate DocumentGenerator |
| 101 | + const dg = new DocumentGenerator(this.customVars.documentation); |
| 102 | + |
| 103 | + // Map function configurations |
| 104 | + const funcConfigs = this.serverless.service.getAllFunctions().map((functionName) => { |
| 105 | + const func = this.serverless.service.getFunction(functionName); |
| 106 | + return merge({ _functionName: functionName }, func); |
| 107 | + }); |
| 108 | + |
| 109 | + // Add Paths to OpenAPI Output from Function Configuration |
| 110 | + dg.addPathsFromFunctionConfig(funcConfigs); |
| 111 | + |
| 112 | + // Process CLI Input options |
| 113 | + const config = this.processCliInput(); |
| 114 | + |
| 115 | + // Generate the resulting OpenAPI Object |
| 116 | + const outputObject = dg.generate(); |
| 117 | + |
| 118 | + // Output the OpenAPI document to the correct format |
| 119 | + let outputContent = ''; |
| 120 | + switch (config.format.toLowerCase()) { |
| 121 | + case 'json': |
| 122 | + outputContent = JSON.stringify(outputObject, null, config.indent); |
| 123 | + break; |
| 124 | + case 'yaml': |
| 125 | + default: |
| 126 | + outputContent = YAML.safeDump(outputObject, { indent: config.indent }); |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + // Write to disk |
| 131 | + fs.writeFileSync(config.file, outputContent); |
| 132 | + process.stdout.write(`${ c.bold.green('[SUCCESS]') } Output file to "${c.bold.red(config.file)}"\n`); |
| 133 | + } |
| 134 | +} |
0 commit comments