Skip to content

Commit

Permalink
refactor: introduce helper functions for logging
Browse files Browse the repository at this point in the history
Ticket: DX-660
  • Loading branch information
anshchaturvedi committed Aug 8, 2024
1 parent c11dc0e commit 60218da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/openapi-generator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { KNOWN_IMPORTS } from './knownImports';
import { findSymbolInitializer } from './resolveInit';
import { parseCodecInitializer } from './codec';
import { SourceFile } from './sourceFile';
import { logError, logInfo, logWarn } from './error';

const app = command({
name: 'api-ts',
Expand Down Expand Up @@ -87,7 +88,7 @@ const app = command({
const codecFilePath = p.resolve(codecFile);
const codecModule = await import(codecFilePath);
if (codecModule.default === undefined) {
console.error(`[ERROR] Could not find default export in ${codecFilePath}`);
logError(`Could not find default export in ${codecFilePath}`);
process.exit(1);
}
const customCodecs = codecModule.default(E);
Expand All @@ -96,13 +97,13 @@ const app = command({

const project = await new Project({}, knownImports).parseEntryPoint(filePath);
if (E.isLeft(project)) {
console.error(`[ERROR] ${project.left}`);
logError(`${project.left}`);
process.exit(1);
}

const entryPoint = project.right.get(filePath);
if (entryPoint === undefined) {
console.error(`[ERROR] Could not find entry point ${filePath}`);
logError(`Could not find entry point ${filePath}`);
process.exit(1);
}

Expand All @@ -119,22 +120,20 @@ const app = command({
symbol.init.callee.type === 'Super' ||
symbol.init.callee.type === 'Import'
) {
console.error(
`[WARN] Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`,
);
logWarn(`Skipping ${symbol.name} because it is a ${symbol.init.callee.type}`);
continue;
} else if (!isApiSpec(entryPoint, symbol.init.callee)) {
continue;
}
console.error(`[INFO] Found API spec in ${symbol.name}`);
logInfo(`[INFO] Found API spec in ${symbol.name}`);

const result = parseApiSpec(
project.right,
entryPoint,
symbol.init.arguments[0]!.expression,
);
if (E.isLeft(result)) {
console.error(`[ERROR] Error when parsing ${symbol.name}: ${result.left}`);
logError(`Error when parsing ${symbol.name}: ${result.left}`);
process.exit(1);
}

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

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

Expand Down
13 changes: 13 additions & 0 deletions packages/openapi-generator/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ export function errorLeft(message: string): E.Either<string, never> {
export function stripStacktraceOfErrors(errors: string[]) {
return errors.map((e) => e!.split('\n')[0]);
}

// helper functions for logging
export function logError(message: string): void {
console.error(`[ERROR] ${message}`);
}

export function logWarn(message: string): void {
console.error(`[WARN] ${message}`);
}

export function logInfo(message: string): void {
console.error(`[INFO] ${message}`);
}

0 comments on commit 60218da

Please sign in to comment.