Files
morten-olsen.github.io/bin/resources/glob/index.ts
Morten Olsen 9b1a067d56 init
2023-03-27 10:46:18 +02:00

34 lines
714 B
TypeScript

import fastGlob from "fast-glob";
import watchGlob from "glob-watcher";
import { Observable } from "../../observable";
type GlobOptions<T> = {
cwd?: string;
pattern: string;
create?: (path: string) => T;
};
const defaultCreate = (a: any) => a;
const createGlob = <T = string>({
cwd,
pattern,
create = defaultCreate,
}: GlobOptions<T>) => {
const glob = new Observable(async () => {
const files = await fastGlob(pattern, { cwd });
return files.map(create);
});
const watcher = watchGlob(pattern, { cwd });
watcher.on("add", (path) => {
glob.set((current) => Promise.resolve([...(current || []), create(path)]));
return glob;
});
return glob;
};
export { createGlob };