Skip to content

Commit 60218da

Browse files
author
Ansh Chaturvedi
committed
refactor: introduce helper functions for logging
Ticket: DX-660
1 parent c11dc0e commit 60218da

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

packages/openapi-generator/src/cli.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { KNOWN_IMPORTS } from './knownImports';
1818
import { findSymbolInitializer } from './resolveInit';
1919
import { parseCodecInitializer } from './codec';
2020
import { SourceFile } from './sourceFile';
21+
import { logError, logInfo, logWarn } from './error';
2122

2223
const app = command({
2324
name: 'api-ts',
@@ -87,7 +88,7 @@ const app = command({
8788
const codecFilePath = p.resolve(codecFile);
8889
const codecModule = await import(codecFilePath);
8990
if (codecModule.default === undefined) {
90-
console.error(`[ERROR] Could not find default export in ${codecFilePath}`);
91+
logError(`Could not find default export in ${codecFilePath}`);
9192
process.exit(1);
9293
}
9394
const customCodecs = codecModule.default(E);
@@ -96,13 +97,13 @@ const app = command({
9697

9798
const project = await new Project({}, knownImports).parseEntryPoint(filePath);
9899
if (E.isLeft(project)) {
99-
console.error(`[ERROR] ${project.left}`);
100+
logError(`${project.left}`);
100101
process.exit(1);
101102
}
102103

103104
const entryPoint = project.right.get(filePath);
104105
if (entryPoint === undefined) {
105-
console.error(`[ERROR] Could not find entry point ${filePath}`);
106+
logError(`Could not find entry point ${filePath}`);
106107
process.exit(1);
107108
}
108109

@@ -119,22 +120,20 @@ const app = command({
119120
symbol.init.callee.type === 'Super' ||
120121
symbol.init.callee.type === 'Import'
121122
) {
122-
console.error(
123-
`[WARN] Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`,
124-
);
123+
logWarn(`Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`);
125124
continue;
126125
} else if (!isApiSpec(entryPoint, symbol.init.callee)) {
127126
continue;
128127
}
129-
console.error(`[INFO] Found API spec in ${symbol.name}`);
128+
logInfo(`[INFO] Found API spec in ${symbol.name}`);
130129

131130
const result = parseApiSpec(
132131
project.right,
133132
entryPoint,
134133
symbol.init.arguments[0]!.expression,
135134
);
136135
if (E.isLeft(result)) {
137-
console.error(`[ERROR] Error when parsing ${symbol.name}: ${result.left}`);
136+
logError(`Error when parsing ${symbol.name}: ${result.left}`);
138137
process.exit(1);
139138
}
140139

@@ -145,7 +144,7 @@ const app = command({
145144
apiSpec.push(...result.right);
146145
}
147146
if (apiSpec.length === 0) {
148-
console.error(`[ERROR] Could not find API spec in ${filePath}`);
147+
logError(`Could not find API spec in ${filePath}`);
149148
process.exit(1);
150149
}
151150

@@ -166,7 +165,7 @@ const app = command({
166165
}
167166
const sourceFile = project.right.get(ref.location);
168167
if (sourceFile === undefined) {
169-
console.error(`[ERROR] Could not find '${ref.name}' from '${ref.location}'`);
168+
logError(`Could not find '${ref.name}' from '${ref.location}'`);
170169
process.exit(1);
171170
}
172171

packages/openapi-generator/src/error.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ export function errorLeft(message: string): E.Either<string, never> {
2020
export function stripStacktraceOfErrors(errors: string[]) {
2121
return errors.map((e) => e!.split('\n')[0]);
2222
}
23+
24+
// helper functions for logging
25+
export function logError(message: string): void {
26+
console.error(`[ERROR] ${message}`);
27+
}
28+
29+
export function logWarn(message: string): void {
30+
console.error(`[WARN] ${message}`);
31+
}
32+
33+
export function logInfo(message: string): void {
34+
console.error(`[INFO] ${message}`);
35+
}

0 commit comments

Comments
 (0)