feat(medusa,medusa-cli): add an exec command (#7376)
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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"
|
||||
|
||||
6
packages/core/types/src/common/medusa-cli.ts
Normal file
6
packages/core/types/src/common/medusa-cli.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { MedusaContainer } from "./medusa-container"
|
||||
|
||||
export type ExecArgs = {
|
||||
container: MedusaContainer
|
||||
args: string[]
|
||||
}
|
||||
42
packages/medusa/src/commands/exec.ts
Normal file
42
packages/medusa/src/commands/exec.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ import { SubscriberLoader } from "./helpers/subscribers"
|
||||
type Options = {
|
||||
directory: string
|
||||
expressApp: Express
|
||||
isTest: boolean
|
||||
}
|
||||
|
||||
const isWorkerMode = (configModule) => {
|
||||
|
||||
Reference in New Issue
Block a user