Skip to content

Commit 49376ff

Browse files
committed
Refactor logging
1 parent 1337b1f commit 49376ff

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/DocumentGenerator.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as c from 'chalk';
33
import * as openApiValidator from 'swagger2openapi/validate';
44

55
import * as uuid from 'uuid';
6-
import { IParameterConfig, IServerlessFunctionConfig, IServiceDescription } from './types';
6+
import { ILog, IParameterConfig, IServerlessFunctionConfig, IServiceDescription } from './types';
77
import { clone, merge } from './utils';
88

99
export class DocumentGenerator {
@@ -23,13 +23,18 @@ export class DocumentGenerator {
2323
};
2424

2525
private serviceDescriptor: IServiceDescription;
26+
private log: ILog;
2627

2728
/**
2829
* Constructor
2930
* @param serviceDescriptor IServiceDescription
3031
*/
31-
constructor (serviceDescriptor: IServiceDescription) {
32+
constructor ({ log, serviceDescriptor }: {
33+
log: ILog,
34+
serviceDescriptor: IServiceDescription,
35+
}) {
3236
this.serviceDescriptor = clone(serviceDescriptor);
37+
this.log = log;
3338

3439
merge(this.config, {
3540
openapi: this.openapiVersion,
@@ -53,17 +58,21 @@ export class DocumentGenerator {
5358

5459
public generate () {
5560
const result: any = {};
56-
process.stdout.write(`${ c.bold.yellow('[VALIDATION]') } Validating OpenAPI generated output\n`);
61+
this.log(`${ c.bold.yellow('[VALIDATION]') } Validating OpenAPI generated output\n`);
62+
5763
try {
5864
openApiValidator.validateSync(this.config, result);
59-
process.stdout.write(`${ c.bold.green('[VALIDATION]') } OpenAPI valid: ${c.bold.green('true')}\n\n`);
65+
66+
this.log(`${ c.bold.green('[VALIDATION]') } OpenAPI valid: ${c.bold.green('true')}\n\n`);
67+
6068
return this.config;
6169
} catch (e) {
62-
process.stdout.write(
70+
this.log(
6371
`${c.bold.red('[VALIDATION]')} Failed to validate OpenAPI document: \n\n${c.yellow(e.message)}\n\n` +
64-
`${c.bold.green('Path:')} ${result.valid} ${result.context}\n`,
65-
);
66-
// throw new Error('Failed to validate OpenAPI document');
72+
`${c.bold.green('Path:')} ${JSON.stringify(result, null, 2)}\n`,
73+
);
74+
75+
throw new Error('Failed to validate OpenAPI document');
6776
}
6877
}
6978

src/ServerlessOpenApiDocumentation.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as c from 'chalk';
22
import * as fs from 'fs';
33
import * as YAML from 'js-yaml';
44
import { DocumentGenerator } from './DocumentGenerator';
5-
import { IConfigType } from './types';
5+
import { IConfigType, ILog } from './types';
66
import { merge } from './utils';
77

88
export 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
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ export interface IParameterConfig {
4343
example?: any;
4444
examples?: [any];
4545
}
46+
47+
export type ILog = (...str: string[]) => void;

0 commit comments

Comments
 (0)