UNPKG

readdir

Version:

Reads a directory and return results with the ability to use Ant style file match patterns

152 lines 7.55 kB
"use strict"; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var fs_1 = require("fs"); var file_exists_1 = require("@kwsites/file-exists"); var deferred_error_1 = require("./deferred-error"); var read_dir_options_1 = require("../read-dir-options"); /** * Given the name of the directory about to be traversed, checks whether it should be - allows for the automatic * removal of "hidden" directories. * * @param {String} base * @param {String} directoryName * @param {Number} options * @return {Boolean} */ function should_read_directory(base, directoryName, options) { return !(read_dir_options_1.ReadDirOptions.NON_RECURSIVE & options) && !!(directoryName.charAt(0) != '.' || (read_dir_options_1.ReadDirOptions.INCLUDE_HIDDEN & options)); } exports.should_read_directory = should_read_directory; /** * Reads the supplied directory path and builds an array of files within the directory. This will work recursively * on each sub directory found. The optional appendTo argument can be used to merge file paths onto an existing * array, and is used internally for recursion. */ function read_dir_sync(dir, appendTo, prefixLength, options) { var contents = fs_1.readdirSync(dir); var result = appendTo || []; contents.forEach(function (itm) { var newPath = dir + itm; if (is_dir(newPath)) { if (should_read_directory(dir, itm, options)) { read_dir_sync(newPath + '/', result, prefixLength, options); } if (read_dir_options_1.ReadDirOptions.INCLUDE_DIRECTORIES & options) { result.push(newPath.substring(prefixLength) + '/'); } } else { result.push(newPath.substring(prefixLength)); } }); return result; } exports.read_dir_sync = read_dir_sync; function read_dir(dir, prefixLength, options, fileStat) { var appendTo = []; function directoryListingIterator(itm) { var _this = this; return new Promise(function (done, fail) { return __awaiter(_this, void 0, void 0, function () { var newPath, stat, isDirectory, _a, _b, _c, e_1; return __generator(this, function (_d) { switch (_d.label) { case 0: newPath = dir.replace(/\/$/, '') + '/' + itm; _d.label = 1; case 1: _d.trys.push([1, 5, , 6]); return [4 /*yield*/, fileStat(newPath)]; case 2: stat = _d.sent(); if (!stat) { return [2 /*return*/, deferred_error_1.deferred_error(done, fail, new Error("Unable to stat file: \"" + newPath + "\""), options)]; } isDirectory = stat.isDirectory(); if (!isDirectory) { return [2 /*return*/, done(appendTo.push(newPath.substring(prefixLength)))]; } if (read_dir_options_1.ReadDirOptions.INCLUDE_DIRECTORIES & options) { appendTo.push(newPath.substring(prefixLength) + '/'); } if (!should_read_directory(dir, itm, options)) return [3 /*break*/, 4]; _b = (_a = appendTo.push).apply; _c = [appendTo]; return [4 /*yield*/, read_dir(newPath, prefixLength, options, fileStat)]; case 3: _b.apply(_a, _c.concat([(_d.sent())])); _d.label = 4; case 4: done(); return [3 /*break*/, 6]; case 5: e_1 = _d.sent(); return [2 /*return*/, deferred_error_1.deferred_error(done, fail, e_1, options)]; case 6: return [2 /*return*/]; } }); }); }); } return new Promise(function (done, fail) { function directoryListing(err, contents) { if (err) { return deferred_error_1.deferred_error_with_data(done, fail, appendTo, err, options); } if (!contents.length) { return done([]); } Promise.all(contents.map(directoryListingIterator)) .then(function () { return done(appendTo); }) .catch(function (error) { return deferred_error_1.deferred_error_with_data(done, fail, appendTo, error, options); }); } fs_1.readdir(dir, directoryListing); }); } exports.read_dir = read_dir; /** * Gets a flag that identifies whether the supplied path is a directory or a file, true when a directory. In the * case that the file doesn't exist the result will be false. * * @param path * @return {Boolean} */ function is_dir(path) { return file_exists_1.exists(path, file_exists_1.FOLDER); } //# sourceMappingURL=directory.js.map