UNPKG

fs-extender

Version:
323 lines 9.02 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEmptySync = exports.isEmpty = exports.statIsSymbolicLinkSync = exports.statIsSymbolicLink = exports.statIsFileSync = exports.statIsFile = exports.statIsDirectorySync = exports.statIsDirectory = exports.existAccessSync = exports.existAccess = exports.exists = void 0; const fs = __importStar(require("./patch")); const fsAux = process.env["FS_EXTENDER_FS_OVERRIDE"] ? require(process.env["FS_EXTENDER_FS_OVERRIDE"]) : require("fs"); const utils_1 = require("@n3okill/utils"); const util = __importStar(require("../util")); const IgnorePatch = util.parseBoolean(process.env["FS_EXTENDER_IGNORE_PATCH"]); /** * Test whether or not the given path exists by checking with the file system. * Then call the `callback` argument with either true or false: * * ```js * import { exists } from 'fs-extender'; * * exists('/etc/passwd', (err, e) => { * console.log(e ? 'it exists' : 'no passwd!'); * }); * ``` * This method normalizes the functioning of the exists function of Node.js, returning an error as the first argument of callback * * Using `fs.exists()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing * so introduces a race condition, since other processes may change the file's * state between the two calls. Instead, user code should open/read/write the * file directly and handle the error raised if the file does not exist. * * **write (NOT RECOMMENDED)** * * ```js * import { exists, open, close } from 'fs-extender'; * * exists('myfile', (err, e) => { * if (e) { * console.error('myfile already exists'); * } else { * open('myfile', 'wx', (err, fd) => { * if (err) throw err; * * try { * writeMyData(fd); * } finally { * close(fd, (err) => { * if (err) throw err; * }); * } * }); * } * }); * ``` * * **write (RECOMMENDED)** * * ```js * import { open, close, lstatSync } from 'fs-extender'; * open('myfile', 'wx', (err, fd) => { * if (err) { * if (err.code === 'EEXIST') { * console.error('myfile already exists'); * return; * } * * throw err; * } * * try { * writeMyData(fd); * } finally { * close(fd, (err) => { * if (err) throw err; * }); * } * }); * ``` * * **read (NOT RECOMMENDED)** * * ```js * import { open, close, exists } from 'fs-extender'; * * exists('myfile', (err, e) => { * if (e) { * open('myfile', 'r', (err, fd) => { * if (err) throw err; * * try { * readMyData(fd); * } finally { * close(fd, (err) => { * if (err) throw err; * }); * } * }); * } else { * console.error('myfile does not exist'); * } * }); * ``` * * **read (RECOMMENDED)** * * ```js * import { open, close } from 'fs-extender'; * * open('myfile', 'r', (err, fd) => { * if (err) { * if (err.code === 'ENOENT') { * console.error('myfile does not exist'); * return; * } * * throw err; * } * * try { * readMyData(fd); * } finally { * close(fd, (err) => { * if (err) throw err; * }); * } * }); * ``` * * The "not recommended" examples above check for existence and then use the * file; the "recommended" examples are better because they use the file directly * and handle the error, if any. * * In general, check for the existence of a file only if the file won’t be * used directly, for example when its existence is a signal from another * process. */ function exists(path, callback) { /* istanbul ignore next */ if (!IgnorePatch) { if (!fsAux.exists) { Reflect.apply(existAccess, fs, [path, callback]); } else { Reflect.apply(fsAux.exists, fs, [ path, (val) => { callback(null, val); }, ]); } } else { Reflect.apply(fs.exists, fs, [path, callback]); } } exports.exists = exists; function existAccess(path, mode, callback) { if (utils_1.Type.isFunction(mode)) { callback = mode; mode = undefined; } Reflect.apply(fs.access, fs, [ path, mode, (err) => { callback(null, !err); }, ]); } exports.existAccess = existAccess; /** * Check if it is possible to access the `path` object * @param path * @param mode * @returns `boolean` */ function existAccessSync(path, mode) { try { Reflect.apply(fs.accessSync, fs, [path, mode]); return true; } catch (err) { return false; } } exports.existAccessSync = existAccessSync; /** * Check if the item is a directory * @param path * @param callback */ function statIsDirectory(path, callback) { Reflect.apply(fs.stat, fs, [ path, (err, stats) => { if (err) { /* istanbul ignore next */ return callback(err, false, stats); } callback(null, stats.isDirectory(), stats); }, ]); } exports.statIsDirectory = statIsDirectory; /** * Check if path is a directory * @param path * @returns */ function statIsDirectorySync(path) { return Reflect.apply(fs.statSync, fs, [path]).isDirectory(); } exports.statIsDirectorySync = statIsDirectorySync; /** * Check if path is a file * @param path * @param callback */ function statIsFile(path, callback) { Reflect.apply(fs.stat, fs, [ path, (err, stats) => { if (err) { return callback(err, false, stats); } callback(null, stats.isFile(), stats); }, ]); } exports.statIsFile = statIsFile; /** * Check if path is a file * @param path * @returns */ function statIsFileSync(path) { return Reflect.apply(fs.statSync, fs, [path]).isFile(); } exports.statIsFileSync = statIsFileSync; /** * Check if pat his a symbolik link * @param path * @param callback */ function statIsSymbolicLink(path, callback) { /* istanbul ignore next */ Reflect.apply(fs.lstat, fs, [ path, (err, stats) => { if (err) { return callback(err, false, stats); } callback(null, stats.isSymbolicLink(), stats); }, ]); } exports.statIsSymbolicLink = statIsSymbolicLink; /** * Check if path is a symbolik link * @param path * @returns */ function statIsSymbolicLinkSync(path) { return Reflect.apply(fs.lstatSync, fs, [path]).isSymbolicLink(); } exports.statIsSymbolicLinkSync = statIsSymbolicLinkSync; function isEmpty(path, options, callback) { const cb = util.getCallback(options, callback); const dereference = util.getObjectOption(options, "dereference", false); const stat = dereference ? fs.stat : fs.lstat; stat(path, (err, stats) => { if (err) { return cb(err); } if (stats.isDirectory()) { fs.readdir(path, (err2, items) => { if (err2) { /* istanbul ignore next */ return cb(err2); } return cb(null, items.length === 0); }); } else { cb(null, stats.size === 0); } }); } exports.isEmpty = isEmpty; /** * Check if given path is empty, if it's a folder it will use * `readdir` and check the number of returing items, * if it's another thing it will return the `size === 0`. * Will throw any error that happens while checking */ function isEmptySync(path, options = { dereference: false }) { const dereference = util.getObjectOption(options, "dereference", false); const stat = dereference ? fs.statSync : fs.lstatSync; const stats = stat(path); if (stats.isDirectory()) { const items = fs.readdirSync(path); return items.length === 0; } else { return stats.size === 0; } } exports.isEmptySync = isEmptySync; //# sourceMappingURL=addins.js.map