forked from graphprotocol/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-remote-filepaths.ts
57 lines (49 loc) · 1.55 KB
/
fetch-remote-filepaths.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
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
}
async function fetchRemoteFilePaths({ 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) => /\.mdx?$/.test(filePath)),
}
if (filterDocs) {
result.filePaths = result.filePaths.filter(filterDocs)
}
const json = JSON.stringify(result, null, 2)
await fs.writeFile(outputPath, json, 'utf8')
console.log(`✅ Remote files from "${url}" saved!`)
}
await fetchRemoteFilePaths({
user: 'graphprotocol',
repo: 'graph-client',
branch: 'main',
docsPath: 'docs/',
outputPath: path.join(process.cwd(), 'remote-files', 'graph-client.json'),
})
await fetchRemoteFilePaths({
user: 'graphprotocol',
repo: 'graph-tooling',
branch: 'main',
docsPath: 'packages/ts/',
outputPath: path.join(process.cwd(), 'remote-files', 'graph-ts.json'),
})