beam-cli
Version:
A beautifully simple CLI for running Lighthouse audits on a statically generated (SSG) website
26 lines (25 loc) • 978 B
JavaScript
import fsPromises from 'node:fs/promises';
import fs from 'node:fs';
/* eslint-disable @typescript-eslint/naming-convention */
/**
* Be sure to wrap in a Try-Catch block
* @param {string} filePath - Path to File, should include directory and extension
*/
export const loadJSONFile = async (filePath) => {
if (fs.existsSync(filePath)) {
const fileContent = await fsPromises.readFile(filePath, 'utf8');
const result = JSON.parse(fileContent);
return result;
}
throw new Error('JSON File not found');
};
/**
* Be sure to wrap in a Try-Catch block
* @param {string} filePath - Path to File, should include directory and extension
* @param {Record<string, any>} contents - Contents to be written as JSON file
*/
export const saveJSONFile = async (filePath, contents) => {
const content = JSON.stringify(contents);
await fsPromises.writeFile(filePath, content, 'utf8');
};
/* eslint-enable @typescript-eslint/naming-convention */