forked from octokit/webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-actions-and-examples-from-payloads.mts
43 lines (37 loc) · 1.21 KB
/
get-actions-and-examples-from-payloads.mts
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
import { readFileSync, readdirSync } from "fs";
interface ActionsAndExamples {
actions: string[];
examples: object[];
}
const parentDirURL = new URL("..", import.meta.url);
export const getActionsAndExamplesFromPayloads = (
folderName: string,
): Record<string, ActionsAndExamples> => {
const pathToPayloads = `payload-examples/${folderName}`;
const eventsByName: Record<string, ActionsAndExamples> = {};
readdirSync(pathToPayloads, { withFileTypes: true })
.filter((entity) => entity.isDirectory())
.map((entity) => entity.name)
.forEach((event) => {
readdirSync(`${pathToPayloads}/${event}`)
.filter((path) => path.endsWith(".json"))
.forEach((path) => {
const payload = JSON.parse(
readFileSync(
new URL(`${pathToPayloads}/${event}/${path}`, parentDirURL),
).toString(),
) as {
action?: string;
};
eventsByName[event] ||= {
actions: [],
examples: [],
};
if (payload.action) {
eventsByName[event].actions.push(payload.action);
}
eventsByName[event].examples.push(payload);
});
});
return eventsByName;
};