UNPKG

@rae004/get-file-paths

Version:

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

91 lines (90 loc) 5.29 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } 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); } }; var __asyncDelegator = (this && this.__asyncDelegator) || function (o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFileName = exports.getFilePathsGenerator = exports.getRelativePath = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); /** * Returns a file path from a relative root within a full path * * e.g. a fullPath of /usr/src/app/src/forms/layouts/<relativeRoot>/public/index.html * *** would be returned as public/index.html with opts.removeLeadingSlash = true (Default) * *** or /public/index.html with opts.removeLeadingSlash = false * * If lastIndexOfRelativeRoot = true we use lastIndexOf method to locate the index of relativeRoot * * e.g. if our full path is /usr/<relativeRoot>/app/<relativeRoot>/forms/layouts/test-form-layout/public/index.html * *** with lastIndexOfRelativeRoot = true (Default), then it would return a result of <relativeRoot>/forms/layouts/test-form-layout/public/index.html * *** with lastIndexOfRelativeRoot = false we would get <relativeRoot>/app/<relativeRoot>/forms/layouts/test-form-layout/public/index.html * * @param fullPath * @param options */ const getRelativePath = (fullPath, options) => { var _a; const relativeRoot = (_a = options.relativeRoot) !== null && _a !== void 0 ? _a : ''; const relativeRootLength = options.relativeRoot ? options.relativeRoot.length : 0; const includeRelativeRoot = (options === null || options === void 0 ? void 0 : options.includeRelativeRoot) ? -1 : relativeRootLength; return options.lastIndexOfRelativeRoot ? fullPath.substring(fullPath.lastIndexOf(relativeRoot) + includeRelativeRoot, fullPath.length) : fullPath.substring(fullPath.indexOf(relativeRoot) + includeRelativeRoot, fullPath.length); }; exports.getRelativePath = getRelativePath; /** * Generator function to loop through directories and return full file paths. * Recursively calls itself if another directory is encountered. * * @param dir directory path to search. * @param excludes string array of directory or file names to exclude. */ const getFilePathsGenerator = function (dir, excludes = []) { return __asyncGenerator(this, arguments, function* () { for (const dirent of yield __await(fs_1.promises.readdir(dir, { withFileTypes: true }))) { const res = path_1.default.resolve(dir, dirent.name); const isExcluded = excludes.map((exclude) => res.includes(exclude)); if (!isExcluded.includes(true)) { if (dirent.isDirectory()) { yield __await(yield* __asyncDelegator(__asyncValues((0, exports.getFilePathsGenerator)(res, excludes)))); } else { yield yield __await(res); } } } }); }; exports.getFilePathsGenerator = getFilePathsGenerator; const getFileName = (path) => { return path.substring(path.lastIndexOf('/'), path.length); }; exports.getFileName = getFileName;