UNPKG

write-dir-safe

Version:

Create directories and their parents recursively

32 lines (31 loc) 920 B
import { promises, mkdirSync } from "node:fs"; function handleError(e) { if (e.code === "EEXIST") { return true; } else if (e.code === "ENOENT") { return false; } else { return undefined; } } export async function writeDir(path, options = {}) { var _options_recursive; return promises.mkdir(path, { recursive: (_options_recursive = options.recursive) !== null && _options_recursive !== void 0 ? _options_recursive : true }).then(()=>{ return true; }).catch((e)=>{ return handleError(e); }); } export function writeDirSync(path, options = {}) { try { var _options_recursive; mkdirSync(path, { recursive: (_options_recursive = options.recursive) !== null && _options_recursive !== void 0 ? _options_recursive : true }); return true; } catch (e) { return handleError(e); } }