forked from worldcoin/world-id-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.ts
126 lines (106 loc) · 3.87 KB
/
sync.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
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
/**
* This script syncs the READMEs of all World ID repositories so all of them have the same contextual information.
* The content from SHARED-README.md is added to all World ID repositories.
*/
import fs from 'fs'
const matchRegex =
/<!-- WORLD-ID-SHARED-README-TAG:START - Do not remove or modify this section directly -->(\r\n|\r|\n|.)*<!-- WORLD-ID-SHARED-README-TAG:END -->/
const sharedReadme: string = fs.readFileSync('SHARED-README.md', 'utf8')
const readmeSubstitution = `<!-- WORLD-ID-SHARED-README-TAG:START - Do not remove or modify this section directly -->\n${sharedReadme}\n<!-- WORLD-ID-SHARED-README-TAG:END -->`
const localSync = () => {
// Local sync
const localReadme: string = fs.readFileSync('README.md', 'utf8')
const newReadme = localReadme.replace(matchRegex, readmeSubstitution)
if (newReadme !== localReadme) {
fs.writeFileSync('README.md', newReadme, { encoding: 'utf8', flag: 'w' })
console.log('✅ Updated local README.')
} else {
console.log('🟢 Local README is already up-to-date.')
}
}
interface RepositoryInterface {
name: string
repository: string
sync_readme: boolean
description: string
}
interface RepositoriesInterface {
description: string
list: RepositoryInterface[]
}
const remoteSync = async () => {
const githubToken = process.env.GITHUB_TOKEN
if (!githubToken) {
throw new Error('`GITHUB_TOKEN` must be set to run this script.')
}
console.log('Starting remote sync ...')
const repositories: RepositoriesInterface = require('./repositories.json')
for (const repository of repositories.list) {
if (!repository.sync_readme) {
continue
}
console.log(`Starting check for ${repository.repository}`)
const response = await fetch(`https://api.github.com/repos/${repository.repository}/contents/README.md`)
if (!response.ok) {
throw new Error(`Error fetching repository ${repository.repository}.`)
}
const readmeMeta = await response.json()
const currentReadme = Buffer.from(readmeMeta.content, 'base64').toString('utf8')
if (!currentReadme.includes('WORLD-ID-SHARED-README-TAG:START')) {
throw new Error(`${repository.name} repository does not have the shared README tag.`)
}
const newContents = Buffer.from(currentReadme.replace(matchRegex, readmeSubstitution), 'utf8').toString(
'base64'
)
if (newContents !== readmeMeta.content.replaceAll('\n', '')) {
console.log(`Updating ${repository.repository}...`)
const payload = {
message: 'docs: syncing README for World ID repositories',
content: newContents,
sha: readmeMeta.sha,
committer: {
name: 'worldcoin[bot]',
email: '[email protected]',
},
}
const updateResponse = await fetch(
`https://api.github.com/repos/${repository.repository}/contents/README.md`,
{
method: 'PUT',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
Authorization: `token ${githubToken}`,
},
}
)
if (!updateResponse.ok) {
throw new Error(`Error updating repository ${repository.repository}. ${await updateResponse.text()}`)
}
console.log(`✅ Updated repository ${repository.repository}.`)
} else {
console.log(`🟢 ${repository.repository} is already up-to-date.`)
}
}
}
/**
* Syncs the descriptions for all repositories from GitHub.
*/
const descriptionsSync = async () => {
const repositories: RepositoriesInterface = require('./repositories.json')
for (let i = 0; i < repositories.list.length; i++) {
const repository = repositories.list[i]
const response = await fetch(`https://api.github.com/repos/${repository.repository}`)
repositories.list[i].description = (await response.json()).description
}
fs.writeFileSync('repositories.json', JSON.stringify(repositories, null, 2))
}
if (require.main === module) {
if (process.argv[2] === 'remote') {
remoteSync()
} else if (process.argv[2] === 'local') {
localSync()
} else {
descriptionsSync()
}
}