diff --git a/src/utils/filter/filter.ts b/src/utils/filter/filter.ts index d54c5d0..3be47e6 100644 --- a/src/utils/filter/filter.ts +++ b/src/utils/filter/filter.ts @@ -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; }; \ No newline at end of file diff --git a/src/utils/path/path.ts b/src/utils/path/path.ts index d8485a5..5cd358b 100644 --- a/src/utils/path/path.ts +++ b/src/utils/path/path.ts @@ -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 => { +export const getFiles = async( + path: string, + filter: Ignore | null = null, + removeRootPath: string | null = null, + files: string[] = [] +): Promise => { 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); }