-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathfetch-remote-docs.ts
64 lines (57 loc) · 1.9 KB
/
fetch-remote-docs.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from 'node:fs/promises'
import path from 'node:path'
type Params = {
user: string
repo: string
branch: string
docsPath: string
outputPath: string
filterDocs?: (filePath: string) => boolean
}
const CWD = process.cwd()
async function fetchRemoteDocs({ user, repo, branch, docsPath, outputPath, filterDocs }: Params): Promise<void> {
const url = `https://api.github.com/repos/${user}/${repo}/git/trees/${branch}?recursive=1`
const response = await fetch(url)
const data = await response.json()
if (data.message) {
console.error('❌ GitHub API rate limit exceeded, skipping…', JSON.stringify(data, null, 2))
process.exit(0)
}
const filePaths = (data.tree as { path: string }[])
.filter((item) => item.path.startsWith(docsPath))
.map((item) => item.path.replace(docsPath, ''))
const result = {
user,
repo,
branch,
docsPath,
filePaths: filePaths.filter((filePath) => filePath.endsWith('.md')),
}
if (filterDocs) {
result.filePaths = result.filePaths.filter(filterDocs)
}
for (const fp of result.filePaths) {
const response = await fetch(`https://raw.githubusercontent.com/${user}/${repo}/${branch}/${docsPath}${fp}`)
if (!response.ok) {
throw new Error(`Failed to fetch remote file. ${response.status} ${response.statusText}`)
}
const text = await response.text()
const filePath = path.join(outputPath, fp)
await fs.writeFile(filePath, text)
console.log(`✅ Saved remote file "${fp}" in ${path.relative(CWD, filePath)}`)
}
}
await fetchRemoteDocs({
user: 'graphprotocol',
repo: 'graph-client',
branch: 'main',
docsPath: 'docs/',
outputPath: path.join(CWD, 'src', 'pages', 'en', 'querying', 'graph-client'),
})
await fetchRemoteDocs({
user: 'graphprotocol',
repo: 'graph-tooling',
branch: 'main',
docsPath: 'packages/ts/',
outputPath: path.join(CWD, 'src', 'pages', 'en', 'developing', 'graph-ts'),
})