mirror of
https://github.com/unarray/file-tree-generator.git
synced 2026-07-22 08:40:29 +00:00
30 lines
No EOL
841 B
TypeScript
30 lines
No EOL
841 B
TypeScript
import { beginningString } from "#/utils/regex";
|
|
import { readdir, stat } from "fs/promises";
|
|
import type { Ignore } from "ignore";
|
|
import { sep as separator } from "path";
|
|
|
|
export const getFiles = async(
|
|
path: string,
|
|
filter: Ignore | null = null,
|
|
removeRootPath: string | null = null,
|
|
files: string[] = []
|
|
): Promise<string[]> => {
|
|
const fileList = await readdir(path);
|
|
const regex = beginningString(removeRootPath ?? "");
|
|
|
|
for (const file of fileList) {
|
|
const fullName = `${path}${separator}${file}`;
|
|
const name = `${path}${separator}${file}`.replace(regex, "");
|
|
const entityStat = await stat(fullName);
|
|
|
|
if (filter?.ignores(name)) continue;
|
|
|
|
if (entityStat.isDirectory()) {
|
|
await getFiles(fullName, filter, removeRootPath, files);
|
|
} else {
|
|
files.push(name);
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}; |