Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(project-config): Cache results of common functions #8581

Merged
merged 5 commits into from
Jun 15, 2023
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
5 changes: 5 additions & 0 deletions packages/project-config/src/configPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ const CONFIG_FILE_NAME = 'redwood.toml'
/**
* Search the parent directories for the Redwood configuration file.
*/
const getConfigPathCache = new Map<string, string>()
export const getConfigPath = (
cwd: string = process.env.RWJS_CWD ?? process.cwd()
): string => {
if (getConfigPathCache.has(cwd)) {
return getConfigPathCache.get(cwd) as string
}
const configPath = findUp(CONFIG_FILE_NAME, cwd)
if (!configPath) {
throw new Error(
`Could not find a "${CONFIG_FILE_NAME}" file, are you sure you're in a Redwood project?`
)
}
getConfigPathCache.set(cwd, configPath)
return configPath
}
7 changes: 6 additions & 1 deletion packages/project-config/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ export const resolveFile = (
/**
* Path constants that are relevant to a Redwood project.
*/
// TODO: Make this a proxy and make it lazy.
const getPathsCache = new Map<string, Paths>()
export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
if (getPathsCache.has(BASE_DIR)) {
return getPathsCache.get(BASE_DIR) as Paths
}

const routes = resolveFile(path.join(BASE_DIR, PATH_WEB_ROUTES)) as string
const { schemaPath } = getConfig(getConfigPath(BASE_DIR)).api
const schemaDir = path.dirname(schemaPath)
Expand Down Expand Up @@ -214,6 +218,7 @@ export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
fs.mkdirSync(paths.generated.types.includes, { recursive: true })
fs.mkdirSync(paths.generated.types.mirror, { recursive: true })

getPathsCache.set(BASE_DIR, paths)
return paths
}

Expand Down