chore: Fixes somes command and migrate to ts (#7444)

**What**
Fix some commands that are using the loaders return unproperly and cleanup, also includes a migration to ts
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-05-24 14:17:58 +00:00
1 parent 7390821da5
commit d354b253d5
8 files changed
+56 -103

No files matched your search

@@ -30,7 +30,7 @@ if (!IS_DEV) {
if (LOG_FILE) { if (LOG_FILE) {
transports.push( transports.push(
new winston.transports.File({ new winston.transports.File({
filename: LOG_FILE filename: LOG_FILE,
}) })
) )
} }
@@ -172,7 +172,7 @@ export class Reporter {
* message to log the error under; or an error object. * message to log the error under; or an error object.
* @param {Error?} error - an error object to log message with * @param {Error?} error - an error object to log message with
*/ */
error = (messageOrError, error = null) => { error = (messageOrError, error: any = null) => {
let message = messageOrError let message = messageOrError
if (typeof messageOrError === "object") { if (typeof messageOrError === "object") {
message = messageOrError.message message = messageOrError.message
@@ -1,6 +1,6 @@
import boxen from "boxen" import boxen from "boxen"
import { execSync, fork } from "child_process" import { ChildProcess, execSync, fork } from "child_process"
import chokidar from "chokidar" import chokidar, { FSWatcher } from "chokidar"
import Store from "medusa-telemetry/dist/store" import Store from "medusa-telemetry/dist/store"
import { EOL } from "os" import { EOL } from "os"
import path from "path" import path from "path"
@@ -11,7 +11,7 @@ const defaultConfig = {
padding: 5, padding: 5,
borderColor: `blue`, borderColor: `blue`,
borderStyle: `double`, borderStyle: `double`,
} } as boxen.Options
export default async function ({ port, directory }) { export default async function ({ port, directory }) {
const args = process.argv const args = process.argv
@@ -35,8 +35,8 @@ export default async function ({ port, directory }) {
) )
const devServer = { const devServer = {
childProcess: null, childProcess: null as ChildProcess | null,
watcher: null, watcher: null as FSWatcher | null,
/** /**
* Start the development server by forking a new process. * Start the development server by forking a new process.
-76
View File
@@ -1,76 +0,0 @@
import { asValue } from "awilix"
import { revertIsolatedModulesMigration } from "./utils/get-migrations"
import {
ContainerRegistrationKeys,
createMedusaContainer,
} from "@medusajs/utils"
import configModuleLoader from "../loaders/config"
import featureFlagLoader from "../loaders/feature-flags"
import Logger from "../loaders/logger"
import {
loadMedusaApp,
migrateMedusaApp,
revertMedusaApp,
} from "../loaders/medusa-app"
import pgConnectionLoader from "../loaders/pg-connection"
const runLinkMigrations = async (directory) => {
const configModule = configModuleLoader(directory)
const container = createMedusaContainer()
const featureFlagRouter = featureFlagLoader(configModule)
container.register({
featureFlagRouter: asValue(featureFlagRouter),
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
})
await pgConnectionLoader({ configModule, container })
const { runMigrations } = await loadMedusaApp(
{ configModule, container },
{ registerInContainer: false }
)
const options = {
database: {
clientUrl: configModule.projectConfig.database_url,
},
}
await runMigrations(options)
}
const main = async function ({ directory }) {
const args = process.argv
args.shift()
args.shift()
args.shift()
const configModule = configModuleLoader(directory)
const featureFlagRouter = featureFlagLoader(configModule)
const container = createMedusaContainer()
const pgConnection = await pgConnectionLoader({ configModule, container })
container.register({
[ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule),
[ContainerRegistrationKeys.LOGGER]: asValue(Logger),
[ContainerRegistrationKeys.PG_CONNECTION]: asValue(pgConnection),
[ContainerRegistrationKeys.FEATURE_FLAG_ROUTER]: asValue(featureFlagRouter),
})
if (args[0] === "run") {
await migrateMedusaApp({ configModule, container })
Logger.info("Migrations completed.")
process.exit()
} else if (args[0] === "revert") {
await revertMedusaApp({ configModule, container })
await revertIsolatedModulesMigration(configModule)
Logger.info("Migrations reverted.")
} else if (args[0] === "show") {
Logger.info("not supported")
process.exit(0)
}
}
export default main
+34
View File
@@ -0,0 +1,34 @@
import { revertIsolatedModulesMigration } from "./utils/get-migrations"
import Logger from "../loaders/logger"
import { migrateMedusaApp, revertMedusaApp } from "../loaders/medusa-app"
import { initializeContainer } from "../loaders"
import { ContainerRegistrationKeys } from "@medusajs/utils"
const main = async function ({ directory }) {
const args = process.argv
args.shift()
args.shift()
args.shift()
const container = await initializeContainer(directory)
const configModule = container.resolve(
ContainerRegistrationKeys.CONFIG_MODULE
)
if (args[0] === "run") {
await migrateMedusaApp({ configModule, container })
Logger.info("Migrations completed.")
process.exit()
} else if (args[0] === "revert") {
await revertMedusaApp({ configModule, container })
await revertIsolatedModulesMigration(configModule)
Logger.info("Migrations reverted.")
} else if (args[0] === "show") {
Logger.info("not supported")
process.exit(0)
}
}
export default main
@@ -10,6 +10,7 @@ import os from "os"
import loaders from "../loaders" import loaders from "../loaders"
import Logger from "../loaders/logger" import Logger from "../loaders/logger"
import { isPresent } from "@medusajs/utils"
const EVERY_SIXTH_HOUR = "0 */6 * * *" const EVERY_SIXTH_HOUR = "0 */6 * * *"
const CRON_SCHEDULE = EVERY_SIXTH_HOUR const CRON_SCHEDULE = EVERY_SIXTH_HOUR
@@ -30,15 +31,15 @@ export default async function ({ port, cpus, directory }) {
cluster.on("exit", (worker) => { cluster.on("exit", (worker) => {
if (!isShuttingDown) { if (!isShuttingDown) {
cluster.fork() cluster.fork()
} else if (Object.keys(cluster.workers).length === 0) { } else if (!isPresent(cluster.workers)) {
setTimeout(killMainProccess, 100) setTimeout(killMainProccess, 100)
} }
}) })
const gracefulShutDown = () => { const gracefulShutDown = () => {
isShuttingDown = true isShuttingDown = true
for (const id of Object.keys(cluster.workers)) { for (const id of Object.keys(cluster.workers ?? {})) {
cluster.workers[id].kill("SIGTERM") cluster.workers?.[id]?.kill("SIGTERM")
} }
} }
@@ -54,13 +55,13 @@ export default async function ({ port, cpus, directory }) {
const app = express() const app = express()
const { dbConnection, shutdown, prepareShutdown } = await loaders({ const { shutdown } = await loaders({
directory, directory,
expressApp: app, expressApp: app,
}) })
const serverActivity = Logger.activity(`Creating server`) const serverActivity = Logger.activity(`Creating server`)
const server = GracefulShutdownServer.create( const server = GracefulShutdownServer.create(
app.listen(port, (err) => { app.listen(port).on("error", (err) => {
if (err) { if (err) {
return return
} }
@@ -72,9 +73,6 @@ export default async function ({ port, cpus, directory }) {
const gracefulShutDown = () => { const gracefulShutDown = () => {
server server
.shutdown() .shutdown()
.then(async () => {
return await prepareShutdown()
})
.then(async () => { .then(async () => {
await shutdown() await shutdown()
process.exit(0) process.exit(0)
@@ -87,12 +85,12 @@ export default async function ({ port, cpus, directory }) {
process.on("SIGTERM", gracefulShutDown) process.on("SIGTERM", gracefulShutDown)
process.on("SIGINT", gracefulShutDown) process.on("SIGINT", gracefulShutDown)
return { dbConnection, server } return { server }
} }
process.on("message", async (msg) => { process.on("message", async (msg: any) => {
if (msg.index > 0) { if (msg.index > 0) {
process.env.PLUGIN_ADMIN_UI_SKIP_CACHE = true process.env.PLUGIN_ADMIN_UI_SKIP_CACHE = "true"
} }
await start() await start()
@@ -19,14 +19,14 @@ export default async function ({ port, directory }) {
const app = express() const app = express()
try { try {
const { dbConnection, shutdown, prepareShutdown } = await loaders({ const { shutdown } = await loaders({
directory, directory,
expressApp: app, expressApp: app,
}) })
const serverActivity = Logger.activity(`Creating server`) const serverActivity = Logger.activity(`Creating server`)
const server = GracefulShutdownServer.create( const server = GracefulShutdownServer.create(
app.listen(port, (err) => { app.listen(port).on("error", (err) => {
if (err) { if (err) {
return return
} }
@@ -40,9 +40,6 @@ export default async function ({ port, directory }) {
Logger.info("Gracefully shutting down server") Logger.info("Gracefully shutting down server")
server server
.shutdown() .shutdown()
.then(async () => {
return await prepareShutdown()
})
.then(async () => { .then(async () => {
await shutdown() await shutdown()
process.exit(0) process.exit(0)
@@ -60,7 +57,7 @@ export default async function ({ port, directory }) {
track("PING") track("PING")
}) })
return { dbConnection, server } return { server }
} catch (err) { } catch (err) {
Logger.error("Error starting server", err) Logger.error("Error starting server", err)
process.exit(1) process.exit(1)
@@ -6,7 +6,7 @@ import { track } from "medusa-telemetry"
import loaders from "../loaders" import loaders from "../loaders"
import Logger from "../loaders/logger" import Logger from "../loaders/logger"
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export default async function ({ export default async function ({
directory, directory,
@@ -53,7 +53,7 @@ export default async function ({
if (error) { if (error) {
Logger.error(error) Logger.error(error)
throw new Error(error) process.exit(1)
} }
await authService.update({ await authService.update({
+1 -1
View File
@@ -103,7 +103,7 @@ async function loadEntrypoints(
return shutdown return shutdown
} }
async function initializeContainer(rootDirectory: string) { export async function initializeContainer(rootDirectory: string) {
const container = createMedusaContainer() const container = createMedusaContainer()
const configModule = loadConfig(rootDirectory) const configModule = loadConfig(rootDirectory)
const featureFlagRouter = featureFlagsLoader(configModule, Logger) const featureFlagRouter = featureFlagsLoader(configModule, Logger)