1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import * as fs from 'node:fs/promises';
- import path from 'node:path';
- export const loadJSON = async (path, throwError = true) => {
-
- const stat = await fs.stat(path);
-
- if (!stat.isFile()) {
-
- if (throwError) {
- throw new Error(`${path} does not exist.`);
- }
- return;
- }
- const jsonString = await fs.readFile(path, { encoding: 'utf-8' });
- return JSON.parse(jsonString);
- };
- export const exportFile = async (filePath, content) => {
-
- const folder = path.dirname(filePath);
-
- const stat = await fs.stat(folder).catch(() => ({
- isDirectory: () => false,
- }));
-
- if (!stat.isDirectory()) {
- await fs.mkdir(folder).catch(() => {
- return;
- });
- }
-
- return fs.writeFile(filePath, content);
- };
|