-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ts
44 lines (39 loc) · 1.51 KB
/
lib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { MakeDirectoryOptions, WriteFileOptions } from 'node:fs';
export type MayonakaSyncCommand<T> = () => T;
// MayonakaCommand is a lazily evaluated promise that only gets executed when awaited
export class MayonakaCommand<T> {
executor: (resolve: (value: T) => void, reject: (reason?: any) => void) => void;
promise: Promise<T> | null;
constructor(executor: MayonakaCommand<T>['executor']) {
this.executor = executor;
this.promise = null;
}
then(onfulfilled: ((value: T) => T | PromiseLike<T>) | null | undefined, onrejected: ((reason: any) => PromiseLike<never>) | null | undefined) {
this.initPromise();
return this.promise?.then(onfulfilled, onrejected);
}
catch(onrejected: ((reason: any) => PromiseLike<never>) | null | undefined) {
this.initPromise();
return this.promise?.catch(onrejected);
}
finally(onfinally: (() => void) | null | undefined) {
this.initPromise();
return this.promise?.finally(onfinally);
}
initPromise() {
if (!this.promise) {
this.promise = new Promise<T>(this.executor);
}
}
}
export type AddFolderOptions = Omit<MakeDirectoryOptions, 'recursive'>;
export type AddFileOptions = WriteFileOptions;
export function chunk(arr: any[], size: number) {
const result = [];
for (let x = 0; x < Math.ceil(arr.length / size); x++) {
const start = x * size;
const end = start + size;
result.push(arr.slice(start, end));
}
return result;
}