2024-06-26 03:30:43 +00:00
|
|
|
import { Observable, from, map } from "rxjs";
|
2024-06-26 02:21:31 +00:00
|
|
|
import File from "vinyl";
|
|
|
|
import { Glob } from "glob";
|
|
|
|
import { readFile } from "node:fs/promises";
|
2024-06-26 02:38:13 +00:00
|
|
|
import { join, dirname } from "node:path";
|
2024-06-26 03:30:43 +00:00
|
|
|
import { existsSync } from "node:fs";
|
|
|
|
import { writeFile, mkdir } from "node:fs/promises";
|
2024-06-26 02:21:31 +00:00
|
|
|
|
2024-06-26 03:30:43 +00:00
|
|
|
export function src(glob: string | string[]): Observable<Promise<File>> {
|
2024-06-26 02:21:31 +00:00
|
|
|
return from(new Glob(glob, {})).pipe(
|
|
|
|
map(async (path) => new File({ path, contents: await readFile(path) })),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-26 03:30:43 +00:00
|
|
|
export function then<T, U>(f: (v: T) => Promise<U>) {
|
|
|
|
return (observable: Observable<Promise<T>>): Observable<Promise<U>> =>
|
|
|
|
observable.pipe(map((v) => v.then(f)));
|
|
|
|
}
|
2024-06-26 02:38:13 +00:00
|
|
|
|
2024-06-26 03:30:43 +00:00
|
|
|
export function dest(prefix: string) {
|
|
|
|
return async (file: File) => {
|
|
|
|
const actualPath = join(prefix, file.path);
|
|
|
|
if (!existsSync(dirname(actualPath))) {
|
|
|
|
await mkdir(dirname(actualPath), { recursive: true });
|
|
|
|
}
|
|
|
|
await writeFile(actualPath, file.contents as Buffer);
|
|
|
|
console.log("[-] Written", actualPath);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
};
|
2024-06-26 02:38:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 02:21:31 +00:00
|
|
|
export function synchronise<T>() {
|
|
|
|
return (observable: Observable<Promise<T>>): Observable<T> =>
|
|
|
|
new Observable<T>((subscriber) => {
|
2024-06-26 03:30:43 +00:00
|
|
|
const runningPromises = new Set<Promise<void>>();
|
2024-06-26 02:21:31 +00:00
|
|
|
let done = false;
|
|
|
|
|
|
|
|
observable.subscribe({
|
|
|
|
next(value) {
|
|
|
|
const promise = value.then((v) => {
|
2024-06-26 03:30:43 +00:00
|
|
|
runningPromises.delete(promise);
|
2024-06-26 02:21:31 +00:00
|
|
|
|
|
|
|
subscriber.next(v);
|
2024-06-26 03:30:43 +00:00
|
|
|
if (runningPromises.size === 0 && done) {
|
2024-06-26 02:21:31 +00:00
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
});
|
2024-06-26 03:30:43 +00:00
|
|
|
runningPromises.add(promise);
|
2024-06-26 02:21:31 +00:00
|
|
|
},
|
|
|
|
complete() {
|
|
|
|
done = true;
|
2024-06-26 03:30:43 +00:00
|
|
|
if (runningPromises.size === 0) {
|
2024-06-26 02:21:31 +00:00
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|