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:
committed by
GitHub
parent
7390821da5
commit
d354b253d5
@@ -30,7 +30,7 @@ if (!IS_DEV) {
|
||||
if (LOG_FILE) {
|
||||
transports.push(
|
||||
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.
|
||||
* @param {Error?} error - an error object to log message with
|
||||
*/
|
||||
error = (messageOrError, error = null) => {
|
||||
error = (messageOrError, error: any = null) => {
|
||||
let message = messageOrError
|
||||
if (typeof messageOrError === "object") {
|
||||
message = messageOrError.message
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import boxen from "boxen"
|
||||
import { execSync, fork } from "child_process"
|
||||
import chokidar from "chokidar"
|
||||
import { ChildProcess, execSync, fork } from "child_process"
|
||||
import chokidar, { FSWatcher } from "chokidar"
|
||||
import Store from "medusa-telemetry/dist/store"
|
||||
import { EOL } from "os"
|
||||
import path from "path"
|
||||
@@ -11,7 +11,7 @@ const defaultConfig = {
|
||||
padding: 5,
|
||||
borderColor: `blue`,
|
||||
borderStyle: `double`,
|
||||
}
|
||||
} as boxen.Options
|
||||
|
||||
export default async function ({ port, directory }) {
|
||||
const args = process.argv
|
||||
@@ -35,8 +35,8 @@ export default async function ({ port, directory }) {
|
||||
)
|
||||
|
||||
const devServer = {
|
||||
childProcess: null,
|
||||
watcher: null,
|
||||
childProcess: null as ChildProcess | null,
|
||||
watcher: null as FSWatcher | null,
|
||||
|
||||
/**
|
||||
* Start the development server by forking a new process.
|
||||
@@ -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
packages/medusa/src/commands/migrate.ts
Normal file
34
packages/medusa/src/commands/migrate.ts
Normal 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 Logger from "../loaders/logger"
|
||||
import { isPresent } from "@medusajs/utils"
|
||||
|
||||
const EVERY_SIXTH_HOUR = "0 */6 * * *"
|
||||
const CRON_SCHEDULE = EVERY_SIXTH_HOUR
|
||||
@@ -30,15 +31,15 @@ export default async function ({ port, cpus, directory }) {
|
||||
cluster.on("exit", (worker) => {
|
||||
if (!isShuttingDown) {
|
||||
cluster.fork()
|
||||
} else if (Object.keys(cluster.workers).length === 0) {
|
||||
} else if (!isPresent(cluster.workers)) {
|
||||
setTimeout(killMainProccess, 100)
|
||||
}
|
||||
})
|
||||
|
||||
const gracefulShutDown = () => {
|
||||
isShuttingDown = true
|
||||
for (const id of Object.keys(cluster.workers)) {
|
||||
cluster.workers[id].kill("SIGTERM")
|
||||
for (const id of Object.keys(cluster.workers ?? {})) {
|
||||
cluster.workers?.[id]?.kill("SIGTERM")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,13 +55,13 @@ export default async function ({ port, cpus, directory }) {
|
||||
|
||||
const app = express()
|
||||
|
||||
const { dbConnection, shutdown, prepareShutdown } = await loaders({
|
||||
const { shutdown } = await loaders({
|
||||
directory,
|
||||
expressApp: app,
|
||||
})
|
||||
const serverActivity = Logger.activity(`Creating server`)
|
||||
const server = GracefulShutdownServer.create(
|
||||
app.listen(port, (err) => {
|
||||
app.listen(port).on("error", (err) => {
|
||||
if (err) {
|
||||
return
|
||||
}
|
||||
@@ -72,9 +73,6 @@ export default async function ({ port, cpus, directory }) {
|
||||
const gracefulShutDown = () => {
|
||||
server
|
||||
.shutdown()
|
||||
.then(async () => {
|
||||
return await prepareShutdown()
|
||||
})
|
||||
.then(async () => {
|
||||
await shutdown()
|
||||
process.exit(0)
|
||||
@@ -87,12 +85,12 @@ export default async function ({ port, cpus, directory }) {
|
||||
process.on("SIGTERM", 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) {
|
||||
process.env.PLUGIN_ADMIN_UI_SKIP_CACHE = true
|
||||
process.env.PLUGIN_ADMIN_UI_SKIP_CACHE = "true"
|
||||
}
|
||||
|
||||
await start()
|
||||
@@ -19,14 +19,14 @@ export default async function ({ port, directory }) {
|
||||
const app = express()
|
||||
|
||||
try {
|
||||
const { dbConnection, shutdown, prepareShutdown } = await loaders({
|
||||
const { shutdown } = await loaders({
|
||||
directory,
|
||||
expressApp: app,
|
||||
})
|
||||
|
||||
const serverActivity = Logger.activity(`Creating server`)
|
||||
const server = GracefulShutdownServer.create(
|
||||
app.listen(port, (err) => {
|
||||
app.listen(port).on("error", (err) => {
|
||||
if (err) {
|
||||
return
|
||||
}
|
||||
@@ -40,9 +40,6 @@ export default async function ({ port, directory }) {
|
||||
Logger.info("Gracefully shutting down server")
|
||||
server
|
||||
.shutdown()
|
||||
.then(async () => {
|
||||
return await prepareShutdown()
|
||||
})
|
||||
.then(async () => {
|
||||
await shutdown()
|
||||
process.exit(0)
|
||||
@@ -60,7 +57,7 @@ export default async function ({ port, directory }) {
|
||||
track("PING")
|
||||
})
|
||||
|
||||
return { dbConnection, server }
|
||||
return { server }
|
||||
} catch (err) {
|
||||
Logger.error("Error starting server", err)
|
||||
process.exit(1)
|
||||
@@ -6,7 +6,7 @@ import { track } from "medusa-telemetry"
|
||||
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
export default async function ({
|
||||
directory,
|
||||
@@ -53,7 +53,7 @@ export default async function ({
|
||||
|
||||
if (error) {
|
||||
Logger.error(error)
|
||||
throw new Error(error)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
await authService.update({
|
||||
@@ -103,7 +103,7 @@ async function loadEntrypoints(
|
||||
return shutdown
|
||||
}
|
||||
|
||||
async function initializeContainer(rootDirectory: string) {
|
||||
export async function initializeContainer(rootDirectory: string) {
|
||||
const container = createMedusaContainer()
|
||||
const configModule = loadConfig(rootDirectory)
|
||||
const featureFlagRouter = featureFlagsLoader(configModule, Logger)
|
||||
|
||||
Reference in New Issue
Block a user