Skip to content

Commit

Permalink
Merge pull request #25 from JaredCE/improve-logging-output-for-2-and-3
Browse files Browse the repository at this point in the history
Improve logging output for 2 and 3
  • Loading branch information
JaredCE authored Jul 22, 2022
2 parents 5f30089 + c5223f6 commit 1fcfdfc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
.directory
.DS_STORE*
*.log
openapi.json
postman.json
44 changes: 26 additions & 18 deletions src/openAPIGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ class OpenAPIGenerator {
})
}

log(type = this.defaultLog, ...str) {
log(type = this.defaultLog, str) {
switch(this.serverless.version[0]) {
case '2':
this.serverless.cli.log(str)
let colouredString = str
if (type === 'error') {
colouredString = chalk.bold.red(`✖ ${str}`)
} else if (type === 'success') {
colouredString = chalk.bold.green(`✓ ${str}`)
}

this.serverless.cli.log(colouredString)
break

case '3':
Expand All @@ -106,32 +113,32 @@ class OpenAPIGenerator {

await generator.parse()
.catch(err => {
this.log('error', chalk.bold.red(`ERROR: An error was thrown generating the OpenAPI v3 documentation`))
this.log('error', `ERROR: An error was thrown generating the OpenAPI v3 documentation`)
throw new this.serverless.classes.Error(err)
})

const valid = await generator.validate()
.catch(err => {
this.log('error', chalk.bold.red(`ERROR: An error was thrown validating the OpenAPI v3 documentation`))
this.log('error', `ERROR: An error was thrown validating the OpenAPI v3 documentation`)
throw new this.serverless.classes.Error(err)
})

if (valid)
this.log(this.defaultLog, chalk.bold.green('OpenAPI v3 Documentation Successfully Generated'))
this.log('success', 'OpenAPI v3 Documentation Successfully Generated')

if (config.postmanCollection) {
const postmanGeneration = (err, result) => {
if (err) {
this.log('error', chalk.bold.red(`ERROR: An error was thrown when generating the postman collection`))
this.log('error', `ERROR: An error was thrown when generating the postman collection`)
throw new this.serverless.classes.Error(err)
}

this.log(this.defaultLog, chalk.bold.green('postman collection v2 Documentation Successfully Generated'))
this.log('success', 'postman collection v2 Documentation Successfully Generated')
try {
fs.writeFileSync(config.postmanCollection, JSON.stringify(result.output[0].data))
this.log(this.defaultLog, chalk.bold.green('postman collection v2 Documentation Successfully Written'))
this.log('success', 'postman collection v2 Documentation Successfully Written')
} catch (err) {
this.log('error', chalk.bold.red(`ERROR: An error was thrown whilst writing the postman collection`))
this.log('error', `ERROR: An error was thrown whilst writing the postman collection`)
throw new this.serverless.classes.Error(err)
}
}
Expand All @@ -155,9 +162,9 @@ class OpenAPIGenerator {
}
try {
fs.writeFileSync(config.file, output);
this.log(this.defaultLog, chalk.bold.green('OpenAPI v3 Documentation Successfully Written'))
this.log('success', 'OpenAPI v3 Documentation Successfully Written')
} catch (err) {
this.log('error', chalk.bold.red(`ERROR: An error was thrown whilst writing the openAPI Documentation`))
this.log('error', `ERROR: An error was thrown whilst writing the openAPI Documentation`)
throw new this.serverless.classes.Error(err)
}
}
Expand All @@ -177,20 +184,21 @@ class OpenAPIGenerator {
config.postmanCollection = this.serverless.processedInput.options.postmanCollection || null

if (['yaml', 'json'].indexOf(config.format.toLowerCase()) < 0) {
throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"');
// throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"');
throw new this.serverless.classes.Error('Invalid Output Format Specified - must be one of "yaml" or "json"')
}

config.file = this.serverless.processedInput.options.output ||
((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json');

this.log(
this.defaultLog,
`${chalk.bold.green('[OPTIONS]')}`,
` openApiVersion: "${chalk.bold.red(String(config.openApiVersion))}"`,
` format: "${chalk.bold.red(config.format)}"`,
` output file: "${chalk.bold.red(config.file)}"`,
` indentation: "${chalk.bold.red(String(config.indent))}"`,
` ${config.postmanCollection ? `postman collection: ${chalk.bold.red(config.postmanCollection)}`: `\n\n`}`
`${chalk.bold.green('[OPTIONS]')}
openApiVersion: "${chalk.bold.green(String(config.openApiVersion))}"
format: "${chalk.bold.green(config.format)}"
output file: "${chalk.bold.green(config.file)}"
indentation: "${chalk.bold.green(String(config.indent))}"
${config.postmanCollection ? `postman collection: ${chalk.bold.green(config.postmanCollection)}`: `\n\n`}`
)

return config
Expand Down
36 changes: 23 additions & 13 deletions test/serverless 3/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ functions:
handler: handler.create
events:
- http:
path: create
path: create/{username}
method: post
documentation:
summary: Create User
description: Creates a user and then sends a generated password email
tags:
- jesus
externalDocumentation:
url: https://bing.com
description: A link to bing
requestBody:
description: A user information object
requestModels:
Expand Down Expand Up @@ -54,25 +59,30 @@ functions:

custom:
documentation:
description: This is a description of what this does
version: 1.0.0
models:
- name: ErrorResponse
description: This is an error
contentType: application/json
schema: ${file(models/ErrorResponse.json)}
content:
application/json:
schema: ${file(../models/ErrorResponse.json)}

- name: PutDocumentResponse
description: PUT Document response model (external reference example)
contentType: application/json
schema: ${file(models/PutDocumentResponse.json)}
content:
application/json:
schema: ${file(../models/PutDocumentResponse.json)}

- name: PutDocumentRequest
description: PUT Document request model (inline example)
contentType: application/json
schema:
$schema: http://json-schema.org/draft-04/schema#
properties:
SomeObject:
type: object
content:
application/json:
schema:
$schema: http://json-schema.org/draft-04/schema#
properties:
SomeAttribute:
type: string
SomeObject:
type: object
properties:
SomeAttribute:
type: string

0 comments on commit 1fcfdfc

Please sign in to comment.