Skip to content

Commit 9aea60c

Browse files
committed
move logging to the new class
1 parent 7aa65de commit 9aea60c

File tree

1 file changed

+33
-60
lines changed

1 file changed

+33
-60
lines changed

src/openAPIGenerator.js

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,15 @@ const yaml = require("js-yaml");
55
const chalk = require("chalk");
66

77
const DefinitionGenerator = require("./definitionGenerator");
8+
const Logger = require("./logger");
89
const PostmanGenerator = require("openapi-to-postmanv2");
910

1011
class OpenAPIGenerator {
1112
constructor(serverless, options, { log = {} } = {}) {
1213
this.logOutput = log;
1314
this.serverless = serverless;
1415
this.options = options;
15-
16-
this.logTypes = {
17-
NOTICE: "notice",
18-
DEBUG: "debug",
19-
ERROR: "error",
20-
WARNING: "warning",
21-
INFO: "info",
22-
VERBOSE: "verbose",
23-
SUCCESS: "success",
24-
};
25-
26-
this.defaultLog = this.logTypes.NOTICE;
16+
this.logger = new Logger(this.serverless, this.logOutput);
2717

2818
this.commands = {
2919
openapi: {
@@ -145,7 +135,9 @@ class OpenAPIGenerator {
145135
}
146136

147137
async generate() {
148-
this.log(chalk.bold.underline("OpenAPI v3 Description Generation"));
138+
this.logger.notice(
139+
chalk.bold.underline("OpenAPI v3 Description Generation")
140+
);
149141
this.processCliInput();
150142

151143
const validOpenAPI = await this.generationAndValidation().catch((err) => {
@@ -168,37 +160,31 @@ class OpenAPIGenerator {
168160
}
169161
try {
170162
fs.writeFileSync(this.config.file, output);
171-
this.log(
172-
"OpenAPI v3 Description Successfully Written",
173-
this.logTypes.SUCCESS
174-
);
163+
this.logger.success("OpenAPI v3 Description Successfully Written");
175164
} catch (err) {
176-
this.log(
177-
`ERROR: An error was thrown whilst writing the OpenAPI Description`,
178-
this.logTypes.ERROR
165+
this.logger.error(
166+
`ERROR: An error was thrown whilst writing the OpenAPI Description`
179167
);
180168
throw new this.serverless.classes.Error(err);
181169
}
182170
}
183171

184172
async generationAndValidation() {
185-
const generator = new DefinitionGenerator(this.serverless);
173+
const generator = new DefinitionGenerator(this.serverless, this.logger);
186174

187-
this.log(`Generating OpenAPI Description`, this.logTypes.NOTICE);
175+
this.logger.notice(`Generating OpenAPI Description`);
188176
await generator.parse().catch((err) => {
189-
this.log(
190-
`ERROR: An error was thrown generating the OpenAPI v3 Description`,
191-
this.logTypes.ERROR
177+
this.logger.error(
178+
`ERROR: An error was thrown generating the OpenAPI v3 Description`
192179
);
193180
throw new this.serverless.classes.Error(err);
194181
});
195182

196-
this.log(`Validating generated OpenAPI Description`, this.logTypes.NOTICE);
183+
this.logger.notice(`Validating generated OpenAPI Description`);
197184

198185
const validationResults = await generator.validate().catch((err) => {
199-
this.log(
200-
`ERROR: An error was thrown validating the OpenAPI v3 Description`,
201-
this.logTypes.ERROR
186+
this.logger.error(
187+
`ERROR: An error was thrown validating the OpenAPI v3 Description`
202188
);
203189

204190
throw new this.serverless.classes.Error(err);
@@ -219,42 +205,37 @@ class OpenAPIGenerator {
219205
if (shouldThrow) throw new this.serverless.classes.Error(message);
220206
}
221207

222-
this.log(
223-
"OpenAPI v3 Description Successfully Generated",
224-
this.logTypes.SUCCESS
225-
);
208+
this.logger.success("OpenAPI v3 Description Successfully Generated");
226209

227210
return generator.openAPI;
228211
}
229212

230213
createPostman(openAPI) {
231214
const postmanGeneration = (err, result) => {
232215
if (err) {
233-
this.log(
234-
`ERROR: An error was thrown when generating the postman collection`,
235-
this.logTypes.ERROR
216+
this.logger.error(
217+
`ERROR: An error was thrown when generating the postman collection`
236218
);
237219
throw new this.serverless.classes.Error(err);
238220
}
239221

240-
this.log(
241-
"postman collection v2 Documentation Successfully Generated",
242-
this.logTypes.SUCCESS
222+
this.logger.success(
223+
"postman collection v2 Documentation Successfully Generated"
243224
);
225+
244226
try {
245227
fs.writeFileSync(
246228
this.config.postmanCollection,
247229
JSON.stringify(result.output[0].data)
248230
);
249-
this.log(
250-
"postman collection v2 Documentation Successfully Written",
251-
this.logTypes.SUCCESS
231+
this.logger.success(
232+
"postman collection v2 Documentation Successfully Written"
252233
);
253234
} catch (err) {
254-
this.log(
255-
`ERROR: An error was thrown whilst writing the postman collection`,
256-
this.logTypes.ERROR
235+
this.logger.error(
236+
`ERROR: An error was thrown whilst writing the postman collection`
257237
);
238+
258239
throw new this.serverless.classes.Error(err);
259240
}
260241
};
@@ -295,7 +276,7 @@ class OpenAPIGenerator {
295276
this.serverless.processedInput.options.output ||
296277
(config.format === "yaml" ? "openapi.yml" : "openapi.json");
297278

298-
this.log(
279+
this.logger.notice(
299280
`${chalk.bold.green("[OPTIONS]")}
300281
openApiVersion: "${chalk.bold.green(String(config.openApiVersion))}"
301282
format: "${chalk.bold.green(config.format)}"
@@ -314,26 +295,18 @@ class OpenAPIGenerator {
314295

315296
validationErrorDetails(validationErrors) {
316297
if (validationErrors.length) {
317-
this.log(
298+
this.logger.error(
318299
`${chalk.bold.yellow(
319300
"[VALIDATION]"
320-
)} Validation errors found in OpenAPI Description: \n`,
321-
this.logTypes.ERROR
301+
)} Validation errors found in OpenAPI Description: \n`
322302
);
323303

324304
for (const error of validationErrors) {
325-
this.log(
326-
`${chalk.bold.red("Severity:")} ${error.severity}`,
327-
this.logTypes.ERROR
328-
);
329-
this.log(
330-
`${chalk.bold.yellow("Message:")} ${error.message}`,
331-
this.logTypes.ERROR
332-
);
305+
this.logger.error(`${chalk.bold.red("Severity:")} ${error.severity}`);
306+
this.logger.error(`${chalk.bold.yellow("Message:")} ${error.message}`);
333307
for (const location of error.location) {
334-
this.log(
335-
`${chalk.bold.yellow("found at location:")} ${location.pointer}`,
336-
this.logTypes.ERROR
308+
this.logger.error(
309+
`${chalk.bold.yellow("found at location:")} ${location.pointer}`
337310
);
338311
}
339312
}

0 commit comments

Comments
 (0)