fix(utils): update medusa config resolution for consistency (#9591)
This commit is contained in:
@@ -13,14 +13,14 @@ describe("configLoader", () => {
|
||||
|
||||
expect(configModule).toBeUndefined()
|
||||
|
||||
configLoader(entryDirectory, "medusa-config")
|
||||
await configLoader(entryDirectory, "medusa-config")
|
||||
|
||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||
|
||||
expect(configModule).toBeDefined()
|
||||
expect(configModule.projectConfig.databaseName).toBeUndefined()
|
||||
|
||||
configLoader(entryDirectory, "medusa-config-2")
|
||||
await configLoader(entryDirectory, "medusa-config-2")
|
||||
|
||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||
|
||||
@@ -30,7 +30,7 @@ describe("configLoader", () => {
|
||||
|
||||
process.env.MEDUSA_WORKER_MODE = "worker"
|
||||
|
||||
configLoader(entryDirectory, "medusa-config-2")
|
||||
await configLoader(entryDirectory, "medusa-config-2")
|
||||
|
||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||
|
||||
|
||||
@@ -26,11 +26,14 @@ container.register(
|
||||
* @param entryDirectory The directory to find the config file from
|
||||
* @param configFileName The name of the config file to search for in the entry directory
|
||||
*/
|
||||
export function configLoader(
|
||||
export async function configLoader(
|
||||
entryDirectory: string,
|
||||
configFileName: string
|
||||
): ConfigModule {
|
||||
const config = getConfigFile<ConfigModule>(entryDirectory, configFileName)
|
||||
): Promise<ConfigModule> {
|
||||
const config = await getConfigFile<ConfigModule>(
|
||||
entryDirectory,
|
||||
configFileName
|
||||
)
|
||||
|
||||
if (config.error) {
|
||||
handleConfigError(config.error)
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user