-
Notifications
You must be signed in to change notification settings - Fork 6
/
syncReadmes.js
217 lines (150 loc) · 6.07 KB
/
syncReadmes.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const fs = require('fs')
const path = require('path')
const cwd = process.cwd()
const mainReadmePath = path.join(cwd, 'Readme.md')
const cliReadmePath = path.join(cwd, '/packages/cli/README.md')
const coreReadmePath = path.join(cwd, '/packages/core/README.md')
const ESLintReadmePath = path.join(cwd, '/packages/eslint/README.md')
const vscodeReadmePath = path.join(cwd, '/packages/vscode/README.md')
const syncHero = () => {
console.log('Syncing Hero intro')
const pathsToUpdate = [
mainReadmePath,
cliReadmePath,
coreReadmePath,
ESLintReadmePath,
vscodeReadmePath,
]
const heroContentPath = path.join(cwd, 'readmePartials/hero.md')
const heroContent = fs.readFileSync(heroContentPath).toString()
const heroStartCommentLine = '<!-- HERO START -->'
const heroEndCommentLine = '<!-- HERO END -->'
pathsToUpdate.forEach((filePath) => {
const content = fs.readFileSync(filePath).toString()
const contentBeforeHero = content.split(heroStartCommentLine)[0]
const contentAfterHero = content.split(heroEndCommentLine)[1]
if (contentAfterHero || contentBeforeHero) {
const newContent = `${contentBeforeHero.trimEnd()}
${heroContent.trim()}
${contentAfterHero.trimStart()}`
fs.writeFileSync(filePath, newContent)
console.log('Saved', filePath.replace(cwd + '/', './'))
} else {
console.log('Hero location for', filePath, 'not found, skipping.')
}
})
}
const syncEslintIntro = () => {
console.log('Syncing Eslint intro')
const eslintIntroContentPath = path.join(cwd, 'readmePartials/eslintIntro.md')
const eslintIntroContent = fs.readFileSync(eslintIntroContentPath).toString()
const pathsToUpdate = [ESLintReadmePath, mainReadmePath]
const eslintIntroStartComment = '<!-- ESLINT INTRO START -->'
const eslintIntroEndComment = '<!-- ESLINT INTRO END -->'
pathsToUpdate.forEach((filePath) => {
const content = fs.readFileSync(filePath).toString()
const contentBeforeEslintIntro = content.split(eslintIntroStartComment)[0]
const contentAfterEslintIntro = content.split(eslintIntroEndComment)[1]
const newContent = `${contentBeforeEslintIntro.trimEnd()}
${eslintIntroContent}
${contentAfterEslintIntro.trimStart()}`
fs.writeFileSync(filePath, newContent)
console.log('Saved', filePath.replace(cwd + '/', './'))
})
}
const syncVscodeIntro = () => {
console.log('Syncing VSCode intro')
const vscodeIntroContentPath = path.join(cwd, 'readmePartials/vscodeIntro.md')
const vscodeIntroContent = fs.readFileSync(vscodeIntroContentPath).toString()
const pathsToUpdate = [vscodeReadmePath, mainReadmePath]
const vscodeIntroStartComment = '<!-- VSCODE INTRO START -->'
const vscodeIntroEndComment = '<!-- VSCODE INTRO END -->'
pathsToUpdate.forEach((filePath) => {
const content = fs.readFileSync(filePath).toString()
const contentBeforeEslintIntro = content.split(vscodeIntroStartComment)[0]
const contentAfterEslintIntro = content.split(vscodeIntroEndComment)[1]
const newContent = `${contentBeforeEslintIntro.trimEnd()}
${vscodeIntroContent}
${contentAfterEslintIntro.trimStart()}`
fs.writeFileSync(filePath, newContent)
console.log('Saved', filePath.replace(cwd + '/', './'))
})
}
const syncCliIntro = () => {
console.log('Syncing CLI intro')
const cliIntroContentPath = path.join(cwd, 'readmePartials/cliIntro.md')
const cliIntroContent = fs.readFileSync(cliIntroContentPath).toString()
const pathsToUpdate = [cliReadmePath, mainReadmePath]
const cliIntroStartComment = '<!-- CLI INTRO START -->'
const cliIntroEndComment = '<!-- CLI INTRO END -->'
pathsToUpdate.forEach((filePath) => {
const content = fs.readFileSync(filePath).toString()
const contentBeforeEslintIntro = content.split(cliIntroStartComment)[0]
const contentAfterEslintIntro = content.split(cliIntroEndComment)[1]
const newContent = `${contentBeforeEslintIntro.trimEnd()}
${cliIntroContent}
${contentAfterEslintIntro.trimStart()}`
fs.writeFileSync(filePath, newContent)
console.log('Saved', filePath.replace(cwd + '/', './'))
})
}
const syncFooters = () => {
console.log('Syncing footer')
const pathsToUpdate = [
mainReadmePath,
cliReadmePath,
coreReadmePath,
ESLintReadmePath,
vscodeReadmePath,
]
const footerContentPath = path.join(cwd, 'readmePartials/footer.md')
const footerContent = fs.readFileSync(footerContentPath).toString()
pathsToUpdate.forEach((filePath) => {
const content = fs.readFileSync(filePath).toString()
const footerStartComment = '<!-- FOOTER START -->'
const contentBeforeFooter = content.split(footerStartComment)[0]
if (contentBeforeFooter) {
const newContent = `${contentBeforeFooter.trimEnd()}
${footerContent.trimStart()}`
fs.writeFileSync(filePath, newContent)
console.log('Saved', filePath.replace(cwd + '/', './'))
} else {
console.log('Footer location for', filePath, 'not found, skipping.')
}
})
}
const addUtmSourceParams = () => {
const pathsToUpdate = [
{ filePath: mainReadmePath, utmSource: 'readme_main' },
{ filePath: cliReadmePath, utmSource: 'readme_cli' },
{ filePath: coreReadmePath, utmSource: 'readme_core' },
{ filePath: ESLintReadmePath, utmSource: 'readme_eslint' },
{ filePath: vscodeReadmePath, utmSource: 'readme_vscode' },
]
pathsToUpdate.forEach(({ filePath, utmSource }) => {
let content = fs.readFileSync(filePath).toString()
const linkRegExp = /"https:\/\/((codeque\.co)|(jayu\.dev)).*?"/g
const links = [...new Set(content.match(linkRegExp))].filter(
(link) => !link.includes('utm_source'),
)
links.forEach((link) => {
const url = new URL(link.replace(/"/g, ''))
url.searchParams.append('utm_source', utmSource)
const newLink = `"${url.toString()}"`
content = content.replaceAll(link, newLink)
})
fs.writeFileSync(filePath, content)
console.log(
'Updated',
links.length,
'links in',
filePath.replace(cwd + '/', './'),
)
})
}
syncHero()
syncEslintIntro()
syncVscodeIntro()
syncCliIntro()
syncFooters()
addUtmSourceParams()