@visulima/fs
Version:
Human friendly file system utilities for Node.js
28 lines (25 loc) • 1.03 kB
JavaScript
import { lstatSync, writeFileSync } from 'node:fs';
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 ensureDirSync from './ensureDirSync-2IFUHFK2.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const ensureFileSync = /* @__PURE__ */ __name((filePath) => {
assertValidFileOrDirectoryPath(filePath);
try {
const stat = lstatSync(filePath);
if (!stat.isFile()) {
throw new Error(`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`);
}
} catch (error) {
if (error.code === "ENOENT") {
ensureDirSync(dirname(toPath(filePath)));
writeFileSync(filePath, new Uint8Array());
return;
}
throw error;
}
}, "ensureFileSync");
export { ensureFileSync as default };