forked from sashin/sashinexists
run npm install to generate a package lock
This commit is contained in:
5
node_modules/tiny-readdir/dist/constants.d.ts
generated
vendored
Normal file
5
node_modules/tiny-readdir/dist/constants.d.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { Callback } from './types.js';
|
||||
declare const NOOP_PROMISE_LIKE: {
|
||||
then: (fn: Callback) => void;
|
||||
};
|
||||
export { NOOP_PROMISE_LIKE };
|
||||
9
node_modules/tiny-readdir/dist/constants.js
generated
vendored
Normal file
9
node_modules/tiny-readdir/dist/constants.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/* IMPORT */
|
||||
/* MAIN */
|
||||
const NOOP_PROMISE_LIKE = {
|
||||
then: (fn) => {
|
||||
fn();
|
||||
}
|
||||
};
|
||||
/* EXPORT */
|
||||
export { NOOP_PROMISE_LIKE };
|
||||
4
node_modules/tiny-readdir/dist/index.d.ts
generated
vendored
Normal file
4
node_modules/tiny-readdir/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import type { Dirent, Options, ResultDirectory, ResultDirectories, Result } from './types.js';
|
||||
declare const readdir: (rootPath: string, options?: Options) => Promise<Result>;
|
||||
export default readdir;
|
||||
export type { Dirent, Options, ResultDirectory, ResultDirectories, Result };
|
||||
185
node_modules/tiny-readdir/dist/index.js
generated
vendored
Normal file
185
node_modules/tiny-readdir/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
/* IMPORT */
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import makeCounterPromise from 'promise-make-counter';
|
||||
import { NOOP_PROMISE_LIKE } from './constants.js';
|
||||
import { castArray, isFunction } from './utils.js';
|
||||
/* MAIN */
|
||||
//TODO: Streamline the type of dirmaps
|
||||
const readdir = (rootPath, options) => {
|
||||
const followSymlinks = options?.followSymlinks ?? false;
|
||||
const maxDepth = options?.depth ?? Infinity;
|
||||
const maxPaths = options?.limit ?? Infinity;
|
||||
const ignore = options?.ignore ?? [];
|
||||
const ignores = castArray(ignore).map(ignore => isFunction(ignore) ? ignore : (targetPath) => ignore.test(targetPath));
|
||||
const isIgnored = (targetPath) => ignores.some(ignore => ignore(targetPath));
|
||||
const signal = options?.signal ?? { aborted: false };
|
||||
const onDirents = options?.onDirents || (() => { });
|
||||
const directories = [];
|
||||
const directoriesNames = new Set();
|
||||
const directoriesNamesToPaths = {};
|
||||
const files = [];
|
||||
const filesNames = new Set();
|
||||
const filesNamesToPaths = {};
|
||||
const symlinks = [];
|
||||
const symlinksNames = new Set();
|
||||
const symlinksNamesToPaths = {};
|
||||
const map = {};
|
||||
const visited = new Set();
|
||||
const resultEmpty = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {}, map: {} };
|
||||
const result = { directories, directoriesNames, directoriesNamesToPaths, files, filesNames, filesNamesToPaths, symlinks, symlinksNames, symlinksNamesToPaths, map };
|
||||
const { promise, increment, decrement } = makeCounterPromise();
|
||||
let foundPaths = 0;
|
||||
const handleDirectory = (dirmap, subPath, name, depth) => {
|
||||
if (visited.has(subPath))
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
foundPaths += 1;
|
||||
dirmap.directories.push(subPath);
|
||||
dirmap.directoriesNames.add(name);
|
||||
// dirmap.directoriesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.directoriesNamesToPaths[name] = [] );
|
||||
// dirmap.directoriesNamesToPaths[name].push ( subPath );
|
||||
directories.push(subPath);
|
||||
directoriesNames.add(name);
|
||||
directoriesNamesToPaths.propertyIsEnumerable(name) || (directoriesNamesToPaths[name] = []);
|
||||
directoriesNamesToPaths[name].push(subPath);
|
||||
visited.add(subPath);
|
||||
if (depth >= maxDepth)
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
populateResultFromPath(subPath, depth + 1);
|
||||
};
|
||||
const handleFile = (dirmap, subPath, name) => {
|
||||
if (visited.has(subPath))
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
foundPaths += 1;
|
||||
dirmap.files.push(subPath);
|
||||
dirmap.filesNames.add(name);
|
||||
// dirmap.filesNamesToPaths.propertyIsEnumerable(name) || ( dirmap.filesNamesToPaths[name] = [] );
|
||||
// dirmap.filesNamesToPaths[name].push ( subPath );
|
||||
files.push(subPath);
|
||||
filesNames.add(name);
|
||||
filesNamesToPaths.propertyIsEnumerable(name) || (filesNamesToPaths[name] = []);
|
||||
filesNamesToPaths[name].push(subPath);
|
||||
visited.add(subPath);
|
||||
};
|
||||
const handleSymlink = (dirmap, subPath, name, depth) => {
|
||||
if (visited.has(subPath))
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
foundPaths += 1;
|
||||
dirmap.symlinks.push(subPath);
|
||||
dirmap.symlinksNames.add(name);
|
||||
// dirmap.symlinksNamesToPaths.propertyIsEnumerable(name) || ( dirmap.symlinksNamesToPaths[name] = [] );
|
||||
// dirmap.symlinksNamesToPaths[name].push ( subPath );
|
||||
symlinks.push(subPath);
|
||||
symlinksNames.add(name);
|
||||
symlinksNamesToPaths.propertyIsEnumerable(name) || (symlinksNamesToPaths[name] = []);
|
||||
symlinksNamesToPaths[name].push(subPath);
|
||||
visited.add(subPath);
|
||||
if (!followSymlinks)
|
||||
return;
|
||||
if (depth >= maxDepth)
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
populateResultFromSymlink(subPath, depth + 1);
|
||||
};
|
||||
const handleStat = (dirmap, rootPath, name, stat, depth) => {
|
||||
if (signal.aborted)
|
||||
return;
|
||||
if (isIgnored(rootPath))
|
||||
return;
|
||||
if (stat.isDirectory()) {
|
||||
handleDirectory(dirmap, rootPath, name, depth);
|
||||
}
|
||||
else if (stat.isFile()) {
|
||||
handleFile(dirmap, rootPath, name);
|
||||
}
|
||||
else if (stat.isSymbolicLink()) {
|
||||
handleSymlink(dirmap, rootPath, name, depth);
|
||||
}
|
||||
};
|
||||
const handleDirent = (dirmap, rootPath, dirent, depth) => {
|
||||
if (signal.aborted)
|
||||
return;
|
||||
const separator = (rootPath === path.sep) ? '' : path.sep;
|
||||
const name = dirent.name;
|
||||
const subPath = `${rootPath}${separator}${name}`;
|
||||
if (isIgnored(subPath))
|
||||
return;
|
||||
if (dirent.isDirectory()) {
|
||||
handleDirectory(dirmap, subPath, name, depth);
|
||||
}
|
||||
else if (dirent.isFile()) {
|
||||
handleFile(dirmap, subPath, name);
|
||||
}
|
||||
else if (dirent.isSymbolicLink()) {
|
||||
handleSymlink(dirmap, subPath, name, depth);
|
||||
}
|
||||
};
|
||||
const handleDirents = (dirmap, rootPath, dirents, depth) => {
|
||||
for (let i = 0, l = dirents.length; i < l; i++) {
|
||||
handleDirent(dirmap, rootPath, dirents[i], depth);
|
||||
}
|
||||
};
|
||||
const populateResultFromPath = (rootPath, depth) => {
|
||||
if (signal.aborted)
|
||||
return;
|
||||
if (depth > maxDepth)
|
||||
return;
|
||||
if (foundPaths >= maxPaths)
|
||||
return;
|
||||
increment();
|
||||
fs.readdir(rootPath, { withFileTypes: true }, (error, dirents) => {
|
||||
if (error)
|
||||
return decrement();
|
||||
if (signal.aborted)
|
||||
return decrement();
|
||||
if (!dirents.length)
|
||||
return decrement();
|
||||
const promise = onDirents(dirents) || NOOP_PROMISE_LIKE;
|
||||
promise.then(() => {
|
||||
const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
|
||||
handleDirents(dirmap, rootPath, dirents, depth);
|
||||
decrement();
|
||||
});
|
||||
});
|
||||
};
|
||||
const populateResultFromSymlink = (rootPath, depth) => {
|
||||
increment();
|
||||
fs.realpath(rootPath, (error, realPath) => {
|
||||
if (error)
|
||||
return decrement();
|
||||
if (signal.aborted)
|
||||
return decrement();
|
||||
fs.stat(realPath, (error, stat) => {
|
||||
if (error)
|
||||
return decrement();
|
||||
if (signal.aborted)
|
||||
return decrement();
|
||||
const name = path.basename(realPath);
|
||||
const dirmap = map[rootPath] = { directories: [], directoriesNames: new Set(), directoriesNamesToPaths: {}, files: [], filesNames: new Set(), filesNamesToPaths: {}, symlinks: [], symlinksNames: new Set(), symlinksNamesToPaths: {} };
|
||||
handleStat(dirmap, realPath, name, stat, depth);
|
||||
decrement();
|
||||
});
|
||||
});
|
||||
};
|
||||
const getResult = async (rootPath, depth = 1) => {
|
||||
rootPath = path.normalize(rootPath);
|
||||
visited.add(rootPath);
|
||||
populateResultFromPath(rootPath, depth);
|
||||
await promise;
|
||||
if (signal.aborted)
|
||||
return resultEmpty;
|
||||
return result;
|
||||
};
|
||||
return getResult(rootPath);
|
||||
};
|
||||
/* EXPORT */
|
||||
export default readdir;
|
||||
42
node_modules/tiny-readdir/dist/types.d.ts
generated
vendored
Normal file
42
node_modules/tiny-readdir/dist/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
type Callback = () => void;
|
||||
type ArrayMaybe<T> = T[] | T;
|
||||
type PromiseMaybe<T> = Promise<T> | T;
|
||||
type Dirent = {
|
||||
isFile: () => boolean;
|
||||
isDirectory: () => boolean;
|
||||
isBlockDevice: () => boolean;
|
||||
isCharacterDevice: () => boolean;
|
||||
isSymbolicLink: () => boolean;
|
||||
isFIFO: () => boolean;
|
||||
isSocket: () => boolean;
|
||||
name: string;
|
||||
path: string;
|
||||
};
|
||||
type Options = {
|
||||
depth?: number;
|
||||
limit?: number;
|
||||
followSymlinks?: boolean;
|
||||
ignore?: ArrayMaybe<((targetPath: string) => boolean) | RegExp>;
|
||||
signal?: {
|
||||
aborted: boolean;
|
||||
};
|
||||
onDirents?: (dirents: Dirent[]) => PromiseMaybe<undefined>;
|
||||
};
|
||||
type ResultDirectory = {
|
||||
directories: string[];
|
||||
directoriesNames: Set<string>;
|
||||
directoriesNamesToPaths: Record<string, string[]>;
|
||||
files: string[];
|
||||
filesNames: Set<string>;
|
||||
filesNamesToPaths: Record<string, string[]>;
|
||||
symlinks: string[];
|
||||
symlinksNames: Set<string>;
|
||||
symlinksNamesToPaths: Record<string, string[]>;
|
||||
};
|
||||
type ResultDirectories = {
|
||||
[path: string]: ResultDirectory;
|
||||
};
|
||||
type Result = ResultDirectory & {
|
||||
map: ResultDirectories;
|
||||
};
|
||||
export type { Callback, PromiseMaybe, Dirent, Options, ResultDirectory, ResultDirectories, Result };
|
||||
2
node_modules/tiny-readdir/dist/types.js
generated
vendored
Normal file
2
node_modules/tiny-readdir/dist/types.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/* HELPERS */
|
||||
export {};
|
||||
3
node_modules/tiny-readdir/dist/utils.d.ts
generated
vendored
Normal file
3
node_modules/tiny-readdir/dist/utils.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
declare const castArray: <T>(value: T[] | T) => T[];
|
||||
declare const isFunction: (value: unknown) => value is Function;
|
||||
export { castArray, isFunction };
|
||||
9
node_modules/tiny-readdir/dist/utils.js
generated
vendored
Normal file
9
node_modules/tiny-readdir/dist/utils.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/* MAIN */
|
||||
const castArray = (value) => {
|
||||
return Array.isArray(value) ? value : [value];
|
||||
};
|
||||
const isFunction = (value) => {
|
||||
return (typeof value === 'function');
|
||||
};
|
||||
/* EXPORT */
|
||||
export { castArray, isFunction };
|
||||
Reference in New Issue
Block a user