UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

34 lines (31 loc) 1.12 kB
import { lstat, mkdir } from 'node:fs/promises'; import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-BWWgA1wj.mjs'; import { g as getFileInfoType } from './get-file-info-type-ButUVD6d.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const ensureDir = /* @__PURE__ */ __name(async (directory) => { assertValidFileOrDirectoryPath(directory); try { const fileInfo = await lstat(directory); if (!fileInfo.isDirectory()) { throw new Error(`Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`); } return; } catch (error) { if (error.code !== "ENOENT") { throw error; } } try { await mkdir(directory, { recursive: true }); } catch (error) { if (error.code !== "EEXIST") { throw error; } const fileInfo = await lstat(directory); if (!fileInfo.isDirectory()) { throw new Error(`Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`); } } }, "ensureDir"); export { ensureDir as default };