1
0
mirror of synced 2025-12-19 18:10:59 -05:00

Make it possible to precompute across all versions (#48776)

This commit is contained in:
Peter Bengtsson
2024-02-05 14:09:26 -05:00
committed by GitHub
parent f9eaf2403b
commit 1fe7e5ef53
2 changed files with 30 additions and 11 deletions

View File

@@ -36,7 +36,7 @@ runs:
- name: Run script
if: ${{ inputs.restore-only == '' }}
shell: bash
run: npm run precompute-pageinfo
run: npm run precompute-pageinfo -- --max-versions 2
- name: Cache .remotejson-cache (save)
if: ${{ inputs.restore-only == '' }}

View File

@@ -40,21 +40,24 @@ program
.description('Generates a JSON file with precompute pageinfo data by pathname')
.addOption(
new Option('-l, --language <LANGUAGE...>', 'Which languages to focus on')
.choices(languageKeys)
.choices(languageKeys.concat('all'))
.default(['en']),
)
.option('-o, --output-file <path>', 'path to output file', CACHE_FILE_PATH)
.option('--max-versions <number>', 'max. number of permalink versions per page')
.parse(process.argv)
type Options = {
outputFile: string
languages: string[]
maxVersions: number
}
const opts = program.opts()
main({
outputFile: opts.outputFile,
languages: opts.language,
maxVersions: isNaN(opts.maxVersions) ? 1 : Number(opts.maxVersions),
})
const CI = Boolean(JSON.parse(process.env.CI || 'false'))
@@ -66,10 +69,16 @@ type PageInfo = {
}
async function main(options: Options) {
const { outputFile, languages } = options
const { outputFile, languages, maxVersions } = options
if (outputFile !== CACHE_FILE_PATH) {
console.warn(chalk.yellow(`Writing to ${outputFile} instead of ${CACHE_FILE_PATH}`))
}
if (languages.includes('all')) {
// This sets it to [], which when sent into loadUnversionedTree means
// it does all languages that it can find.
languages.length = 0
}
const unversionedTree = await loadUnversionedTree(languages)
const pageList = await loadPages(unversionedTree, languages)
@@ -79,7 +88,9 @@ async function main(options: Options) {
[pathname: string]: PageInfo
} = {}
for (const page of pageList) {
const pathname = page.permalinks[0].href
let countVersions = 0
for (const permalink of page.permalinks) {
const pathname = permalink.href
try {
const computed = await getPageInfo(page, pathname)
if (computed) {
@@ -89,6 +100,14 @@ async function main(options: Options) {
console.error(`Error computing pageinfo for ${page.fullPath} (${pathname})`)
throw error
}
// By default, we only compute the first permalink href.
// But you can do more if you want.
if (++countVersions >= maxVersions) {
// This means we're content with only doing the first permalink href.
// That's 99% the free-pro-team permalink pathname.
break
}
}
}
console.timeEnd(label)