feat(providence): apply memoisation in optimised-glob
This commit is contained in:
parent
c7341d75ba
commit
1a1bdb76db
1 changed files with 165 additions and 136 deletions
|
|
@ -4,22 +4,25 @@ import nodeFs from 'fs';
|
|||
import path from 'path';
|
||||
|
||||
import { toPosixPath } from './to-posix-path.js';
|
||||
import { memoize } from './memoize.js';
|
||||
|
||||
/**
|
||||
* @typedef {nodeFs} FsLike
|
||||
* @typedef {nodeFs.Dirent & {path:string;parentPath:string}} DirentWithPath
|
||||
* @typedef {{onlyDirectories:boolean;onlyFiles:boolean;deep:number;suppressErrors:boolean;fs: FsLike;cwd:string;absolute:boolean;extglob:boolean;}} FastGlobtions
|
||||
*/
|
||||
|
||||
const [nodeMajor] = process.versions.node.split('.').map(Number);
|
||||
|
||||
/**
|
||||
export const parseGlobToRegex = memoize(
|
||||
/**
|
||||
* @param {string} glob
|
||||
* @param {object} [providedOpts]
|
||||
* @param {boolean} [providedOpts.globstar=true] if true, '/foo/*' => '^\/foo\/[^/]*$' (not allowing folders inside *), else '/foo/*' => '^\/foo\/.*$'
|
||||
* @param {boolean} [providedOpts.extglob=true] if true, supports so called "extended" globs (like bash) and single character matching, matching ranges of characters, group matching etc.
|
||||
* @returns {RegExp}
|
||||
*/
|
||||
export function parseGlobToRegex(glob, providedOpts) {
|
||||
(glob, providedOpts) => {
|
||||
if (typeof glob !== 'string') throw new TypeError('Expected a string');
|
||||
|
||||
const options = {
|
||||
|
|
@ -84,7 +87,9 @@ export function parseGlobToRegex(glob, providedOpts) {
|
|||
regexResultStr += '.*';
|
||||
} else {
|
||||
const isGlobstarSegment =
|
||||
isMultiStar && ['/', undefined].includes(prevChar) && ['/', undefined].includes(nextChar);
|
||||
isMultiStar &&
|
||||
['/', undefined].includes(prevChar) &&
|
||||
['/', undefined].includes(nextChar);
|
||||
if (isGlobstarSegment) {
|
||||
// Match zero or more path segments
|
||||
regexResultStr += '((?:[^/]*(?:/|$))*)';
|
||||
|
|
@ -101,12 +106,14 @@ export function parseGlobToRegex(glob, providedOpts) {
|
|||
}
|
||||
|
||||
return new RegExp(`^${regexResultStr}$`);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
const getStartPath = memoize(
|
||||
/**
|
||||
* @param {string} glob
|
||||
*/
|
||||
function getStartPath(glob) {
|
||||
glob => {
|
||||
const reservedChars = ['?', '[', ']', '{', '}', ',', '.', '*'];
|
||||
let hasFoundReservedChar = false;
|
||||
return glob
|
||||
|
|
@ -118,45 +125,68 @@ function getStartPath(glob) {
|
|||
})
|
||||
.filter(Boolean)
|
||||
.join('/');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let isCacheEnabled = false;
|
||||
/** @type {{[path:string]:nodeFs.Dirent[]}} */
|
||||
/** @type {{[path:string]:DirentWithPath[]}} */
|
||||
const cache = {};
|
||||
|
||||
/**
|
||||
const getAllDirentsFromStartPath = memoize(
|
||||
/**
|
||||
* @param {string} startPath
|
||||
* @param {{fs?:FsLike, dirents?:nodeFs.Dirent[]}} providedOptions
|
||||
* @returns {Promise<nodeFs.Dirent[]>}
|
||||
* @param {{fs?:FsLike, dirents?:DirentWithPath[]}} providedOptions
|
||||
* @returns {Promise<DirentWithPath[]>}
|
||||
*/
|
||||
async function getAllFilesFromStartPath(
|
||||
startPath,
|
||||
{ fs = /** @type {* & FsLike} */ (nodeFs), dirents = [] } = {},
|
||||
) {
|
||||
async (startPath, { fs = /** @type {* & FsLike} */ (nodeFs), dirents = [] } = {}) => {
|
||||
if (isCacheEnabled && cache[startPath]) return cache[startPath];
|
||||
|
||||
// Older node doesn't support recursive option
|
||||
if (nodeMajor < 18) {
|
||||
/** @type {nodeFs.Dirent[]} */
|
||||
const direntsForLvl = await fs.promises.readdir(startPath, { withFileTypes: true });
|
||||
for (const dirent of direntsForLvl) {
|
||||
// @ts-expect-error
|
||||
for (const _dirent of direntsForLvl) {
|
||||
const dirent = /** @type {DirentWithPath} */ (_dirent);
|
||||
dirent.parentPath = dirent.path = startPath; // eslint-disable-line no-multi-assign
|
||||
dirents.push(dirent);
|
||||
dirents.push(/** @type {DirentWithPath} */ (dirent));
|
||||
|
||||
if (dirent.isDirectory()) {
|
||||
const subDir = path.join(startPath, dirent.name);
|
||||
await getAllFilesFromStartPath(subDir, { fs, dirents });
|
||||
await getAllDirentsFromStartPath(subDir, { fs, dirents });
|
||||
}
|
||||
}
|
||||
return /** @type {nodeFs.Dirent[]} */ (dirents);
|
||||
return dirents;
|
||||
}
|
||||
|
||||
dirents.push(
|
||||
// @ts-expect-error
|
||||
dirents.push(...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })));
|
||||
...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })),
|
||||
);
|
||||
cache[startPath] = dirents;
|
||||
return dirents;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const getAllDirentsRelativeToCwd = memoize(
|
||||
/**
|
||||
* @param {string} fullStartPath
|
||||
* @param {{fs?:FsLike, cwd:string}} options
|
||||
* @returns {Promise<{relativeToCwdPath:string;dirent:DirentWithPath}[]>}
|
||||
*/
|
||||
async (fullStartPath, options) => {
|
||||
const allDirentsRelativeToStartPath = await getAllDirentsFromStartPath(fullStartPath, {
|
||||
fs: options.fs,
|
||||
});
|
||||
|
||||
const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({
|
||||
relativeToCwdPath: toPosixPath(
|
||||
path.join(dirent.parentPath || dirent.path, dirent.name),
|
||||
).replace(`${toPosixPath(options.cwd)}/`, ''),
|
||||
dirent,
|
||||
}));
|
||||
return allDirEntsRelativeToCwd;
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Lightweight glob implementation.
|
||||
|
|
@ -219,18 +249,11 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
|
|||
const fullStartPath = path.join(options.cwd, startPath);
|
||||
|
||||
try {
|
||||
const allDirentsRelativeToStartPath = await getAllFilesFromStartPath(fullStartPath, {
|
||||
const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, {
|
||||
cwd: options.cwd,
|
||||
fs: options.fs,
|
||||
});
|
||||
|
||||
const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({
|
||||
relativeToCwdPath: toPosixPath(
|
||||
// @ts-expect-error
|
||||
path.join(dirent.parentPath || dirent.path, dirent.name),
|
||||
).replace(`${toPosixPath(options.cwd)}/`, ''),
|
||||
dirent,
|
||||
}));
|
||||
|
||||
globEntries.push(...allDirEntsRelativeToCwd);
|
||||
} catch (e) {
|
||||
if (!options.suppressErrors) {
|
||||
|
|
@ -262,8 +285,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
|
|||
filteredPaths = filteredPaths.map(f => toPosixPath(path.join(options.cwd, f)));
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
const driveLetter = path.win32.resolve(options.cwd).slice(0, 1).toUpperCase();
|
||||
filteredPaths = filteredPaths.map(f => `${driveLetter}:${f}`);
|
||||
const driveChar = path.win32.resolve(options.cwd).slice(0, 1).toUpperCase();
|
||||
filteredPaths = filteredPaths.map(f => `${driveChar}:${f}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -273,10 +296,16 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
|
|||
|
||||
const result = options.unique ? Array.from(new Set(filteredPaths)) : filteredPaths;
|
||||
|
||||
return result.sort((a, b) => {
|
||||
const res = result.sort((a, b) => {
|
||||
const pathDiff = a.split('/').length - b.split('/').length;
|
||||
return pathDiff !== 0 ? pathDiff : a.localeCompare(b);
|
||||
});
|
||||
|
||||
// It could happen the fs changes with the next call, so we clear the cache
|
||||
getAllDirentsRelativeToCwd.clearCache();
|
||||
getAllDirentsFromStartPath.clearCache();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
optimisedGlob.disableCache = () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue