fix(utils): update medusa config resolution for consistency (#9591)
This commit is contained in:
committed by
GitHub
parent
537567b679
commit
e9383f25e0
@@ -1,5 +1,11 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
try {
|
||||
require("ts-node").register({})
|
||||
require("tsconfig-paths").register({})
|
||||
} catch {}
|
||||
require("dotenv").config()
|
||||
|
||||
const Configstore = require(`configstore`)
|
||||
const pkg = require(`../package.json`)
|
||||
const _ = require(`lodash`)
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ export async function configLoaderOverride(
|
||||
override: { clientUrl: string; debug?: boolean }
|
||||
) {
|
||||
const { configManager } = await import("@medusajs/framework/config")
|
||||
const { configModule, error } = getConfigFile<
|
||||
const { configModule, error } = await getConfigFile<
|
||||
ReturnType<typeof configManager.loadConfig>
|
||||
>(entryDirectory, "medusa-config")
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import path from "path"
|
||||
import { rm, copyFile, access, constants } from "node:fs/promises"
|
||||
import { access, constants, copyFile, rm } from "node:fs/promises"
|
||||
import type tsStatic from "typescript"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import { ConfigModule } from "@medusajs/framework/types"
|
||||
@@ -39,15 +39,13 @@ async function clean(path: string) {
|
||||
/**
|
||||
* Loads the medusa config file or exits with an error
|
||||
*/
|
||||
function loadMedusaConfig(directory: string) {
|
||||
async function loadMedusaConfig(directory: string) {
|
||||
/**
|
||||
* Parsing the medusa config file to ensure it is error
|
||||
* free
|
||||
*/
|
||||
const { configModule, configFilePath, error } = getConfigFile<ConfigModule>(
|
||||
directory,
|
||||
"medusa-config"
|
||||
)
|
||||
const { configModule, configFilePath, error } =
|
||||
await getConfigFile<ConfigModule>(directory, "medusa-config")
|
||||
if (error) {
|
||||
console.error(`Failed to load medusa-config.js`)
|
||||
console.error(error)
|
||||
@@ -199,7 +197,7 @@ async function buildBackend(projectRoot: string): Promise<boolean> {
|
||||
*/
|
||||
async function buildFrontend(projectRoot: string): Promise<boolean> {
|
||||
const startTime = process.hrtime()
|
||||
const configFile = loadMedusaConfig(projectRoot)
|
||||
const configFile = await loadMedusaConfig(projectRoot)
|
||||
if (!configFile) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ async function loadEntrypoints(
|
||||
export async function initializeContainer(
|
||||
rootDirectory: string
|
||||
): Promise<MedusaContainer> {
|
||||
configLoader(rootDirectory, "medusa-config")
|
||||
await configLoader(rootDirectory, "medusa-config")
|
||||
await featureFlagsLoader(join(__dirname, "feature-flags"))
|
||||
|
||||
container.register({
|
||||
|
||||
@@ -100,7 +100,10 @@ let index!: IndexTypes.IIndexService
|
||||
|
||||
const beforeAll_ = async () => {
|
||||
try {
|
||||
configLoader(path.join(__dirname, "./../__fixtures__"), "medusa-config")
|
||||
await configLoader(
|
||||
path.join(__dirname, "./../__fixtures__"),
|
||||
"medusa-config"
|
||||
)
|
||||
|
||||
console.log(`Creating database ${dbName}`)
|
||||
await dbUtils.create(dbName)
|
||||
|
||||
@@ -33,7 +33,10 @@ let medusaAppLoader!: MedusaAppLoader
|
||||
|
||||
const beforeAll_ = async () => {
|
||||
try {
|
||||
configLoader(path.join(__dirname, "./../__fixtures__"), "medusa-config")
|
||||
await configLoader(
|
||||
path.join(__dirname, "./../__fixtures__"),
|
||||
"medusa-config"
|
||||
)
|
||||
|
||||
console.log(`Creating database ${dbName}`)
|
||||
await dbUtils.create(dbName)
|
||||
|
||||
Reference in New Issue
Block a user