openapi-typescript-codegen/src/utils/readSpecFromDisk.ts
2020-09-24 22:46:04 +02:00

22 lines
670 B
TypeScript

import * as path from 'path';
import { exists, readFile } from './fileSystem';
/**
* Check if given file exists and try to read the content as string.
* @param input
*/
export async function readSpecFromDisk(input: string): Promise<string> {
const filePath = path.resolve(process.cwd(), input);
const fileExists = await exists(filePath);
if (fileExists) {
try {
const content = await readFile(filePath, 'utf8');
return content.toString();
} catch (e) {
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
}
}
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
}