mirror of
https://github.com/unarray/file-tree-generator.git
synced 2026-07-22 08:40:29 +00:00
Optimize file explorer to directly filter files
This commit is contained in:
parent
7b8377584f
commit
7530f99a90
2 changed files with 21 additions and 4 deletions
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue