Optimize file explorer to directly filter files

This commit is contained in:
Unarray 2023-08-27 12:01:14 +02:00
parent 7b8377584f
commit 7530f99a90
2 changed files with 21 additions and 4 deletions

View file

@ -1,5 +1,11 @@
import type { Ignore } from "ignore";
import ignore from "ignore";
export const filter = (items: string[], patterns: string[]): string[] => {
return ignore().add(patterns).filter(items);
};
export const getFilter = (patterns: string[]): Ignore => {
const filter = ignore().add(patterns);
return filter;
};

View file

@ -1,15 +1,26 @@
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: string[] = [], files: string[] = []): Promise<string[]> => {
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 name = `${path}${separator}${file}`;
const entityStat = await stat(name);
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(name, filter, files);
await getFiles(fullName, filter, removeRootPath, files);
} else {
files.push(name);
}