Skip to content

Commit

Permalink
Merge branch 'develop' into feat/store-region
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed May 21, 2024
2 parents 330beaf + 73c917f commit 5a1e2ac
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/cli/medusa-cli/src/create-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 0 deletions packages/core/types/src/common/medusa-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MedusaContainer } from "./medusa-container"

export type ExecArgs = {
container: MedusaContainer
args: string[]
}
42 changes: 42 additions & 0 deletions packages/medusa/src/commands/exec.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 0 additions & 1 deletion packages/medusa/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { SubscriberLoader } from "./helpers/subscribers"
type Options = {
directory: string
expressApp: Express
isTest: boolean
}

const isWorkerMode = (configModule) => {
Expand Down

0 comments on commit 5a1e2ac

Please sign in to comment.