UNPKG

tiny-readdir-glob

Version:

A simple promisified recursive readdir function, with support for globs.

67 lines (66 loc) 3.59 kB
/* IMPORT */ import path from 'node:path'; import process from 'node:process'; import readdir from 'tiny-readdir'; import { castArray, globsExplode, globsCompile, globsPartition, ignoreCompile, intersection, uniqFlat, uniqMergeConcat } from './utils.js'; /* MAIN */ const readdirGlob = async (glob, options) => { const [globsPositive, globsNegative] = globsPartition(castArray(glob)); const cwd = options?.cwd ?? process.cwd(); const ignore = [...castArray(options?.ignore ?? []), ...globsNegative]; const bucketDirectories = []; const bucketFiles = []; const bucketSymlinks = []; const bucketDirectoriesFound = []; const bucketFilesFound = []; const bucketSymlinksFound = []; const bucketDirectoriesFoundNames = []; const bucketFilesFoundNames = []; const bucketSymlinksFoundNames = []; const bucketDirectoriesFoundNamesToPaths = []; const bucketFilesFoundNamesToPaths = []; const bucketSymlinksFoundNamesToPaths = []; for (const [folders, foldersGlobs] of globsExplode(globsPositive)) { const isMatch = globsCompile(foldersGlobs); for (const folder of folders) { const rootPath = path.join(cwd, folder).replace(/\/$/, ''); const isIgnored = ignoreCompile(rootPath, ignore); const isRelativeMatch = (targetPath) => isMatch(rootPath, targetPath); const result = await readdir(rootPath, { depth: options?.depth, limit: options?.limit, followSymlinks: options?.followSymlinks, ignore: isIgnored, signal: options?.signal, onDirents: options?.onDirents }); bucketDirectories.push(result.directories.filter(isRelativeMatch)); bucketFiles.push(result.files.filter(isRelativeMatch)); bucketSymlinks.push(result.symlinks.filter(isRelativeMatch)); bucketDirectoriesFound.push(result.directories); bucketFilesFound.push(result.files); bucketSymlinksFound.push(result.symlinks); bucketDirectoriesFoundNames.push(result.directoriesNames); bucketFilesFoundNames.push(result.filesNames); bucketSymlinksFoundNames.push(result.symlinksNames); bucketDirectoriesFoundNamesToPaths.push(result.directoriesNamesToPaths); bucketFilesFoundNamesToPaths.push(result.filesNamesToPaths); bucketSymlinksFoundNamesToPaths.push(result.symlinksNamesToPaths); } } const directories = uniqFlat(bucketDirectories); const files = uniqFlat(bucketFiles); const symlinks = uniqFlat(bucketSymlinks); const directoriesFound = uniqFlat(bucketDirectoriesFound); const filesFound = uniqFlat(bucketFilesFound); const symlinksFound = uniqFlat(bucketSymlinksFound); const directoriesFoundNames = intersection(bucketDirectoriesFoundNames); const filesFoundNames = intersection(bucketFilesFoundNames); const symlinksFoundNames = intersection(bucketSymlinksFoundNames); const directoriesFoundNamesToPaths = uniqMergeConcat(bucketDirectoriesFoundNamesToPaths); const filesFoundNamesToPaths = uniqMergeConcat(bucketFilesFoundNamesToPaths); const symlinksFoundNamesToPaths = uniqMergeConcat(bucketSymlinksFoundNamesToPaths); return { directories, files, symlinks, directoriesFound, filesFound, symlinksFound, directoriesFoundNames, filesFoundNames, symlinksFoundNames, directoriesFoundNamesToPaths, filesFoundNamesToPaths, symlinksFoundNamesToPaths }; }; /* EXPORT */ export default readdirGlob;