@visulima/fs
Version:
Human friendly file system utilities for Node.js
28 lines (25 loc) • 1.02 kB
JavaScript
import { lstat, writeFile } from 'node:fs/promises';
import { dirname } from '@visulima/path';
import { toPath } from '@visulima/path/utils';
import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-BWWgA1wj.mjs';
import { g as getFileInfoType } from './get-file-info-type-CeiZ6n6g.mjs';
import ensureDir from './ensureDir-XiBlkOLR.mjs';
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 lstat(filePath);
if (!stat.isFile()) {
throw new Error(`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`);
}
} catch (error) {
if (error.code === "ENOENT") {
await ensureDir(dirname(toPath(filePath)));
await writeFile(filePath, new Uint8Array());
return;
}
throw error;
}
}, "ensureFile");
export { ensureFile as default };