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

sync.js 4.3 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
  1. const fs = require('fs')
  2. const path = require('path')
  3. const mkdirp = require('mkdirp').sync
  4. const rimraf = require('rimraf').sync
  5. const chalk = require('chalk')
  6. const languages = require('../languages')
  7. const buildRecords = require('./build-records')
  8. const findIndexablePages = require('./find-indexable-pages')
  9. const cacheDir = path.join(process.cwd(), './.search-cache')
  10. const allVersions = require('../all-versions')
  11. const { namePrefix } = require('./config')
  12. // Algolia
  13. const getRemoteIndexNames = require('./algolia-get-remote-index-names')
  14. const AlgoliaIndex = require('./algolia-search-index')
  15. // Lunr
  16. const LunrIndex = require('./lunr-search-index')
  17. const getLunrIndexNames = require('./lunr-get-index-names')
  18. // Build a search data file for every combination of product version and language
  19. // e.g. `github-docs-dotcom-en.json` and `github-docs-2.14-ja.json`
  20. module.exports = async function syncSearchIndexes (opts = {}) {
  21. if (opts.dryRun) {
  22. console.log('This is a dry run! The script will build the indices locally but not upload anything.\n')
  23. rimraf(cacheDir)
  24. mkdirp(cacheDir)
  25. }
  26. if (opts.language) {
  27. if (!Object.keys(languages).includes(opts.language)) {
  28. console.log(`Error! ${opts.language} not found. You must provide a currently supported two-letter language code.`)
  29. process.exit(1)
  30. }
  31. }
  32. if (opts.version) {
  33. if (!Object.keys(allVersions).includes(opts.version)) {
  34. console.log(`Error! ${opts.version} not found. You must provide a currently supported version in <PLAN@RELEASE> format.`)
  35. process.exit(1)
  36. }
  37. }
  38. // build indices for a specific language if provided; otherwise build indices for all languages
  39. const languagesToBuild = opts.language
  40. ? Object.keys(languages).filter(language => language === opts.language)
  41. : Object.keys(languages)
  42. // build indices for a specific version if provided; otherwise build indices for all veersions
  43. const versionsToBuild = opts.version
  44. ? Object.keys(allVersions).filter(version => version === opts.version)
  45. : Object.keys(allVersions)
  46. console.log(`Building indices for ${opts.language || 'all languages'} and ${opts.version || 'all versions'}.\n`)
  47. // Exclude WIP pages, hidden pages, index pages, etc
  48. const indexablePages = await findIndexablePages()
  49. // Build and validate all indices
  50. for (const languageCode of languagesToBuild) {
  51. for (const pageVersion of versionsToBuild) {
  52. // if GHES, resolves to the release number like 2.21, 2.22, etc.
  53. // if FPT, resolves to 'dotcom'
  54. // if GHAE, resolves to 'ghae'
  55. const indexVersion = allVersions[pageVersion].plan === 'enterprise-server'
  56. ? allVersions[pageVersion].currentRelease
  57. : allVersions[pageVersion].miscBaseName
  58. // github-docs-dotcom-en, github-docs-2.22-en
  59. const indexName = `${namePrefix}-${indexVersion}-${languageCode}`
  60. // The page version will be the new version, e.g., free-pro-team@latest, enterprise-server@2.22
  61. const records = await buildRecords(indexName, indexablePages, pageVersion, languageCode)
  62. const index = process.env.AIRGAP
  63. ? new LunrIndex(indexName, records)
  64. : new AlgoliaIndex(indexName, records)
  65. if (opts.dryRun) {
  66. const cacheFile = path.join(cacheDir, `${indexName}.json`)
  67. fs.writeFileSync(cacheFile, JSON.stringify(index, null, 2))
  68. console.log('wrote dry-run index to disk: ', cacheFile)
  69. } else {
  70. if (process.env.AIRGAP) {
  71. await index.write()
  72. console.log('wrote index to file: ', indexName)
  73. } else {
  74. await index.syncWithRemote()
  75. console.log('synced index with remote: ', indexName)
  76. }
  77. }
  78. }
  79. }
  80. // Fetch a list of index names and cache it for tests
  81. // to ensure that an index exists for every language and GHE version
  82. const remoteIndexNames = process.env.AIRGAP
  83. ? await getLunrIndexNames()
  84. : await getRemoteIndexNames()
  85. const cachedIndexNamesFile = path.join(__dirname, './cached-index-names.json')
  86. fs.writeFileSync(
  87. cachedIndexNamesFile,
  88. JSON.stringify(remoteIndexNames, null, 2)
  89. )
  90. if (!process.env.CI) {
  91. console.log(chalk.green(`\nCached index names in ${path.relative(process.cwd(), cachedIndexNamesFile)}`))
  92. console.log(chalk.green('(If this file has any changes, please commit them)'))
  93. }
  94. console.log('\nDone!')
  95. }
Tip!

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

Comments

Loading...