fix(utils): update medusa config resolution for consistency (#9591)
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
try {
|
||||||
|
require("ts-node").register({})
|
||||||
|
require("tsconfig-paths").register({})
|
||||||
|
} catch {}
|
||||||
|
require("dotenv").config()
|
||||||
|
|
||||||
const Configstore = require(`configstore`)
|
const Configstore = require(`configstore`)
|
||||||
const pkg = require(`../package.json`)
|
const pkg = require(`../package.json`)
|
||||||
const _ = require(`lodash`)
|
const _ = require(`lodash`)
|
||||||
|
|||||||
@@ -13,14 +13,14 @@ describe("configLoader", () => {
|
|||||||
|
|
||||||
expect(configModule).toBeUndefined()
|
expect(configModule).toBeUndefined()
|
||||||
|
|
||||||
configLoader(entryDirectory, "medusa-config")
|
await configLoader(entryDirectory, "medusa-config")
|
||||||
|
|
||||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||||
|
|
||||||
expect(configModule).toBeDefined()
|
expect(configModule).toBeDefined()
|
||||||
expect(configModule.projectConfig.databaseName).toBeUndefined()
|
expect(configModule.projectConfig.databaseName).toBeUndefined()
|
||||||
|
|
||||||
configLoader(entryDirectory, "medusa-config-2")
|
await configLoader(entryDirectory, "medusa-config-2")
|
||||||
|
|
||||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ describe("configLoader", () => {
|
|||||||
|
|
||||||
process.env.MEDUSA_WORKER_MODE = "worker"
|
process.env.MEDUSA_WORKER_MODE = "worker"
|
||||||
|
|
||||||
configLoader(entryDirectory, "medusa-config-2")
|
await configLoader(entryDirectory, "medusa-config-2")
|
||||||
|
|
||||||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,14 @@ container.register(
|
|||||||
* @param entryDirectory The directory to find the config file from
|
* @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
|
* @param configFileName The name of the config file to search for in the entry directory
|
||||||
*/
|
*/
|
||||||
export function configLoader(
|
export async function configLoader(
|
||||||
entryDirectory: string,
|
entryDirectory: string,
|
||||||
configFileName: string
|
configFileName: string
|
||||||
): ConfigModule {
|
): Promise<ConfigModule> {
|
||||||
const config = getConfigFile<ConfigModule>(entryDirectory, configFileName)
|
const config = await getConfigFile<ConfigModule>(
|
||||||
|
entryDirectory,
|
||||||
|
configFileName
|
||||||
|
)
|
||||||
|
|
||||||
if (config.error) {
|
if (config.error) {
|
||||||
handleConfigError(config.error)
|
handleConfigError(config.error)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { join } from "path"
|
import { join } from "path"
|
||||||
|
import { dynamicImport } from "./dynamic-import"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to resolve the config file in a given root directory.
|
* 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.
|
* @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.
|
* @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,
|
rootDir: string,
|
||||||
configName: string
|
configName: string
|
||||||
):
|
): Promise<
|
||||||
| { configModule: null; configFilePath: string; error: Error }
|
| { configModule: null; configFilePath: string; error: Error }
|
||||||
| { configModule: TConfig; configFilePath: string; error: null } {
|
| { configModule: TConfig; configFilePath: string; error: null }
|
||||||
|
> {
|
||||||
const configPath = join(rootDir, configName)
|
const configPath = join(rootDir, configName)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const configFilePath = require.resolve(configPath)
|
const configFilePath = join(process.cwd(), rootDir, configName)
|
||||||
const configExports = require(configFilePath)
|
const resolvedExports = await dynamicImport(configPath)
|
||||||
return {
|
return {
|
||||||
configModule:
|
configModule:
|
||||||
configExports && "default" in configExports
|
"default" in resolvedExports && resolvedExports.default
|
||||||
? configExports.default
|
? resolvedExports.default
|
||||||
: configExports,
|
: resolvedExports,
|
||||||
configFilePath,
|
configFilePath,
|
||||||
error: null,
|
error: null,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ export async function configLoaderOverride(
|
|||||||
override: { clientUrl: string; debug?: boolean }
|
override: { clientUrl: string; debug?: boolean }
|
||||||
) {
|
) {
|
||||||
const { configManager } = await import("@medusajs/framework/config")
|
const { configManager } = await import("@medusajs/framework/config")
|
||||||
const { configModule, error } = getConfigFile<
|
const { configModule, error } = await getConfigFile<
|
||||||
ReturnType<typeof configManager.loadConfig>
|
ReturnType<typeof configManager.loadConfig>
|
||||||
>(entryDirectory, "medusa-config")
|
>(entryDirectory, "medusa-config")
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import path from "path"
|
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 type tsStatic from "typescript"
|
||||||
import { logger } from "@medusajs/framework/logger"
|
import { logger } from "@medusajs/framework/logger"
|
||||||
import { ConfigModule } from "@medusajs/framework/types"
|
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
|
* 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
|
* Parsing the medusa config file to ensure it is error
|
||||||
* free
|
* free
|
||||||
*/
|
*/
|
||||||
const { configModule, configFilePath, error } = getConfigFile<ConfigModule>(
|
const { configModule, configFilePath, error } =
|
||||||
directory,
|
await getConfigFile<ConfigModule>(directory, "medusa-config")
|
||||||
"medusa-config"
|
|
||||||
)
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Failed to load medusa-config.js`)
|
console.error(`Failed to load medusa-config.js`)
|
||||||
console.error(error)
|
console.error(error)
|
||||||
@@ -199,7 +197,7 @@ async function buildBackend(projectRoot: string): Promise<boolean> {
|
|||||||
*/
|
*/
|
||||||
async function buildFrontend(projectRoot: string): Promise<boolean> {
|
async function buildFrontend(projectRoot: string): Promise<boolean> {
|
||||||
const startTime = process.hrtime()
|
const startTime = process.hrtime()
|
||||||
const configFile = loadMedusaConfig(projectRoot)
|
const configFile = await loadMedusaConfig(projectRoot)
|
||||||
if (!configFile) {
|
if (!configFile) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ async function loadEntrypoints(
|
|||||||
export async function initializeContainer(
|
export async function initializeContainer(
|
||||||
rootDirectory: string
|
rootDirectory: string
|
||||||
): Promise<MedusaContainer> {
|
): Promise<MedusaContainer> {
|
||||||
configLoader(rootDirectory, "medusa-config")
|
await configLoader(rootDirectory, "medusa-config")
|
||||||
await featureFlagsLoader(join(__dirname, "feature-flags"))
|
await featureFlagsLoader(join(__dirname, "feature-flags"))
|
||||||
|
|
||||||
container.register({
|
container.register({
|
||||||
|
|||||||
@@ -100,7 +100,10 @@ let index!: IndexTypes.IIndexService
|
|||||||
|
|
||||||
const beforeAll_ = async () => {
|
const beforeAll_ = async () => {
|
||||||
try {
|
try {
|
||||||
configLoader(path.join(__dirname, "./../__fixtures__"), "medusa-config")
|
await configLoader(
|
||||||
|
path.join(__dirname, "./../__fixtures__"),
|
||||||
|
"medusa-config"
|
||||||
|
)
|
||||||
|
|
||||||
console.log(`Creating database ${dbName}`)
|
console.log(`Creating database ${dbName}`)
|
||||||
await dbUtils.create(dbName)
|
await dbUtils.create(dbName)
|
||||||
|
|||||||
@@ -33,7 +33,10 @@ let medusaAppLoader!: MedusaAppLoader
|
|||||||
|
|
||||||
const beforeAll_ = async () => {
|
const beforeAll_ = async () => {
|
||||||
try {
|
try {
|
||||||
configLoader(path.join(__dirname, "./../__fixtures__"), "medusa-config")
|
await configLoader(
|
||||||
|
path.join(__dirname, "./../__fixtures__"),
|
||||||
|
"medusa-config"
|
||||||
|
)
|
||||||
|
|
||||||
console.log(`Creating database ${dbName}`)
|
console.log(`Creating database ${dbName}`)
|
||||||
await dbUtils.create(dbName)
|
await dbUtils.create(dbName)
|
||||||
|
|||||||
Reference in New Issue
Block a user