UNPKG

@graphql-mesh/utils

Version:
58 lines (57 loc) 1.75 kB
import { fs, path as pathModule } from '@graphql-mesh/cross-helpers'; export async function pathExists(path) { if (!path) { return false; } try { await fs.promises.stat(path); return true; } catch (e) { if (e.toString().includes('ENOENT')) { return false; } else { throw e; } } } export function writeJSON(path, data, replacer, space) { const stringified = JSON.stringify(data, replacer, space); return writeFile(path, stringified, 'utf-8'); } export const writeFile = async (path, ...args) => { if (typeof path === 'string') { const containingDir = pathModule.dirname(path); if (!(await pathExists(containingDir))) { await mkdir(containingDir); } } return fs.promises.writeFile(path, ...args); }; export async function mkdir(path, options = { recursive: true }) { const ifExists = await pathExists(path); if (!ifExists) { await fs.promises.mkdir(path, options); } } export async function rmdirs(dir) { if (await pathExists(dir)) { const entries = await fs.promises.readdir(dir, { withFileTypes: true }); const results = await Promise.allSettled(entries.map(entry => { const fullPath = pathModule.join(dir, entry.name); if (entry.isDirectory()) { return rmdirs(fullPath); } else { return fs.promises.unlink(fullPath); } })); for (const result of results) { if (result.status === 'rejected' && result.reason.code !== 'ENOENT') { throw result.reason; } } await fs.promises.rmdir(dir); } }