mirror of
https://github.com/zhigang1992/now-deployment.git
synced 2026-06-14 09:49:12 +08:00
28 lines
580 B
TypeScript
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;
|
|
}
|