@visulima/fs
Version:
Human friendly file system utilities for Node.js
30 lines (26 loc) • 1.07 kB
JavaScript
;
const node_fs = require('node:fs');
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 ensureDirSync = require('./ensureDirSync-CXdMkg0Y.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const ensureFileSync = /* @__PURE__ */ __name((filePath) => {
assertValidFileOrDirectoryPath(filePath);
try {
const stat = node_fs.lstatSync(filePath);
if (!stat.isFile()) {
throw new Error(`Ensure path exists, expected 'file', got '${getFileInfoType.getFileInfoType(stat)}'`);
}
} catch (error) {
if (error.code === "ENOENT") {
ensureDirSync(path.dirname(utils.toPath(filePath)));
node_fs.writeFileSync(filePath, new Uint8Array());
return;
}
throw error;
}
}, "ensureFileSync");
module.exports = ensureFileSync;