forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
66 lines (57 loc) · 2.73 KB
/
sync.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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp').sync
const rimraf = require('rimraf').sync
const chalk = require('chalk')
const languages = require('../languages')
const buildRecords = require('./build-records')
const findIndexablePages = require('./find-indexable-pages')
const getRemoteIndexNames = require('./get-remote-index-names')
const Index = require('./search-index')
const cacheDir = path.join(process.cwd(), './.algolia-cache')
const { getOldVersionFromNewVersion } = require('../old-versions-utils')
// TODO need to update this to the new versions in coordination with the index filenames
const allVersions = Object.keys(require('../all-versions'))
// Build a search data file for every combination of product version and language
// e.g. `github-docs-dotcom-en.json` and `github-docs-2.14-ja.json`
module.exports = async function syncAlgoliaIndices (opts = {}) {
if (opts.dryRun) {
console.log('This is a dry run! The script will abort without uploading anything.')
rimraf(cacheDir)
mkdirp(cacheDir)
}
// Exlude WIP pages, hidden pages, index pages, etc
const indexablePages = await findIndexablePages()
// Build and validate all indices
for (const languageCode in languages) {
for (const pageVersion of allVersions) {
// TODO update to new versions in coordination with the index filenames
// For now, the index filename is still github-docs-dotcom-en, github-docs-2.22-en
const indexName = `${Index.namePrefix}-${getOldVersionFromNewVersion(pageVersion)}-${languageCode}`
// The page version will be the new version, e.g., free-pro-team@latest, [email protected]
const records = await buildRecords(indexName, indexablePages, pageVersion, languageCode)
const index = new Index(indexName, records)
if (opts.dryRun) {
const cacheFile = path.join(cacheDir, `${indexName}.json`)
fs.writeFileSync(cacheFile, JSON.stringify(index, null, 2))
console.log('wrote dry-run index to disk: ', cacheFile)
} else {
await index.syncWithRemote()
console.log('synced index with remote: ', indexName)
}
}
}
// Fetch a list of index names and cache it for tests
// to ensure that an index exists for every language and GHE version
const remoteIndexNames = await getRemoteIndexNames()
const cachedIndexNamesFile = path.join(__dirname, './cached-index-names.json')
fs.writeFileSync(
cachedIndexNamesFile,
JSON.stringify(remoteIndexNames, null, 2)
)
if (!process.env.CI) {
console.log(chalk.green(`\nCached remote index names in ${path.relative(process.cwd(), cachedIndexNamesFile)}`))
console.log(chalk.green('(If this file has any changes, please commit them)'))
}
console.log('\nDone!')
}