Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

archive-version.js 7.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const { execSync } = require('child_process')
  5. const app = require('../../lib/app')
  6. const port = '4001'
  7. const host = `http://localhost:${port}`
  8. const scrape = require('website-scraper')
  9. const program = require('commander')
  10. const rimraf = require('rimraf').sync
  11. const version = require('../../lib/enterprise-server-releases').oldestSupported
  12. const archivalRepoName = 'help-docs-archived-enterprise-versions'
  13. const archivalRepoUrl = `https://github.com/github/${archivalRepoName}`
  14. const loadRedirects = require('../../lib/redirects/precompile')
  15. const { loadPageMap } = require('../../lib/pages')
  16. const remoteImageStoreBaseURL = 'https://githubdocs.azureedge.net/github-images'
  17. // [start-readme]
  18. //
  19. // Run this script during the Enterprise deprecation process to download
  20. // static copies of all pages for the oldest supported Enterprise version.
  21. // See the Enterprise deprecation issue template for instructions.
  22. //
  23. // [end-readme]
  24. program
  25. .description('Scrape HTML of the oldest supported Enterprise version and add it to the archival repository.')
  26. .option('-p, --path-to-archival-repo <PATH>', `path to a local checkout of ${archivalRepoUrl}`)
  27. .option('-d, --dry-run', 'only scrape the first 10 pages for testing purposes')
  28. .parse(process.argv)
  29. const pathToArchivalRepo = program.pathToArchivalRepo
  30. const dryRun = program.dryRun
  31. main()
  32. class RewriteAssetPathsPlugin {
  33. constructor (version, tempDirectory) {
  34. this.version = version
  35. this.tempDirectory = tempDirectory
  36. }
  37. apply (registerAction) {
  38. registerAction('onResourceSaved', async ({ resource }) => {
  39. // Show some activity
  40. process.stdout.write('.')
  41. // Only operate on HTML files
  42. if (!resource.isHtml() && !resource.isCss()) return
  43. // Get the text contents of the resource
  44. const text = resource.getText()
  45. let newBody = ''
  46. // Rewrite HTML asset paths. Example:
  47. // ../assets/images/foo/bar.png ->
  48. // https://githubdocs.azureedge.net/github-images/enterprise/2.17/assets/images/foo/bar.png
  49. if (resource.isHtml()) {
  50. newBody = text.replace(
  51. /(?<attribute>src|href)="(?:\.\.\/)*(?<basepath>dist|javascripts|stylesheets|assets\/fonts|assets\/images|node_modules)/g,
  52. (match, attribute, basepath) => {
  53. let replaced = path.join('/enterprise', this.version, basepath)
  54. if (basepath === 'assets/images') {
  55. replaced = remoteImageStoreBaseURL + replaced
  56. }
  57. const returnValue = `${attribute}="${replaced}`
  58. return returnValue
  59. }
  60. )
  61. }
  62. // Rewrite CSS asset paths. Example
  63. // url("../assets/fonts/alliance/alliance-no-1-regular.woff") ->
  64. // url("https://githubdocs.azureedge.net/github-images/enterprise/2.20/assets/fonts/alliance/alliance-no-1-regular.woff")
  65. if (resource.isCss()) {
  66. newBody = text.replace(
  67. /(?<attribute>url)\("(?:\.\.\/)*(?<basepath>assets\/fonts|assets\/images)/g,
  68. (match, attribute, basepath) => {
  69. const replaced = path.join(`${remoteImageStoreBaseURL}/enterprise`, this.version, basepath)
  70. const returnValue = `${attribute}("${replaced}`
  71. return returnValue
  72. }
  73. )
  74. }
  75. const filePath = path.join(this.tempDirectory, resource.getFilename())
  76. await fs
  77. .promises
  78. .writeFile(filePath, newBody, 'binary')
  79. })
  80. }
  81. }
  82. async function main () {
  83. if (!pathToArchivalRepo) {
  84. console.log(`Please specify a path to a local checkout of ${archivalRepoUrl}`)
  85. const scriptPath = path.relative(process.cwd(), __filename)
  86. console.log(`Example: ${scriptPath} -p ../${archivalRepoName}`)
  87. process.exit()
  88. }
  89. if (dryRun) {
  90. console.log('This is a dry run! Creating HTML for redirects and scraping the first 10 pages only.\n')
  91. }
  92. // Build the production assets, to simulate a production deployment
  93. console.log('Running `npm run build` for production assets')
  94. execSync('npm run build', { stdio: 'inherit' })
  95. console.log('Finish building production assets')
  96. const fullPathToArchivalRepo = path.join(process.cwd(), pathToArchivalRepo)
  97. if (!fs.existsSync(fullPathToArchivalRepo)) {
  98. console.log(`archival repo path does not exist: ${fullPathToArchivalRepo}`)
  99. process.exit()
  100. }
  101. console.log(`Enterprise version to archive: ${version}`)
  102. const pageMap = await loadPageMap()
  103. const permalinksPerVersion = Object.keys(pageMap)
  104. .filter(key => key.includes(`/enterprise-server@${version}`))
  105. const urls = dryRun
  106. ? permalinksPerVersion.slice(0, 10).map(href => `${host}${href}`)
  107. : permalinksPerVersion.map(href => `${host}${href}`)
  108. console.log(`found ${urls.length} pages for version ${version}`)
  109. if (dryRun) {
  110. console.log(`\nscraping html for these pages only:\n${urls.join('\n')}\n`)
  111. }
  112. const finalDirectory = path.join(fullPathToArchivalRepo, version)
  113. const tempDirectory = path.join(__dirname, '../website-scraper-temp')
  114. // remove temp directory
  115. rimraf(tempDirectory)
  116. // remove and recreate empty target directory
  117. rimraf(finalDirectory)
  118. fs.mkdirSync(finalDirectory, { recursive: true })
  119. const scraperOptions = {
  120. urls,
  121. urlFilter: (url) => {
  122. // Do not download assets from other hosts like S3 or octodex.github.com
  123. // (this will keep them as remote references in the downloaded pages)
  124. return url.startsWith(`http://localhost:${port}/`)
  125. },
  126. directory: tempDirectory,
  127. filenameGenerator: 'bySiteStructure',
  128. requestConcurrency: 6,
  129. plugins: [new RewriteAssetPathsPlugin(version, tempDirectory)]
  130. }
  131. app.listen(port, async () => {
  132. console.log(`started server on ${host}`)
  133. await scrape(scraperOptions).catch(err => {
  134. console.error('scraping error')
  135. console.error(err)
  136. })
  137. fs.renameSync(
  138. path.join(tempDirectory, `/localhost_${port}`),
  139. path.join(finalDirectory)
  140. )
  141. rimraf(tempDirectory)
  142. console.log(`\n\ndone scraping! added files to ${path.relative(process.cwd(), finalDirectory)}\n`)
  143. // create redirect html files to preserve frontmatter redirects
  144. await createRedirectsFile(permalinksPerVersion, pageMap, finalDirectory)
  145. console.log(`next step: deprecate ${version} in lib/enterprise-server-releases.js`)
  146. process.exit()
  147. })
  148. }
  149. async function createRedirectsFile (permalinks, pageMap, finalDirectory) {
  150. const pagesPerVersion = permalinks.map(permalink => pageMap[permalink])
  151. const redirects = await loadRedirects(pagesPerVersion, pageMap)
  152. const redirectsPerVersion = {}
  153. Object.entries(redirects).forEach(([oldPath, newPath]) => {
  154. // remove any liquid variables that sneak in
  155. oldPath = oldPath
  156. .replace('/{{ page.version }}', '')
  157. .replace('/{{ currentVersion }}', '')
  158. // ignore any old paths that are not in this version
  159. if (!(oldPath.includes(`/enterprise-server@${version}`) || oldPath.includes(`/enterprise/${version}`))) return
  160. redirectsPerVersion[oldPath] = newPath
  161. })
  162. fs.writeFileSync(path.posix.join(finalDirectory, 'redirects.json'), JSON.stringify(redirectsPerVersion, null, 2))
  163. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...