forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive-enterprise-version.js
executable file
·198 lines (160 loc) · 6.51 KB
/
archive-enterprise-version.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const server = require('../server')
const port = '4001'
const host = `http://localhost:${port}`
const scrape = require('website-scraper')
const program = require('commander')
const rimraf = require('rimraf').sync
const mkdirp = require('mkdirp').sync
const version = require('../lib/enterprise-server-releases').oldestSupported
const archivalRepoName = 'help-docs-archived-enterprise-versions'
const archivalRepoUrl = `https://github.com/github/${archivalRepoName}`
const loadRedirects = require('../lib/redirects/precompile')
// [start-readme]
//
// Run this script during the Enterprise deprecation process to download
// static copies of all pages for the oldest supported Enterprise version.
// See the Enterprise deprecation issue template for instructions.
//
// [end-readme]
program
.description('Scrape HTML of the oldest supported Enterprise version and add it to the archival repository.')
.option('-p, --path-to-archival-repo <PATH>', `path to a local checkout of ${archivalRepoUrl}`)
.option('-d, --dry-run', 'only scrape the first 10 pages for testing purposes')
.parse(process.argv)
const pathToArchivalRepo = program.pathToArchivalRepo
const dryRun = program.dryRun
main()
class RewriteAssetPathsPlugin {
constructor (version, tempDirectory) {
this.version = version
this.tempDirectory = tempDirectory
}
apply (registerAction) {
registerAction('onResourceSaved', async ({ resource }) => {
// Show some activity
process.stdout.write('.')
// Only operate on HTML files
if (!resource.isHtml()) return
// Get the text contents of the resource
const text = resource.getText()
// Rewrite asset paths. Example:
// ../../javascripts/index.js -> /enterprise/2.17/javascripts/index.js
const newBody = text.replace(
/(?<attribute>src|href)="(?:\.\.\/)*(?<basepath>dist|javascripts|stylesheets|assets|node_modules)/g,
(match, attribute, basepath) => {
const replaced = path.join('/enterprise', this.version, basepath)
const returnValue = `${attribute}="${replaced}`
return returnValue
}
)
const filePath = path.join(this.tempDirectory, resource.getFilename())
await fs
.promises
.writeFile(filePath, newBody)
})
}
}
async function main () {
if (!pathToArchivalRepo) {
console.log(`Please specify a path to a local checkout of ${archivalRepoUrl}`)
console.log(`Example: script/archive-enterprise-version.js ../${archivalRepoName}`)
process.exit()
}
if (dryRun) {
console.log('This is a dry run! Creating HTML for redirects and scraping the first 10 pages only.\n')
}
// Build the production assets, to simulate a production deployment
console.log('Running `npm run build` for production assets')
execSync('npm run build', { stdio: 'inherit' })
console.log('Finish building production assets')
const fullPathToArchivalRepo = path.join(process.cwd(), pathToArchivalRepo)
if (!fs.existsSync(fullPathToArchivalRepo)) {
console.log(`archival repo path does not exist: ${fullPathToArchivalRepo}`)
process.exit()
}
console.log(`Enterprise version to archive: ${version}`)
const pages = await (require('../lib/pages')())
const permalinksPerVersion = Object.keys(pages)
.filter(key => key.includes(`/enterprise/${version}`))
const urls = dryRun
? permalinksPerVersion.slice(0, 10).map(href => `${host}${href}`)
: permalinksPerVersion.map(href => `${host}${href}`)
console.log(`found ${urls.length} pages for version ${version}`)
if (dryRun) {
console.log(`\nscraping html for these pages only:\n${urls.join('\n')}\n`)
}
const finalDirectory = path.join(fullPathToArchivalRepo, version)
const tempDirectory = path.join(__dirname, '../website-scraper-temp')
// remove temp directory
rimraf(tempDirectory)
// remove and recreate empty target directory
rimraf(finalDirectory)
fs.mkdirSync(finalDirectory, { recursive: true })
const scraperOptions = {
urls,
urlFilter: (url) => {
// Do not download assets from other hosts like S3 or octodex.github.com
// (this will keep them as remote references in the downloaded pages)
return url.startsWith(`http://localhost:${port}/`)
},
directory: tempDirectory,
filenameGenerator: 'bySiteStructure',
requestConcurrency: 6,
plugins: [new RewriteAssetPathsPlugin(version, tempDirectory)]
}
server.listen(port, async () => {
console.log(`started server on ${host}`)
await scrape(scraperOptions).catch(err => {
console.error('scraping error')
console.error(err)
})
fs.renameSync(
path.join(tempDirectory, `/localhost_${port}`),
path.join(finalDirectory)
)
rimraf(tempDirectory)
console.log(`\n\ndone scraping! added files to ${path.relative(process.cwd(), finalDirectory)}\n`)
// create redirect html files to preserve frontmatter redirects
await createRedirectPages(permalinksPerVersion, pages, finalDirectory)
console.log(`next step: deprecate ${version} in lib/enterprise-server-releases.js`)
process.exit()
})
}
async function createRedirectPages (permalinks, pages, finalDirectory) {
const pagesPerVersion = permalinks.map(permalink => pages[permalink])
const redirects = await loadRedirects(pagesPerVersion)
Object.entries(redirects).forEach(([oldPath, newPath]) => {
// replace any liquid variables with the version number
oldPath = oldPath.replace('{{ page.version }}', version)
// ignore any old paths that are not in this version
if (!oldPath.includes(`/enterprise/${version}`)) return
const fullPath = path.join(finalDirectory, oldPath)
const filename = `${fullPath}/index.html`
const html = getRedirectHtml(newPath)
mkdirp(fullPath)
fs.writeFileSync(filename, html)
})
console.log('done creating redirect files!\n')
}
// prior art: https://github.com/github/help-docs-archived-enterprise-versions/blob/master/2.12/user/leave-a-repo/index.html
// redirect html files already exist in <=2.12 because these versions were deprecated on the old static site
function getRedirectHtml (newPath) {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="${newPath}">
<meta http-equiv="refresh" content="0; url=${newPath}">
</head>
<body>
<h1>Redirecting...</h1>
<a href="${newPath}">Click here if you are not redirected.</a>
<script>location='${newPath}'</script>
</body>
</html>`
}