forked from graphprotocol/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-remote-filepaths.ts
88 lines (79 loc) · 2.49 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
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
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: 'streamingfast',
repo: 'substreams',
branch: 'develop',
docsPath: 'docs/',
outputPath: path.join(process.cwd(), 'remote-files', 'substreams.json'),
filterDocs: (filePath) =>
![
'SUMMARY.md', // toc
'developers-guide/modules/README.md', // generated cards by gitbook
].includes(filePath),
})
await fetchRemoteFilePaths({
user: 'streamingfast',
repo: 'firehose-docs',
branch: 'master',
docsPath: '',
outputPath: path.join(process.cwd(), 'remote-files', 'firehose.json'),
filterDocs: (filePath) =>
![
'SUMMARY.md', // toc
// ---- empty placeholders ----
'intro/README.md',
'firehose-setup/overview.md',
'firehose-setup/near/README.md',
'integrate-new-chains/README.md',
'references/README.md',
'concepts/README.md',
// ----------------------------
// ---- not shown in the existing site ----
'setup/cosmos/README.md',
'setup/ethereum/README.md',
'setup/ethereum/reprocessing-history.md',
'setup/ethereum/synchronization.md',
// ----------------------------------------
].includes(filePath),
})
await fetchRemoteFilePaths({
user: 'graphprotocol',
repo: 'graph-client',
branch: 'main',
docsPath: 'docs/',
outputPath: path.join(process.cwd(), 'remote-files', 'graph-client.json'),
})