UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

30 lines (26 loc) 1.07 kB
'use strict'; const promises = require('node:fs/promises'); const path = require('@visulima/path'); const utils = require('@visulima/path/utils'); const assertValidFileOrDirectoryPath = require('./assertValidFileOrDirectoryPath-BMbgA-eI.cjs'); const getFileInfoType = require('./get-file-info-type-BpRzQHvd.cjs'); const ensureDir = require('./ensureDir-iUK2ga5M.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const ensureFile = /* @__PURE__ */ __name(async (filePath) => { assertValidFileOrDirectoryPath(filePath); try { const stat = await promises.lstat(filePath); if (!stat.isFile()) { throw new Error(`Ensure path exists, expected 'file', got '${getFileInfoType.getFileInfoType(stat)}'`); } } catch (error) { if (error.code === "ENOENT") { await ensureDir(path.dirname(utils.toPath(filePath))); await promises.writeFile(filePath, new Uint8Array()); return; } throw error; } }, "ensureFile"); module.exports = ensureFile;