chore(framework): Initial commit (#8221)
**What** - Initiate the framework package (which is just a place to move things around for now) - move the config loader and related resources as well as the `ConfigModule` type - Create a ConfigManager singleton which prepare and store the config (later can be stored entirely in the container) and allow for easier test override - re export the logger from the framework - replace medusa config loader with the framework one - `build` run type check on tests as well but `prepublishOnly` will not fail on build if tests are typed broken FIXES FRMW-2607 FIXES FRMW-2609 FIXES FRMW-2614 FIXES FRMW-2618
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
import { ConfigModule } from "@medusajs/types"
|
||||
import { getConfigFile, isDefined } from "@medusajs/utils"
|
||||
import logger from "./logger"
|
||||
|
||||
const isProduction = ["production", "prod"].includes(process.env.NODE_ENV || "")
|
||||
|
||||
const errorHandler = isProduction
|
||||
? (msg: string): never => {
|
||||
throw new Error(msg)
|
||||
}
|
||||
: console.log
|
||||
|
||||
export const handleConfigError = (error: Error): void => {
|
||||
logger.error(`Error in loading config: ${error.message}`)
|
||||
if (error.stack) {
|
||||
logger.error(error.stack)
|
||||
}
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const buildHttpConfig = (projectConfig: ConfigModule["projectConfig"]) => {
|
||||
const http = projectConfig.http ?? {}
|
||||
|
||||
http.jwtExpiresIn = http?.jwtExpiresIn ?? "1d"
|
||||
http.authCors = http.authCors ?? ""
|
||||
http.storeCors = http.storeCors ?? ""
|
||||
http.adminCors = http.adminCors ?? ""
|
||||
|
||||
http.jwtSecret = http?.jwtSecret ?? process.env.JWT_SECRET
|
||||
|
||||
if (!http.jwtSecret) {
|
||||
errorHandler(
|
||||
`[medusa-config] ⚠️ http.jwtSecret not found.${
|
||||
isProduction ? "" : "Using default 'supersecret'."
|
||||
}`
|
||||
)
|
||||
|
||||
http.jwtSecret = "supersecret"
|
||||
}
|
||||
|
||||
http.cookieSecret =
|
||||
projectConfig.http?.cookieSecret ?? process.env.COOKIE_SECRET
|
||||
|
||||
if (!http.cookieSecret) {
|
||||
errorHandler(
|
||||
`[medusa-config] ⚠️ http.cookieSecret not found.${
|
||||
isProduction ? "" : " Using default 'supersecret'."
|
||||
}`
|
||||
)
|
||||
|
||||
http.cookieSecret = "supersecret"
|
||||
}
|
||||
|
||||
return http
|
||||
}
|
||||
|
||||
const normalizeProjectConfig = (
|
||||
projectConfig: ConfigModule["projectConfig"]
|
||||
) => {
|
||||
if (!projectConfig?.redisUrl) {
|
||||
console.log(
|
||||
`[medusa-config] ⚠️ redisUrl not found. A fake redis instance will be used.`
|
||||
)
|
||||
}
|
||||
|
||||
projectConfig.http = buildHttpConfig(projectConfig)
|
||||
|
||||
let workedMode = projectConfig?.workerMode
|
||||
|
||||
if (!isDefined(workedMode)) {
|
||||
const env = process.env.MEDUSA_WORKER_MODE
|
||||
if (isDefined(env)) {
|
||||
if (env === "shared" || env === "worker" || env === "server") {
|
||||
workedMode = env
|
||||
}
|
||||
} else {
|
||||
workedMode = "shared"
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...projectConfig,
|
||||
workerMode: workedMode,
|
||||
}
|
||||
}
|
||||
|
||||
export default (rootDirectory: string): ConfigModule => {
|
||||
const { configModule, error } = getConfigFile<ConfigModule>(
|
||||
rootDirectory,
|
||||
`medusa-config`
|
||||
)
|
||||
|
||||
if (error) {
|
||||
handleConfigError(error)
|
||||
}
|
||||
|
||||
const projectConfig = normalizeProjectConfig(configModule.projectConfig)
|
||||
|
||||
return {
|
||||
projectConfig,
|
||||
admin: configModule?.admin ?? {},
|
||||
modules: configModule.modules ?? {},
|
||||
featureFlags: configModule?.featureFlags ?? {},
|
||||
plugins: configModule?.plugins ?? [],
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import requestIp from "request-ip"
|
||||
import { v4 } from "uuid"
|
||||
import adminLoader from "./admin"
|
||||
import apiLoader from "./api"
|
||||
import loadConfig from "./config"
|
||||
import { configLoader, logger } from "@medusajs/framework"
|
||||
import expressLoader from "./express"
|
||||
import featureFlagsLoader from "./feature-flags"
|
||||
import { registerJobs } from "./helpers/register-jobs"
|
||||
@@ -20,7 +20,6 @@ import { registerWorkflows } from "./helpers/register-workflows"
|
||||
import { getResolvedPlugins } from "./helpers/resolve-plugins"
|
||||
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
|
||||
import { SubscriberLoader } from "./helpers/subscribers"
|
||||
import Logger from "./logger"
|
||||
import loadMedusaApp from "./medusa-app"
|
||||
import registerPgConnection from "./pg-connection"
|
||||
|
||||
@@ -124,11 +123,11 @@ async function loadEntrypoints(
|
||||
|
||||
export async function initializeContainer(rootDirectory: string) {
|
||||
const container = createMedusaContainer()
|
||||
const configModule = loadConfig(rootDirectory)
|
||||
const featureFlagRouter = featureFlagsLoader(configModule, Logger)
|
||||
const configModule = configLoader(rootDirectory, "medusa-config.js")
|
||||
const featureFlagRouter = featureFlagsLoader(configModule, logger)
|
||||
|
||||
container.register({
|
||||
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
|
||||
[ContainerRegistrationKeys.LOGGER]: asValue(logger),
|
||||
[ContainerRegistrationKeys.FEATURE_FLAG_ROUTER]: asValue(featureFlagRouter),
|
||||
[ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule),
|
||||
[ContainerRegistrationKeys.REMOTE_QUERY]: asValue(null),
|
||||
|
||||
Reference in New Issue
Block a user