feat(medusa,medusa-cli): add an exec command (#7376)

This commit is contained in:
Shahed Nasser
2024-05-21 11:56:44 +03:00
committed by GitHub
parent 442b0b2038
commit 73c917fdce
5 changed files with 62 additions and 1 deletions

View File

@@ -321,6 +321,19 @@ function buildLocalCommands(cli, isLocalProject) {
})
),
})
.command({
command: `exec [file] [args..]`,
desc: `Run a function defined in a file.`,
handler: handlerP(
getCommandHandler(`exec`, (args, cmd) => {
cmd(args)
// Return an empty promise to prevent handlerP from exiting early.
// The development server shouldn't ever exit until the user directly
// kills it so this is fine.
return new Promise((resolve) => {})
})
),
})
}
function isLocalMedusaProject() {

View File

@@ -2,5 +2,6 @@ export * from "./common"
export * from "./rule"
export * from "./batch"
export * from "./config-module"
export * from "./medusa-cli"
export * from "./medusa-container"
export * from "./with-calculated"

View File

@@ -0,0 +1,6 @@
import { MedusaContainer } from "./medusa-container"
export type ExecArgs = {
container: MedusaContainer
args: string[]
}

View File

@@ -0,0 +1,42 @@
import loaders from "../loaders"
import express from "express"
import path from "path"
import logger from "../loaders/logger"
import { ExecArgs } from "@medusajs/types"
type Options = {
file: string
args: string[]
}
export default async function script({ file, args }: Options) {
logger.info(`Executing script at ${file}...`)
const app = express()
const directory = process.cwd()
try {
// set worker mode
process.env.MEDUSA_WORKER_MODE = "worker"
const { container } = await loaders({
directory,
expressApp: app,
})
const scriptFile = (await import(path.resolve(directory, file))).default
const scriptParams: ExecArgs = {
container,
args,
}
await scriptFile(scriptParams)
logger.info(`Finished executing script.`)
process.exit()
} catch (err) {
logger.error("Error running script", err)
process.exit(1)
}
}

View File

@@ -22,7 +22,6 @@ import { SubscriberLoader } from "./helpers/subscribers"
type Options = {
directory: string
expressApp: Express
isTest: boolean
}
const isWorkerMode = (configModule) => {