-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.ts
More file actions
64 lines (49 loc) · 1.69 KB
/
utility.ts
File metadata and controls
64 lines (49 loc) · 1.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { $, path, fs } from 'npm:zx';
import WebUtility from 'npm:web-utility';
const { Second, sleep } = WebUtility;
/**
* Returns an ISO-8601 timestamp for the start or end (`endOfDay=true`) of a date string.
*/
export const toISOTimestamp = (date: string, endOfDay = false) =>
endOfDay ? `${date}T23:59:59Z` : `${date}T00:00:00Z`;
export async function resolvePath(
root: string,
folder: string,
file: string,
): Promise<string> {
const absolutePath = path.isAbsolute(folder)
? folder
: path.resolve(root, folder);
try {
const stats = await fs.stat(absolutePath);
return stats.isDirectory()
? path.join(absolutePath, file)
: absolutePath;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return absolutePath;
throw error;
}
}
/**
* Run a GraphQL query via `gh api graphql`.
*/
export async function gql<T = unknown>(
query: string,
extraArgs: string[] = [],
): Promise<T> {
const result = await $`gh api graphql -f query=${query} ${extraArgs}`;
return (JSON.parse(result.stdout) as { data: T }).data;
}
// ── Rate-limited Search API wrapper ──────────────────────────────────────────
const SEARCH_INTERVAL_S = 2.1;
let _lastSearchAt = 0;
/**
* Rate-limited wrapper for GitHub Search API (30 requests/min max).
*/
export async function ghSearch<T = unknown>(path: string): Promise<T> {
const elapsedS = (Date.now() - _lastSearchAt) / Second;
if (elapsedS < SEARCH_INTERVAL_S) await sleep(SEARCH_INTERVAL_S - elapsedS);
_lastSearchAt = Date.now();
const result = await $`gh api ${path}`;
return JSON.parse(result.stdout) as T;
}