Ferdi Koomen 30e26621a5 - Added core template files
- Working on v2 parsing
2019-11-07 01:17:39 +01:00

47 lines
1.3 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import * as yaml from 'js-yaml';
/**
* Check if given file exists and try to read the content as string.
* @param filePath
*/
function read(filePath: string): string {
if (fs.existsSync(filePath)) {
try {
return fs.readFileSync(filePath, 'utf8').toString();
} catch (e) {
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
}
}
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
}
/**
* Load and parse te open api spec. If the file extension is ".yml" or ".yaml"
* we will try to parse the file as a YAML spec, otherwise we will fallback
* on parsing the file as JSON.
* @param filePath
*/
export function getOpenApiSpec(filePath: string): any {
const content: string = read(filePath);
const extname: string = path.extname(filePath).toLowerCase();
switch (extname) {
case '.yml':
case '.yaml':
try {
return yaml.safeLoad(content);
} catch (e) {
throw new Error(`Could not parse OpenApi YAML: "${filePath}"`);
}
default:
try {
return JSON.parse(content);
} catch (e) {
throw new Error(`Could not parse OpenApi JSON: "${filePath}"`);
}
}
}