Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions script/search/lunr-search-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ export default class LunrIndex {
return Object.fromEntries(this.records.map((record) => [record.objectID, record]))
}

async write(outDirectory = path.posix.join(__dirname, '../../lib/search/indexes')) {
async write({
outDirectory = path.posix.join(__dirname, '../../lib/search/indexes'),
compressFiles = true,
}) {
this.build()

// Write the parsed records
await Promise.resolve(this.recordsObject)
.then(JSON.stringify)
.then(compress)
.then((str) => (compressFiles ? compress(str) : str))
.then((content) =>
fs.writeFile(
path.join(outDirectory, `${this.name}-records.json.br`),
path.join(
outDirectory,
compressFiles ? `${this.name}-records.json.br` : `${this.name}-records.json`
),
content
// Do not set to 'utf8'
)
Expand All @@ -96,10 +102,10 @@ export default class LunrIndex {
// Write the index
await Promise.resolve(this.index)
.then(JSON.stringify)
.then(compress)
.then((str) => (compressFiles ? compress(str) : str))
.then((content) =>
fs.writeFile(
path.join(outDirectory, `${this.name}.json.br`),
path.join(outDirectory, compressFiles ? `${this.name}.json.br` : `${this.name}.json`),
content
// Do not set to 'utf8'
)
Expand Down
32 changes: 32 additions & 0 deletions script/search/search-index-records.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs/promises'

import validateRecords from './validate-records.js'
import { compress } from '../../lib/search/compress.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

export async function writeIndexRecords(
name,
records,
{
outDirectory = path.posix.join(__dirname, '../../lib/search/indexes'),
compressFiles = true,
prettyPrint = false,
}
) {
validateRecords(name, records)

const recordsObject = Object.fromEntries(records.map((record) => [record.objectID, record]))
const content = JSON.stringify(recordsObject, undefined, prettyPrint ? 2 : 0)

const filePath = path.join(
outDirectory,
compressFiles ? `${name}-records.json.br` : `${name}-records.json`
)
await fs.writeFile(filePath, compressFiles ? await compress(content) : content)

return filePath
}
13 changes: 12 additions & 1 deletion script/search/sync-search-indices.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ program
.option('-d, --dry-run', 'Does not write to disk')
.option(
'-o, --out-directory <DIRECTORY>',
`Where to dump the created files (default ${DEFAULT_OUT_DIRECTORY}`
`Where to dump the created files (default ${DEFAULT_OUT_DIRECTORY})`
)
.option('--no-compression', `Do not Brotli compress the created .json files (default false)`)
// Once we've fully removed all Lunr indexing code, we can remove this option
// and change where it's used to be that the default is to not generate
// any Lunr indexes.
.option('--no-lunr-index', `Do not generate a Lunr index, just the records file (default false)`)
.parse(process.argv)

main(program.opts())
Expand Down Expand Up @@ -88,12 +93,18 @@ async function main(opts) {

const outDirectory = opts.outDirectory || DEFAULT_OUT_DIRECTORY

const compressFiles = !!opts.compression

const generateLunrIndex = !!opts.lunrIndex

const options = {
dryRun,
language,
notLanguage,
version,
outDirectory,
compressFiles,
generateLunrIndex,
}
await searchSync(options)
}
25 changes: 20 additions & 5 deletions script/search/sync.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env node
import chalk from 'chalk'

import languages from '../../lib/languages.js'
import buildRecords from './build-records.js'
import findIndexablePages from './find-indexable-pages.js'
import { allVersions } from '../../lib/all-versions.js'
import { namePrefix } from '../../lib/search/config.js'
import LunrIndex from './lunr-search-index.js'
import { writeIndexRecords } from './search-index-records.js'

// 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`
Expand All @@ -14,6 +17,8 @@ export default async function syncSearchIndexes({
dryRun,
notLanguage,
outDirectory,
compressFiles,
generateLunrIndex,
}) {
const t0 = new Date()

Expand All @@ -28,7 +33,9 @@ export default async function syncSearchIndexes({
)

console.log(
`Building indices for ${language || 'all languages'} and ${version || 'all versions'}.\n`
`Building indices for ${chalk.yellow(language || 'all languages')} and ${chalk.yellow(
version || 'all versions'
)}.\n`
)

// Exclude WIP pages, hidden pages, index pages, etc
Expand Down Expand Up @@ -67,11 +74,19 @@ export default async function syncSearchIndexes({
languageCode,
redirects
)
const index = new LunrIndex(indexName, records)
if (generateLunrIndex) {
const index = new LunrIndex(indexName, records)

if (!dryRun) {
await index.write(outDirectory)
console.log('wrote index to file: ', indexName)
if (!dryRun) {
await index.write({ outDirectory, compressFiles })
console.log('wrote index to file: ', indexName)
}
} else {
const fileWritten = await writeIndexRecords(indexName, records, {
outDirectory,
compressFiles,
})
console.log(`wrote records to ${fileWritten}`)
}
}
}
Expand Down