fix(utils): update medusa config resolution for consistency (#9591)

This commit is contained in:
Adrien de Peretti
2024-10-15 16:52:06 +02:00
committed by GitHub
parent 537567b679
commit e9383f25e0
9 changed files with 40 additions and 25 deletions

View File

@@ -1,4 +1,5 @@
import { join } from "path"
import { dynamicImport } from "./dynamic-import"
/**
* Attempts to resolve the config file in a given root directory.
@@ -6,22 +7,23 @@ import { join } from "path"
* @param {string} configName - the name of the config file.
* @return {object} an object containing the config module and its path as well as an error property if the config couldn't be loaded.
*/
export function getConfigFile<TConfig = unknown>(
export async function getConfigFile<TConfig = unknown>(
rootDir: string,
configName: string
):
): Promise<
| { configModule: null; configFilePath: string; error: Error }
| { configModule: TConfig; configFilePath: string; error: null } {
| { configModule: TConfig; configFilePath: string; error: null }
> {
const configPath = join(rootDir, configName)
try {
const configFilePath = require.resolve(configPath)
const configExports = require(configFilePath)
const configFilePath = join(process.cwd(), rootDir, configName)
const resolvedExports = await dynamicImport(configPath)
return {
configModule:
configExports && "default" in configExports
? configExports.default
: configExports,
"default" in resolvedExports && resolvedExports.default
? resolvedExports.default
: resolvedExports,
configFilePath,
error: null,
}