@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
94 lines (92 loc) • 3.54 kB
JavaScript
import { hasFileExtension, joinPaths } from "@stryke/path";
import { isString } from "@stryke/type-checks";
import defu from "defu";
import { glob } from "glob";
//#region src/list-files.ts
const DEFAULT_OPTIONS = { dot: true };
/**
* Determines whether a string contains glob "magic" characters.
*
* @remarks
* This includes wildcards (`*`, `?`), character classes (`[...]`),
* extglob groups (`!(...)`, `+(...)`, etc.), and brace expansion
* (`{option1,option2}`). When any of these are present the value should be
* treated as an existing glob pattern instead of a plain path.
*
* @param value - The string to inspect
* @returns `true` if the string contains glob magic characters
*/
function isGlobPattern(value) {
return /[*?[\]{}()!+@]/.test(value);
}
/**
* A files and directories listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
async function list(filesGlob, options) {
return glob(isString(filesGlob) ? isGlobPattern(filesGlob) || hasFileExtension(filesGlob) ? filesGlob : joinPaths(filesGlob, "**/*") : filesGlob.input ? joinPaths(filesGlob.input, filesGlob.glob) : filesGlob.glob, defu(isString(filesGlob) ? {} : {
dot: filesGlob.dot,
ignore: filesGlob.ignore
}, options ?? {}, DEFAULT_OPTIONS));
}
/**
* A synchronous files and directories listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
function listSync(filesGlob, options) {
return glob.sync(isString(filesGlob) ? isGlobPattern(filesGlob) || hasFileExtension(filesGlob) ? filesGlob : joinPaths(filesGlob, "**/*") : filesGlob.input ? joinPaths(filesGlob.input, filesGlob.glob) : filesGlob.glob, defu(isString(filesGlob) ? {} : {
dot: filesGlob.dot,
ignore: filesGlob.ignore
}, options ?? {}, DEFAULT_OPTIONS));
}
/**
* A file listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
async function listFiles(filesGlob, options) {
const result = (await list(filesGlob, defu({ withFileTypes: true }, options ?? {}))).filter((ret) => ret.isFile());
if (!options?.withFileTypes) return result.map((file) => file.fullpath());
return result;
}
/**
* A synchronous file listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
function listFilesSync(filesGlob, options) {
const result = listSync(filesGlob, defu({ withFileTypes: true }, options ?? {})).filter((ret) => ret.isFile());
if (!options?.withFileTypes) return result.map((file) => file.fullpath());
return result;
}
/**
* A directories listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
async function listDirectories(filesGlob, options) {
const result = (await list(filesGlob, defu({ withFileTypes: true }, options ?? {}))).filter((ret) => ret.isDirectory());
if (!options?.withFileTypes) return result.map((file) => file.fullpath());
return result;
}
/**
* A synchronous directories listing helper function
*
* @param filesGlob - A glob pattern to match files
* @returns A list of file paths
*/
function listDirectoriesSync(filesGlob, options) {
const result = listSync(filesGlob, defu({ withFileTypes: true }, options ?? {})).filter((ret) => ret.isDirectory());
if (!options?.withFileTypes) return result.map((file) => file.fullpath());
return result;
}
//#endregion
export { list, listDirectories, listDirectoriesSync, listFiles, listFilesSync, listSync };
//# sourceMappingURL=list-files.mjs.map