This commit is contained in:
Morten Olsen
2023-03-29 15:00:24 +02:00
parent a04d5f5d71
commit 35ef204433
12 changed files with 441 additions and 48 deletions

View File

@@ -12,14 +12,27 @@ type GlobOptions<T> = {
const defaultCreate = (a: any) => a;
const createGlob = <T = string>({ cwd, pattern, create = defaultCreate }: GlobOptions<T>) => {
const setup = (item: any) => {
if (item instanceof Observable) {
item.subscribe(() => {
glob.recreate();
});
}
};
const glob = new Observable(async () => {
const files = await fastGlob(pattern, { cwd });
return files.map((path) => create(resolve(cwd, path)));
return files.map((path) => {
const item = create(resolve(cwd, path));
setup(item);
return item;
});
});
const watcher = watchGlob(pattern, { cwd });
watcher.on('add', (path) => {
glob.set((current) => Promise.resolve([...(current || []), create(resolve(cwd, path))]));
const item = create(resolve(cwd, path));
glob.set((current) => Promise.resolve([...(current || []), item]));
setup(item);
return glob;
});