UNPKG

@rae004/get-file-paths

Version:

A package to get all file paths in a given directory recursively.

69 lines (68 loc) 3.52 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 __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFilePaths = void 0; const utilities_1 = require("./lib/utilities"); const defaults_1 = require("./lib/defaults"); /** * Take a directory path and return an array of objects with full & relative file paths for every file in that directory. * Recursive through child directories and except for directories or files listed in the excludes array. * * @param directoryPath * @param options */ const getFilePaths = (directoryPath, options) => __awaiter(void 0, void 0, void 0, function* () { var _a, e_1, _b, _c; const ourOptions = Object.assign(Object.assign({}, defaults_1.GET_RELATIVE_PATH_DEFAULTS), options); const paths = []; try { for (var _d = true, _e = __asyncValues((0, utilities_1.getFilePathsGenerator)(directoryPath, ourOptions.excludes)), _f; _f = yield _e.next(), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { const fullPath = _c; const relativePath = (options === null || options === void 0 ? void 0 : options.relativeRoot) ? (0, utilities_1.getRelativePath)(fullPath, ourOptions) : fullPath; const fileName = (0, utilities_1.getFileName)(fullPath); const fileObject = { fullPath, relativePath: ourOptions.removeLeadingSlash ? relativePath.substring(1, relativePath.length) : relativePath, fileName: ourOptions.removeLeadingSlash ? fileName.substring(1, fileName.length) : fileName, }; paths.push(fileObject); } finally { _d = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return paths; }); exports.getFilePaths = getFilePaths;