Files
now-deployment/node_modules/now/dist/util/read-json-file.ts
2019-10-17 12:36:15 +09:00

28 lines
580 B
TypeScript

import fs from 'fs-extra';
import { CantParseJSONFile } from './errors-ts';
export default async function readJSONFile(
file: string
): Promise<Object | null | CantParseJSONFile> {
const content = await readFileSafe(file);
if (content === null) {
return content;
}
try {
const json = JSON.parse(content);
return json;
} catch (error) {
return new CantParseJSONFile(file);
}
}
async function readFileSafe(file: string) {
if (fs.existsSync(file)) {
const content = await fs.readFile(file);
return content.toString();
}
return null;
}