Make it possible to precompute across all versions (#48776)
This commit is contained in:
@@ -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 == '' }}
|
||||
|
||||
@@ -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,15 +88,25 @@ async function main(options: Options) {
|
||||
[pathname: string]: PageInfo
|
||||
} = {}
|
||||
for (const page of pageList) {
|
||||
const pathname = page.permalinks[0].href
|
||||
try {
|
||||
const computed = await getPageInfo(page, pathname)
|
||||
if (computed) {
|
||||
pageinfos[pathname] = computed
|
||||
let countVersions = 0
|
||||
for (const permalink of page.permalinks) {
|
||||
const pathname = permalink.href
|
||||
try {
|
||||
const computed = await getPageInfo(page, pathname)
|
||||
if (computed) {
|
||||
pageinfos[pathname] = computed
|
||||
}
|
||||
} catch (error) {
|
||||
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
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error computing pageinfo for ${page.fullPath} (${pathname})`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
console.timeEnd(label)
|
||||
|
||||
Reference in New Issue
Block a user