diff --git a/lib/create-tree.js b/lib/create-tree.js index eec6f240a80d..0a5e47711286 100644 --- a/lib/create-tree.js +++ b/lib/create-tree.js @@ -4,10 +4,22 @@ import fs from 'fs/promises' import Page from './page.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) +// Module level cache +const _basePaths = new Map() +// Return a full directory based on __dirname from a specific language directory. +// This function is memoized with a simple global cache object. +function getBasePath(directory) { + if (!_basePaths.has(directory)) { + _basePaths.set(directory, path.posix.join(__dirname, '..', directory, 'content')) + console.log(_basePaths.get(directory)) + } + return _basePaths.get(directory) +} + export default async function createTree(originalPath, langObj) { // This basePath definition is needed both here and in lib/page-data.js because this // function runs recursively, and the value for originalPath changes on recursive runs. - const basePath = path.posix.join(__dirname, '..', langObj.dir, 'content') + const basePath = getBasePath(langObj.dir) // On recursive runs, this is processing page.children items in `/` format. // If the path exists as is, assume this is a directory with a child index.md.