UNPKG

fs-extender

Version:
191 lines 7.71 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; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.listSync = exports.promises = exports.list = exports.getOptions = void 0; const fs = __importStar(require("../patch")); const NodeAssert = __importStar(require("assert")); const path_extender_1 = __importDefault(require("path-extender")); const util = __importStar(require("../util")); /** @internal */ function getOptions(opt) { return { dereference: util.getObjectOption(opt, "dereference", false), ignoreAccessError: util.getObjectOption(opt, "ignoreAccessError", false), depth: util.getObjectOption(opt, "depth", -1), //buffer: util.getObjectOption(opt, "buffer", false), encoding: util.getObjectOption(opt, "encoding", "utf8"), }; } exports.getOptions = getOptions; function list(path, options, cb) { NodeAssert.ok(path, "'path' is required"); const opt = getOptions(options); const callback = util.getCallback(options, cb); _list(path, opt) .then((items) => callback(null, items)) .catch((err) => callback(err)); } exports.list = list; function _list(path, options) { return __awaiter(this, void 0, void 0, function* () { let items = []; const result = []; const subItems = []; let depth = 0; if (Buffer.isBuffer(path)) { options.encoding = "buffer"; } else if (options.encoding === "buffer") { path = Buffer.from(util.fileURLToPath(path)); } const statFn = options.dereference ? fs.promises.stat : fs.promises.lstat; subItems.push({ depth, items: [path] }); do { items = subItems[0].items; depth = subItems[0].depth; subItems.shift(); do { const item = items.shift(); let stats; try { stats = yield Reflect.apply(statFn, fs, [item]); } catch (er) { const err = er; if (!(err.code === "ENOENT" && options.ignoreAccessError)) { throw err; } continue; } result.push([item, stats]); if (stats.isDirectory()) { try { if (depth <= options.depth || options.depth === -1) { const its = yield fs.promises.readdir(item, { encoding: options.encoding, }); if (its.length) { subItems.push({ depth: depth + 1, items: its.map((m) => path_extender_1.default.join(item, m)), }); } } } catch (er) { const err = er; /* istanbul ignore next */ if (!options.ignoreAccessError || (options.ignoreAccessError && !(err.code === "EACCES" || err.code === "EPERM"))) { throw err; } } } } while (items.length); } while (subItems.length); return result.map((f) => ({ path: f[0], stats: f[1] })); }); } // eslint-disable-next-line @typescript-eslint/no-namespace var promises; (function (promises) { function list(path, options) { return __awaiter(this, void 0, void 0, function* () { const opt = getOptions(options); return Reflect.apply(_list, fs, [path, opt]); }); } promises.list = list; })(promises = exports.promises || (exports.promises = {})); function listSync(path, options) { let items = []; const result = []; const subItems = []; let depth = 0; NodeAssert.ok(path, "path must be defined"); const opt = getOptions(options); const statFn = opt.dereference ? fs.statSync : fs.lstatSync; if (Buffer.isBuffer(path)) { opt.encoding = "buffer"; } else if (opt.encoding === "buffer") { path = Buffer.from(util.fileURLToPath(path)); } subItems.push({ depth, items: [path] }); do { items = subItems[0].items; depth = subItems[0].depth; subItems.shift(); do { const item = items.shift(); let stats; try { stats = Reflect.apply(statFn, fs, [item]); } catch (er) { const err = er; if (!(err.code === "ENOENT" && opt.ignoreAccessError)) { throw err; } continue; } result.push([item, stats]); if (stats.isDirectory()) { try { if (depth <= opt.depth || opt.depth === -1) { const its = fs.readdirSync(item, { encoding: opt.encoding, }); if (its.length) { subItems.push({ depth: depth + 1, items: its.map((m) => path_extender_1.default.join(item, m)), }); } } } catch (er) { const err = er; /* istanbul ignore next */ if (!opt.ignoreAccessError || (opt.ignoreAccessError && !(err.code === "EACCES" || err.code === "EPERM"))) { throw err; } } } } while (items.length); } while (subItems.length); return result.map((f) => ({ path: f[0], stats: f[1] })); } exports.listSync = listSync; //# sourceMappingURL=index.js.map