chore(framework): Continue to move loaders to framework (#8258)
**What** More move and cleanup FIXES FRMW-2603 FIXES FRMW-2608 FIXES FRMW-2610 FIXES FRMW-2611 Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
a2a377c8ca
commit
bcd9d9c2b1
@@ -60,7 +60,7 @@ export class Reporter {
|
||||
this.ora_ = activityLogger
|
||||
}
|
||||
|
||||
panic = (data) => {
|
||||
panic(data) {
|
||||
const parsedPanic = panicHandler(data)
|
||||
|
||||
this.loggerInstance_.log({
|
||||
@@ -81,17 +81,17 @@ export class Reporter {
|
||||
* @param {string} level - the level to check if logger is configured for
|
||||
* @return {boolean} whether we should log
|
||||
*/
|
||||
shouldLog = (level) => {
|
||||
level = this.loggerInstance_.levels[level]
|
||||
shouldLog = (level: string) => {
|
||||
const levelValue = this.loggerInstance_.levels[level]
|
||||
const logLevel = this.loggerInstance_.levels[this.loggerInstance_.level]
|
||||
return level <= logLevel
|
||||
return levelValue <= logLevel
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the log level of the logger.
|
||||
* @param {string} level - the level to set the logger to
|
||||
*/
|
||||
setLogLevel = (level) => {
|
||||
setLogLevel(level: string) {
|
||||
this.loggerInstance_.level = level
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ export class Reporter {
|
||||
* Resets the logger to the value specified by the LOG_LEVEL env var. If no
|
||||
* LOG_LEVEL is set it defaults to "silly".
|
||||
*/
|
||||
unsetLogLevel = () => {
|
||||
unsetLogLevel() {
|
||||
this.loggerInstance_.level = LOG_LEVEL
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ export class Reporter {
|
||||
* @returns {string} the id of the activity; this should be passed to do
|
||||
* further operations on the activity such as success, failure, progress.
|
||||
*/
|
||||
activity = (message, config = {}) => {
|
||||
activity(message: string, config: any = {}) {
|
||||
const id = ulid()
|
||||
if (IS_DEV && this.shouldLog("info")) {
|
||||
const activity = this.ora_(message).start()
|
||||
@@ -146,13 +146,13 @@ export class Reporter {
|
||||
* @param {string} activityId - the id of the activity as returned by activity
|
||||
* @param {string} message - the message to log
|
||||
*/
|
||||
progress = (activityId, message) => {
|
||||
progress(activityId: string, message: string) {
|
||||
const toLog = {
|
||||
level: "info",
|
||||
message,
|
||||
}
|
||||
|
||||
if (typeof activityId === "string" && this.activities_[activityId]) {
|
||||
if (this.activities_[activityId]) {
|
||||
const activity = this.activities_[activityId]
|
||||
if (activity.activity) {
|
||||
activity.text = message
|
||||
@@ -172,8 +172,9 @@ export class Reporter {
|
||||
* message to log the error under; or an error object.
|
||||
* @param {Error?} error - an error object to log message with
|
||||
*/
|
||||
error = (messageOrError, error: any = null) => {
|
||||
let message = messageOrError
|
||||
error(messageOrError: string | Error, error?: Error) {
|
||||
let message = messageOrError as string
|
||||
|
||||
if (typeof messageOrError === "object") {
|
||||
message = messageOrError.message
|
||||
error = messageOrError
|
||||
@@ -204,14 +205,14 @@ export class Reporter {
|
||||
* @param {string} message - the message to log
|
||||
* @returns {object} data about the activity
|
||||
*/
|
||||
failure = (activityId, message) => {
|
||||
failure(activityId: string, message: string) {
|
||||
const time = Date.now()
|
||||
const toLog = {
|
||||
level: "error",
|
||||
message,
|
||||
}
|
||||
|
||||
if (typeof activityId === "string" && this.activities_[activityId]) {
|
||||
if (this.activities_[activityId]) {
|
||||
const activity = this.activities_[activityId]
|
||||
if (activity.activity) {
|
||||
activity.activity.fail(`${message} – ${time - activity.start}`)
|
||||
@@ -243,14 +244,14 @@ export class Reporter {
|
||||
* @param {string} message - the message to log
|
||||
* @returns {Record<string, any>} data about the activity
|
||||
*/
|
||||
success = (activityId, message) => {
|
||||
success(activityId: string, message: string) {
|
||||
const time = Date.now()
|
||||
const toLog = {
|
||||
level: "info",
|
||||
message,
|
||||
}
|
||||
|
||||
if (typeof activityId === "string" && this.activities_[activityId]) {
|
||||
if (this.activities_[activityId]) {
|
||||
const activity = this.activities_[activityId]
|
||||
if (activity.activity) {
|
||||
activity.activity.succeed(`${message} – ${time - activity.start}ms`)
|
||||
@@ -278,7 +279,7 @@ export class Reporter {
|
||||
* Logs a message at the info level.
|
||||
* @param {string} message - the message to log
|
||||
*/
|
||||
debug = (message) => {
|
||||
debug(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
level: "debug",
|
||||
message,
|
||||
@@ -289,7 +290,7 @@ export class Reporter {
|
||||
* Logs a message at the info level.
|
||||
* @param {string} message - the message to log
|
||||
*/
|
||||
info = (message) => {
|
||||
info(message: string) {
|
||||
this.loggerInstance_.log({
|
||||
level: "info",
|
||||
message,
|
||||
@@ -300,7 +301,7 @@ export class Reporter {
|
||||
* Logs a message at the warn level.
|
||||
* @param {string} message - the message to log
|
||||
*/
|
||||
warn = (message) => {
|
||||
warn = (message: string) => {
|
||||
this.loggerInstance_.warn({
|
||||
level: "warn",
|
||||
message,
|
||||
@@ -310,7 +311,7 @@ export class Reporter {
|
||||
/**
|
||||
* A wrapper around winston's log method.
|
||||
*/
|
||||
log = (...args) => {
|
||||
log(...args) {
|
||||
if (args.length > 1) {
|
||||
// @ts-ignore
|
||||
this.loggerInstance_.log(...args)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApiKeyDTO, CreateApiKeyDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { createApiKeysStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { api_keys: CreateApiKeyDTO[] }
|
||||
|
||||
@@ -24,14 +24,15 @@
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@medusajs/framework": "^0.0.1",
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@medusajs/utils": "^1.11.9",
|
||||
"cross-env": "^5.2.1",
|
||||
"jest": "^29.7.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^5.1.6"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@medusajs/framework": "workspace:^",
|
||||
"@medusajs/medusa": ">1.19",
|
||||
"@medusajs/modules-sdk": "^1.12.10",
|
||||
"axios": "^0.28.0",
|
||||
@@ -40,9 +41,6 @@
|
||||
"pg-god": "^1.0.12"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@medusajs/framework": {
|
||||
"optional": true
|
||||
},
|
||||
"@medusajs/medusa": {
|
||||
"optional": true
|
||||
},
|
||||
|
||||
@@ -4,8 +4,6 @@ export async function configLoaderOverride(
|
||||
entryDirectory: string,
|
||||
override: { clientUrl: string; debug?: boolean }
|
||||
) {
|
||||
// in case it is not install as it is optional and required only when using this util
|
||||
// @ts-ignore
|
||||
const { configManager } = await import("@medusajs/framework/config")
|
||||
const { configModule, error } = getConfigFile<
|
||||
ReturnType<typeof configManager.loadConfig>
|
||||
@@ -27,5 +25,8 @@ export async function configLoaderOverride(
|
||||
idle_in_transaction_session_timeout: 20000,
|
||||
}
|
||||
|
||||
configManager.loadConfig(configModule)
|
||||
configManager.loadConfig({
|
||||
projectConfig: configModule,
|
||||
baseDir: entryDirectory,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
createMedusaContainer,
|
||||
isObject,
|
||||
} from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, isObject } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
export async function initDb({ env = {} }: { env?: Record<any, any> }) {
|
||||
@@ -10,18 +6,12 @@ export async function initDb({ env = {} }: { env?: Record<any, any> }) {
|
||||
Object.entries(env).forEach(([k, v]) => (process.env[k] = v))
|
||||
}
|
||||
|
||||
const container = createMedusaContainer()
|
||||
|
||||
// in case it is not install as it is optional and required only when using this util
|
||||
// @ts-ignore
|
||||
const { configManager } = await import("@medusajs/framework/config")
|
||||
const { configManager, pgConnectionLoader, container } = await import(
|
||||
"@medusajs/framework"
|
||||
)
|
||||
const configModule = configManager.config
|
||||
|
||||
const pgConnection =
|
||||
await require("@medusajs/medusa/dist/loaders/pg-connection").default({
|
||||
configModule,
|
||||
container,
|
||||
})
|
||||
const pgConnection = pgConnectionLoader()
|
||||
|
||||
const featureFlagRouter =
|
||||
require("@medusajs/medusa/dist/loaders/feature-flags").default(configModule)
|
||||
|
||||
@@ -26,6 +26,12 @@
|
||||
"import": "./dist/logger/index.js",
|
||||
"require": "./dist/logger/index.js",
|
||||
"node": "./dist/logger/index.js"
|
||||
},
|
||||
"./database": {
|
||||
"types": "./dist/database/index.d.ts",
|
||||
"import": "./dist/database/index.js",
|
||||
"require": "./dist/database/index.js",
|
||||
"node": "./dist/database/index.js"
|
||||
}
|
||||
},
|
||||
"engines": {
|
||||
@@ -42,7 +48,7 @@
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"watch": "tsc --build --watch -p ./tsconfig.build.json",
|
||||
"watch": "tsc --watch -p ./tsconfig.build.json",
|
||||
"watch:test": "tsc --build tsconfig.spec.json --watch",
|
||||
"prepublishOnly": "cross-env NODE_ENV=production tsc -p ./tsconfig.build.json && tsc-alias -p ./tsconfig.build.json",
|
||||
"build": "rimraf dist && tsc --build && tsc-alias",
|
||||
@@ -50,17 +56,23 @@
|
||||
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@medusajs/types": "^1.11.17-preview-20240510084332",
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@types/express": "^4.17.17",
|
||||
"cross-env": "^7.0.3",
|
||||
"ioredis": "^5.2.5",
|
||||
"rimraf": "^3.0.2",
|
||||
"tsc-alias": "^1.8.6",
|
||||
"typescript": "^5.1.6",
|
||||
"vite": "^5.2.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/medusa-cli": "^1.3.23",
|
||||
"@medusajs/utils": "^1.12.0-preview-20240724081425",
|
||||
"awilix": "^8.0.0"
|
||||
"@medusajs/medusa-cli": "^1.3.22",
|
||||
"@medusajs/utils": "^1.11.9",
|
||||
"awilix": "^8.0.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3",
|
||||
"ioredis": "^5.2.5",
|
||||
"ioredis-mock": "8.4.0",
|
||||
"morgan": "^1.9.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import { ConfigModule } from "./types"
|
||||
import { deepCopy, isDefined } from "@medusajs/utils"
|
||||
import { Logger } from "@medusajs/types"
|
||||
import { logger } from "../logger"
|
||||
|
||||
export class ConfigManager {
|
||||
/**
|
||||
* Root dir from where to start
|
||||
* @private
|
||||
*/
|
||||
#baseDir: string
|
||||
|
||||
/**
|
||||
* A flag to specify if we are in production or not, determine whether an error would be critical and thrown or just logged as a warning in developement
|
||||
* @private
|
||||
@@ -18,12 +24,6 @@ export class ConfigManager {
|
||||
readonly #envWorkMode?: ConfigModule["projectConfig"]["workerMode"] = process
|
||||
.env.MEDUSA_WORKER_MODE as ConfigModule["projectConfig"]["workerMode"]
|
||||
|
||||
/**
|
||||
* The logger instance to use
|
||||
* @private
|
||||
*/
|
||||
readonly #logger: Logger
|
||||
|
||||
/**
|
||||
* The config object after loading it
|
||||
* @private
|
||||
@@ -39,10 +39,16 @@ export class ConfigManager {
|
||||
return this.#config
|
||||
}
|
||||
|
||||
constructor({ logger }: { logger: Logger }) {
|
||||
this.#logger = logger
|
||||
get baseDir(): string {
|
||||
return this.#baseDir
|
||||
}
|
||||
|
||||
get isProduction(): boolean {
|
||||
return this.#isProduction
|
||||
}
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Rejects an error either by throwing when in production or by logging the error as a warning
|
||||
* @param error
|
||||
@@ -53,7 +59,7 @@ export class ConfigManager {
|
||||
throw new Error(`[config] ⚠️ ${error}`)
|
||||
}
|
||||
|
||||
this.#logger.warn(error)
|
||||
logger.warn(error)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,17 +147,25 @@ export class ConfigManager {
|
||||
/**
|
||||
* Prepare the full configuration after validation and normalization
|
||||
*/
|
||||
loadConfig(rawConfig: Partial<ConfigModule> = {}): ConfigModule {
|
||||
const projectConfig = this.normalizeProjectConfig(
|
||||
rawConfig.projectConfig ?? {}
|
||||
loadConfig({
|
||||
projectConfig = {},
|
||||
baseDir,
|
||||
}: {
|
||||
projectConfig: Partial<ConfigModule>
|
||||
baseDir: string
|
||||
}): ConfigModule {
|
||||
this.#baseDir = baseDir
|
||||
|
||||
const normalizedProjectConfig = this.normalizeProjectConfig(
|
||||
projectConfig.projectConfig ?? {}
|
||||
)
|
||||
|
||||
this.#config = {
|
||||
projectConfig,
|
||||
admin: rawConfig.admin ?? {},
|
||||
modules: rawConfig.modules ?? {},
|
||||
featureFlags: rawConfig.featureFlags ?? {},
|
||||
plugins: rawConfig.plugins ?? [],
|
||||
projectConfig: normalizedProjectConfig,
|
||||
admin: projectConfig.admin ?? {},
|
||||
modules: projectConfig.modules ?? {},
|
||||
featureFlags: projectConfig.featureFlags ?? {},
|
||||
plugins: projectConfig.plugins ?? [],
|
||||
}
|
||||
|
||||
return this.#config
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { ConfigModule } from "./types"
|
||||
import { getConfigFile } from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, getConfigFile } from "@medusajs/utils"
|
||||
import { logger } from "../logger"
|
||||
import { ConfigManager } from "./config"
|
||||
import { container } from "../container"
|
||||
import { asFunction } from "awilix"
|
||||
|
||||
const handleConfigError = (error: Error): void => {
|
||||
logger.error(`Error in loading config: ${error.message}`)
|
||||
@@ -12,9 +14,12 @@ const handleConfigError = (error: Error): void => {
|
||||
}
|
||||
|
||||
// TODO: Later on we could store the config manager into the unique container
|
||||
export const configManager = new ConfigManager({
|
||||
logger,
|
||||
})
|
||||
export const configManager = new ConfigManager()
|
||||
|
||||
container.register(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE,
|
||||
asFunction(() => configManager)
|
||||
)
|
||||
|
||||
/**
|
||||
* Loads the config file and returns the config module after validating, normalizing the configurations
|
||||
@@ -35,5 +40,8 @@ export function configLoader(
|
||||
handleConfigError(error)
|
||||
}
|
||||
|
||||
return configManager.loadConfig(configModule)
|
||||
return configManager.loadConfig({
|
||||
projectConfig: configModule,
|
||||
baseDir: entryDirectory,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
|
||||
export const container = createMedusaContainer()
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./pg-connection-loader"
|
||||
+11
-8
@@ -1,17 +1,20 @@
|
||||
import { ConfigModule } from "@medusajs/framework"
|
||||
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { asValue, AwilixContainer } from "awilix"
|
||||
import { asValue } from "awilix"
|
||||
import { container } from "../container"
|
||||
import { configManager } from "../config"
|
||||
|
||||
type Options = {
|
||||
container: AwilixContainer
|
||||
configModule: ConfigModule
|
||||
}
|
||||
|
||||
export default async ({ container, configModule }: Options): Promise<any> => {
|
||||
/**
|
||||
* Initialize a knex connection that can then be shared to any resources if needed
|
||||
*/
|
||||
export function pgConnectionLoader(): ReturnType<
|
||||
typeof ModulesSdkUtils.createPgConnection
|
||||
> {
|
||||
if (container.hasRegistration(ContainerRegistrationKeys.PG_CONNECTION)) {
|
||||
return container.resolve(ContainerRegistrationKeys.PG_CONNECTION)
|
||||
}
|
||||
|
||||
const configModule = configManager.config
|
||||
|
||||
// Share a knex connection to be consumed by the shared modules
|
||||
const connectionString = configModule.projectConfig.databaseUrl
|
||||
const driverOptions: any =
|
||||
+12
-19
@@ -1,4 +1,3 @@
|
||||
import { ConfigModule } from "@medusajs/types"
|
||||
import createStore from "connect-redis"
|
||||
import cookieParser from "cookie-parser"
|
||||
import express, { Express } from "express"
|
||||
@@ -6,27 +5,21 @@ import session from "express-session"
|
||||
import Redis from "ioredis"
|
||||
import morgan from "morgan"
|
||||
import path from "path"
|
||||
import { configManager } from "../config"
|
||||
|
||||
type Options = {
|
||||
app: Express
|
||||
configModule: ConfigModule
|
||||
rootDirectory: string
|
||||
}
|
||||
|
||||
export default async ({
|
||||
app,
|
||||
configModule,
|
||||
rootDirectory,
|
||||
}: Options): Promise<{
|
||||
export async function expressLoader({ app }: { app: Express }): Promise<{
|
||||
app: Express
|
||||
shutdown: () => Promise<void>
|
||||
}> => {
|
||||
}> {
|
||||
const baseDir = configManager.baseDir
|
||||
const configModule = configManager.config
|
||||
const isProduction = configManager.isProduction
|
||||
const isStaging = process.env.NODE_ENV === "staging"
|
||||
const isTest = process.env.NODE_ENV === "test"
|
||||
|
||||
let sameSite: string | boolean = false
|
||||
let secure = false
|
||||
if (
|
||||
process.env.NODE_ENV === "production" ||
|
||||
process.env.NODE_ENV === "staging"
|
||||
) {
|
||||
if (isProduction || isStaging) {
|
||||
secure = true
|
||||
sameSite = "none"
|
||||
}
|
||||
@@ -64,14 +57,14 @@ export default async ({
|
||||
app.set("trust proxy", 1)
|
||||
app.use(
|
||||
morgan("combined", {
|
||||
skip: () => process.env.NODE_ENV === "test",
|
||||
skip: () => isTest,
|
||||
})
|
||||
)
|
||||
app.use(cookieParser())
|
||||
app.use(session(sessionOpts))
|
||||
|
||||
// Currently we don't allow configuration of static files, but this can be revisited as needed.
|
||||
app.use("/static", express.static(path.join(rootDirectory, "static")))
|
||||
app.use("/static", express.static(path.join(baseDir, "static")))
|
||||
|
||||
app.get("/health", (req, res) => {
|
||||
res.status(200).send("OK")
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./express-loader"
|
||||
@@ -1,2 +1,5 @@
|
||||
export * from "./config"
|
||||
export * from "./logger"
|
||||
export * from "./http"
|
||||
export * from "./database"
|
||||
export * from "./container"
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
"@inquirer/checkbox": "^2.3.11",
|
||||
"@medusajs/admin-sdk": "0.0.1",
|
||||
"@medusajs/core-flows": "^0.0.9",
|
||||
"@medusajs/framework": "^0.0.1-preview-20240724081425",
|
||||
"@medusajs/framework": "^0.0.1",
|
||||
"@medusajs/link-modules": "^0.2.11",
|
||||
"@medusajs/medusa-cli": "^1.3.22",
|
||||
"@medusajs/modules-sdk": "^1.12.11",
|
||||
|
||||
@@ -5,7 +5,7 @@ import Store from "medusa-telemetry/dist/store"
|
||||
import { EOL } from "os"
|
||||
import path from "path"
|
||||
|
||||
import Logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
|
||||
const defaultConfig = {
|
||||
padding: 5,
|
||||
@@ -57,8 +57,9 @@ export default async function ({ port, directory }) {
|
||||
execArgv: argv,
|
||||
})
|
||||
this.childProcess.on("error", (error) => {
|
||||
Logger.error("Dev server failed to start", error)
|
||||
Logger.info("The server will restart automatically after your changes")
|
||||
// @ts-ignore
|
||||
logger.error("Dev server failed to start", error)
|
||||
logger.info("The server will restart automatically after your changes")
|
||||
})
|
||||
},
|
||||
|
||||
@@ -100,26 +101,26 @@ export default async function ({ port, directory }) {
|
||||
})
|
||||
|
||||
this.watcher.on("add", (file) => {
|
||||
Logger.info(
|
||||
logger.info(
|
||||
`${path.relative(directory, file)} created: Restarting dev server`
|
||||
)
|
||||
this.restart()
|
||||
})
|
||||
this.watcher.on("change", (file) => {
|
||||
Logger.info(
|
||||
logger.info(
|
||||
`${path.relative(directory, file)} modified: Restarting dev server`
|
||||
)
|
||||
this.restart()
|
||||
})
|
||||
this.watcher.on("unlink", (file) => {
|
||||
Logger.info(
|
||||
logger.info(
|
||||
`${path.relative(directory, file)} removed: Restarting dev server`
|
||||
)
|
||||
this.restart()
|
||||
})
|
||||
|
||||
this.watcher.on("ready", function () {
|
||||
Logger.info(`Watching filesystem to reload dev server on file change`)
|
||||
logger.info(`Watching filesystem to reload dev server on file change`)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import loaders from "../loaders"
|
||||
import express from "express"
|
||||
import path from "path"
|
||||
import { existsSync } from "fs"
|
||||
import logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { ExecArgs } from "@medusajs/types"
|
||||
|
||||
type Options = {
|
||||
|
||||
@@ -2,7 +2,7 @@ import boxen from "boxen"
|
||||
import chalk from "chalk"
|
||||
import checkbox from "@inquirer/checkbox"
|
||||
|
||||
import Logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { initializeContainer } from "../loaders"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { getResolvedPlugins } from "../loaders/helpers/resolve-plugins"
|
||||
@@ -96,7 +96,7 @@ const main = async function ({ directory }) {
|
||||
}
|
||||
|
||||
try {
|
||||
const container = await initializeContainer(directory)
|
||||
const container = initializeContainer(directory)
|
||||
|
||||
const configModule = container.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
@@ -111,7 +111,7 @@ const main = async function ({ directory }) {
|
||||
container,
|
||||
})
|
||||
|
||||
Logger.info("Syncing links...")
|
||||
logger.info("Syncing links...")
|
||||
|
||||
const actionPlan = await planner.createPlan()
|
||||
const groupActionPlan = groupByActionPlan(actionPlan)
|
||||
@@ -162,13 +162,13 @@ const main = async function ({ directory }) {
|
||||
}
|
||||
|
||||
if (actionsToExecute.length) {
|
||||
Logger.info("Links sync completed")
|
||||
logger.info("Links sync completed")
|
||||
} else {
|
||||
Logger.info("Database already up-to-date")
|
||||
logger.info("Database already up-to-date")
|
||||
}
|
||||
process.exit()
|
||||
} catch (e) {
|
||||
Logger.error(e)
|
||||
logger.error(e)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { runMedusaAppMigrations } from "../loaders/medusa-app"
|
||||
import { initializeContainer } from "../loaders"
|
||||
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
|
||||
@@ -19,17 +19,17 @@ function validateInputArgs({
|
||||
const actionsRequiringModules = ["revert", "generate"]
|
||||
|
||||
if (modules.length && !actionsRequiringModules.includes(action)) {
|
||||
Logger.error(
|
||||
logger.error(
|
||||
`<modules> cannot be specified with the "${action}" action. Please remove the <modules> argument and try again.`
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
if (!modules.length && actionsRequiringModules.includes(action)) {
|
||||
Logger.error(
|
||||
logger.error(
|
||||
"Please provide the modules for which you want to revert migrations"
|
||||
)
|
||||
Logger.error(`For example: "npx medusa migration revert <moduleName>"`)
|
||||
logger.error(`For example: "npx medusa migration revert <moduleName>"`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ const main = async function ({ directory }) {
|
||||
|
||||
validateInputArgs({ action, modules })
|
||||
|
||||
const container = await initializeContainer(directory)
|
||||
const container = initializeContainer(directory)
|
||||
|
||||
const configModule = container.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
@@ -55,7 +55,7 @@ const main = async function ({ directory }) {
|
||||
const pluginLinks = await resolvePluginsLinks(plugins, container)
|
||||
|
||||
if (action === "run") {
|
||||
Logger.info("Running migrations...")
|
||||
logger.info("Running migrations...")
|
||||
|
||||
await runMedusaAppMigrations({
|
||||
configModule,
|
||||
@@ -65,10 +65,10 @@ const main = async function ({ directory }) {
|
||||
})
|
||||
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
Logger.info("Migrations completed")
|
||||
logger.info("Migrations completed")
|
||||
process.exit()
|
||||
} else if (action === "revert") {
|
||||
Logger.info("Reverting migrations...")
|
||||
logger.info("Reverting migrations...")
|
||||
|
||||
try {
|
||||
await runMedusaAppMigrations({
|
||||
@@ -79,23 +79,23 @@ const main = async function ({ directory }) {
|
||||
action: "revert",
|
||||
})
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
Logger.info("Migrations reverted")
|
||||
logger.info("Migrations reverted")
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
if (error.code && error.code === MedusaError.Codes.UNKNOWN_MODULES) {
|
||||
Logger.error(error.message)
|
||||
logger.error(error.message)
|
||||
const modulesList = error.allModules.map(
|
||||
(name: string) => ` - ${name}`
|
||||
)
|
||||
Logger.error(`Available modules:\n${modulesList.join("\n")}`)
|
||||
logger.error(`Available modules:\n${modulesList.join("\n")}`)
|
||||
} else {
|
||||
Logger.error(error.message, error)
|
||||
logger.error(error.message, error)
|
||||
}
|
||||
process.exit(1)
|
||||
}
|
||||
} else if (action === "generate") {
|
||||
Logger.info("Generating migrations...")
|
||||
logger.info("Generating migrations...")
|
||||
|
||||
await runMedusaAppMigrations({
|
||||
moduleNames: modules,
|
||||
@@ -106,10 +106,10 @@ const main = async function ({ directory }) {
|
||||
})
|
||||
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
Logger.info("Migrations generated")
|
||||
logger.info("Migrations generated")
|
||||
process.exit()
|
||||
} else if (action === "show") {
|
||||
Logger.info("Action not supported yet")
|
||||
logger.info("Action not supported yet")
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import { scheduleJob } from "node-schedule"
|
||||
import os from "os"
|
||||
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { isPresent, GracefulShutdownServer } from "@medusajs/utils"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { GracefulShutdownServer, isPresent } from "@medusajs/utils"
|
||||
|
||||
const EVERY_SIXTH_HOUR = "0 */6 * * *"
|
||||
const CRON_SCHEDULE = EVERY_SIXTH_HOUR
|
||||
@@ -58,10 +58,10 @@ export default async function ({ port, cpus, directory }) {
|
||||
directory,
|
||||
expressApp: app,
|
||||
})
|
||||
const serverActivity = Logger.activity(`Creating server`)
|
||||
const serverActivity = logger.activity(`Creating server`)
|
||||
const server = GracefulShutdownServer.create(
|
||||
app.listen(port).on("listening", () => {
|
||||
Logger.success(serverActivity, `Server is ready on port: ${port}`)
|
||||
logger.success(serverActivity, `Server is ready on port: ${port}`)
|
||||
track("CLI_START_COMPLETED")
|
||||
})
|
||||
)
|
||||
|
||||
@@ -6,7 +6,7 @@ import { track } from "medusa-telemetry"
|
||||
import { scheduleJob } from "node-schedule"
|
||||
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { GracefulShutdownServer } from "@medusajs/utils"
|
||||
|
||||
const EVERY_SIXTH_HOUR = "0 */6 * * *"
|
||||
@@ -24,17 +24,17 @@ export default async function ({ port, directory }) {
|
||||
expressApp: app,
|
||||
})
|
||||
|
||||
const serverActivity = Logger.activity(`Creating server`)
|
||||
const serverActivity = logger.activity(`Creating server`)
|
||||
const server = GracefulShutdownServer.create(
|
||||
app.listen(port).on("listening", () => {
|
||||
Logger.success(serverActivity, `Server is ready on port: ${port}`)
|
||||
logger.success(serverActivity, `Server is ready on port: ${port}`)
|
||||
track("CLI_START_COMPLETED")
|
||||
})
|
||||
)
|
||||
|
||||
// Handle graceful shutdown
|
||||
const gracefulShutDown = () => {
|
||||
Logger.info("Gracefully shutting down server")
|
||||
logger.info("Gracefully shutting down server")
|
||||
server
|
||||
.shutdown()
|
||||
.then(async () => {
|
||||
@@ -42,7 +42,7 @@ export default async function ({ port, directory }) {
|
||||
process.exit(0)
|
||||
})
|
||||
.catch((e) => {
|
||||
Logger.error("Error received when shutting down the server.", e)
|
||||
logger.error("Error received when shutting down the server.", e)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
@@ -56,7 +56,7 @@ export default async function ({ port, directory }) {
|
||||
|
||||
return { server }
|
||||
} catch (err) {
|
||||
Logger.error("Error starting server", err)
|
||||
logger.error("Error starting server", err)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { track } from "medusa-telemetry"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
|
||||
export default async function ({
|
||||
directory,
|
||||
@@ -38,7 +38,7 @@ export default async function ({
|
||||
if (invite) {
|
||||
const invite = await userService.createInvites({ email })
|
||||
|
||||
Logger.info(`
|
||||
logger.info(`
|
||||
Invite token: ${invite.token}
|
||||
Open the invite in Medusa Admin at: [your-admin-url]/invite?token=${invite.token}`)
|
||||
} else {
|
||||
@@ -52,7 +52,7 @@ export default async function ({
|
||||
})
|
||||
|
||||
if (error) {
|
||||
Logger.error(error)
|
||||
logger.error(error)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {
|
||||
StepResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
StepResponse,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { glob } from "glob"
|
||||
import logger from "../logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
|
||||
|
||||
export const registerJobs = async (plugins) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { readdir } from "fs/promises"
|
||||
import { extname, join, sep } from "path"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { authenticate, errorHandler } from "../../../utils/middlewares"
|
||||
import logger from "../../logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
import {
|
||||
AsyncRouteHandler,
|
||||
GlobalMiddlewareDescriptor,
|
||||
|
||||
@@ -9,7 +9,7 @@ import { readdir } from "fs/promises"
|
||||
import { extname, join, sep } from "path"
|
||||
|
||||
import { SubscriberArgs, SubscriberConfig } from "../../../types/subscribers"
|
||||
import logger from "../../logger"
|
||||
import { logger } from "@medusajs/framework"
|
||||
|
||||
type SubscriberHandler<T> = (args: SubscriberArgs<T>) => Promise<void>
|
||||
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import { createDefaultsWorkflow } from "@medusajs/core-flows"
|
||||
import { ConfigModule, MedusaContainer, PluginDetails } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
createMedusaContainer,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, promiseAll } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import { Express, NextFunction, Request, Response } from "express"
|
||||
import path from "path"
|
||||
@@ -12,8 +8,13 @@ import requestIp from "request-ip"
|
||||
import { v4 } from "uuid"
|
||||
import adminLoader from "./admin"
|
||||
import apiLoader from "./api"
|
||||
import { configLoader, logger } from "@medusajs/framework"
|
||||
import expressLoader from "./express"
|
||||
import {
|
||||
configLoader,
|
||||
container,
|
||||
expressLoader,
|
||||
logger,
|
||||
pgConnectionLoader,
|
||||
} from "@medusajs/framework"
|
||||
import featureFlagsLoader from "./feature-flags"
|
||||
import { registerJobs } from "./helpers/register-jobs"
|
||||
import { registerWorkflows } from "./helpers/register-workflows"
|
||||
@@ -21,7 +22,6 @@ import { getResolvedPlugins } from "./helpers/resolve-plugins"
|
||||
import { resolvePluginsLinks } from "./helpers/resolve-plugins-links"
|
||||
import { SubscriberLoader } from "./helpers/subscribers"
|
||||
import loadMedusaApp from "./medusa-app"
|
||||
import registerPgConnection from "./pg-connection"
|
||||
|
||||
type Options = {
|
||||
directory: string
|
||||
@@ -93,9 +93,8 @@ async function loadEntrypoints(
|
||||
|
||||
const { shutdown } = await expressLoader({
|
||||
app: expressApp,
|
||||
configModule,
|
||||
rootDirectory,
|
||||
})
|
||||
|
||||
expressApp.use((req: Request, res: Response, next: NextFunction) => {
|
||||
req.scope = container.createScope() as MedusaContainer
|
||||
req.requestId = (req.headers["x-request-id"] as string) ?? v4()
|
||||
@@ -121,8 +120,7 @@ async function loadEntrypoints(
|
||||
return shutdown
|
||||
}
|
||||
|
||||
export async function initializeContainer(rootDirectory: string) {
|
||||
const container = createMedusaContainer()
|
||||
export function initializeContainer(rootDirectory: string): MedusaContainer {
|
||||
const configModule = configLoader(rootDirectory, "medusa-config.js")
|
||||
const featureFlagRouter = featureFlagsLoader(configModule, logger)
|
||||
|
||||
@@ -133,7 +131,7 @@ export async function initializeContainer(rootDirectory: string) {
|
||||
[ContainerRegistrationKeys.REMOTE_QUERY]: asValue(null),
|
||||
})
|
||||
|
||||
await registerPgConnection({ container, configModule })
|
||||
pgConnectionLoader()
|
||||
return container
|
||||
}
|
||||
|
||||
@@ -145,7 +143,7 @@ export default async ({
|
||||
app: Express
|
||||
shutdown: () => Promise<void>
|
||||
}> => {
|
||||
const container = await initializeContainer(rootDirectory)
|
||||
const container = initializeContainer(rootDirectory)
|
||||
const configModule = container.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
)
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
import logger from "@medusajs/medusa-cli/dist/reporter"
|
||||
|
||||
export default logger
|
||||
@@ -1,63 +0,0 @@
|
||||
import { asValue } from "awilix"
|
||||
import Redis from "ioredis"
|
||||
import FakeRedis from "ioredis-mock"
|
||||
import { EOL } from "os"
|
||||
import { Logger, MedusaContainer } from "../types/global"
|
||||
import { ConfigModule } from "@medusajs/types"
|
||||
|
||||
type Options = {
|
||||
container: MedusaContainer
|
||||
configModule: ConfigModule
|
||||
logger: Logger
|
||||
}
|
||||
|
||||
// TODO: Will be removed when the strict dependency on Redis in the core is removed
|
||||
async function redisLoader({
|
||||
container,
|
||||
configModule,
|
||||
logger,
|
||||
}: Options): Promise<{ shutdown: () => Promise<void> }> {
|
||||
let client!: Redis | FakeRedis
|
||||
|
||||
if (configModule.projectConfig.redisUrl) {
|
||||
client = new Redis(configModule.projectConfig.redisUrl, {
|
||||
// Lazy connect to properly handle connection errors
|
||||
lazyConnect: true,
|
||||
...(configModule.projectConfig.redisOptions ?? {}),
|
||||
})
|
||||
|
||||
try {
|
||||
await client.connect()
|
||||
logger?.info(`Connection to Redis established`)
|
||||
} catch (err) {
|
||||
logger?.error(`An error occurred while connecting to Redis:${EOL} ${err}`)
|
||||
}
|
||||
|
||||
container.register({
|
||||
redisClient: asValue(client),
|
||||
})
|
||||
} else {
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
logger.warn(
|
||||
`No Redis url was provided - using Medusa in production without a proper Redis instance is not recommended`
|
||||
)
|
||||
}
|
||||
|
||||
logger.info("Using fake Redis")
|
||||
|
||||
// Economical way of dealing with redis clients
|
||||
client = new FakeRedis()
|
||||
|
||||
container.register({
|
||||
redisClient: asValue(client),
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
shutdown: async () => {
|
||||
client.disconnect()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default redisLoader
|
||||
Reference in New Issue
Block a user