-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfetch_agents.js
More file actions
160 lines (132 loc) · 4.68 KB
/
fetch_agents.js
File metadata and controls
160 lines (132 loc) · 4.68 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// fetch_agents.js
import fs, { readdirSync, statSync } from "fs";
import fetch from "node-fetch";
import path from "path";
const REPO_OWNER = "alchemyst-ai";
const REPO_NAME = "awesome-saas";
const TARGET_DIR = "developer-docs/example-projects/community";
const headers = { Accept: "application/vnd.github.v3+json" };
// Fetch repo tree (recursive) from GitHub API
async function fetchRepoTree() {
const res = await fetch(
`https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/trees/main?recursive=1`,
{ headers }
);
if (!res.ok) {
console.error("❌ Failed to fetch repo tree:", res.status, await res.text());
process.exit(1);
}
return res.json();
}
// Fetch file content directly from raw.githubusercontent
async function fetchFileContent(filePath) {
const res = await fetch(
`https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/main/${filePath}`
);
if (!res.ok) {
console.error(`❌ Failed to fetch file ${filePath}:`, res.status);
return null;
}
return await res.text();
}
async function run() {
console.log("🚀 Fetching agent READMEs from awesome-saas...");
const tree = await fetchRepoTree();
const readmes = tree.tree.filter((item) =>
item.path.match(/^agents\/[^/]+\/README\.md$/)
);
if (readmes.length === 0) {
console.log("⚠️ No README files found under agents/*/");
return;
}
fs.mkdirSync(TARGET_DIR, { recursive: true });
for (const file of readmes) {
const match = file.path.match(/^agents\/([^/]+)\/README\.md$/);
if (!match) continue;
const agentName = match[1];
const content = await fetchFileContent(file.path);
if (!content) continue;
const outPath = path.join(process.cwd(), TARGET_DIR, `${agentName}.mdx`);
fs.writeFileSync(outPath, content, "utf-8");
console.log(`✅ Saved ${agentName}.mdx`);
}
console.log("🎉 Done! All community agent READMEs fetched as .mdx files.");
}
const walk = (dir, fileList = []) => {
const files = readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = statSync(filePath);
if (stat.isDirectory()) {
walk(filePath, fileList);
} else if (file.endsWith(".mdx")) {
fileList.push(filePath);
}
});
return fileList;
};
function modifyDocsJson() {
const docs = fs.readFileSync('./developer-docs/docs.json', { encoding: "utf-8" });
try {
let docsJson = JSON.parse(docs) ?? {};
let navigationGuidesTab = ((docsJson.navigation ?? {}).tabs ?? [])
.filter(tab => !!tab)
.filter(tab => tab.tab === "Guides").pop();
if (!navigationGuidesTab) return;
let sampleProjectsGroup = (navigationGuidesTab.groups ?? [])
.filter(group => group.group.toLowerCase() === "sample projects").pop();
if (!sampleProjectsGroup) {
console.error("Sample Projects group not found!");
return;
};
let communityGroup = (sampleProjectsGroup.pages ?? [])
.filter(group => typeof group !== "string")
.filter(group => group.group === "By our community")
.pop();
if (!communityGroup) { console.log("Community group not found."); return };
communityGroup.pages = walk("./developer-docs/example-projects/community")
.map(page => page.replace(".mdx", ""))
.map(page => page.replace(
"example-projects/community/developer-docs/",
""
).replace("developer-docs/example-projects", "example-projects"));
console.log(navigationGuidesTab.groups
.map(group => Object.keys(group)));
const navigationGuidesGroups = [
...navigationGuidesTab.groups
.filter(group => group.group.toLowerCase() !== "sample projects"),
{
...sampleProjectsGroup,
pages: [
...sampleProjectsGroup.pages
.filter(page => typeof page === "string" || page.group !== "By our community"),
communityGroup
]
}];
console.log(docsJson.navigation.tabs
.filter(tab => tab.tab.toLowerCase() !== "guides"))
const reconstructedDocsJson = {
...docsJson,
"navigation": {
...docsJson.navigation,
tabs: [
{
...navigationGuidesTab,
groups: navigationGuidesGroups
},
...docsJson.navigation.tabs
.filter(tab => tab.tab.toLowerCase() !== "guides"),
]
}
}
console.log(reconstructedDocsJson.navigation.tabs.map(tab => tab.tab))
fs.writeFileSync("./developer-docs/docs.json", JSON.stringify(reconstructedDocsJson, null, 2));
} catch (err) {
console.error("Error happened while trying to modify docs.json...")
console.error(err);
}
}
run().catch((err) => {
console.error("❌ Error:", err);
process.exit(1);
}).then(modifyDocsJson);