-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathutils.ts
More file actions
42 lines (35 loc) · 1018 Bytes
/
utils.ts
File metadata and controls
42 lines (35 loc) · 1018 Bytes
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
import path from "path";
import type { APIBuilder } from "../src/builder/api-builder";
import type { IterableOutput } from "../src/types";
export type APITypes = (typeof apiTypes)[number];
export const apiTypes = ["withPromise", "sync", "withIterator"] as const;
export function root() {
return process.platform === "win32" ? process.cwd().split(path.sep)[0] : "/";
}
export function cwd() {
return `.${path.sep}`;
}
export function restricted() {
return process.platform === "win32"
? path.join(root(), "Windows", "System32")
: "/etc";
}
export function normalize(paths: string[]) {
return paths.map((p) =>
path.isAbsolute(p) ? path.resolve(p) : path.normalize(p)
);
}
export async function execute<T extends IterableOutput>(
api: APIBuilder<T>,
type: APITypes
): Promise<T> {
let files: T[number][] = [];
if (type === "withIterator") {
for await (const file of api[type]()) {
files.push(file);
}
} else {
files = await api[type]();
}
return files as T;
}