feat(medusa): add error handling to exec script (#7394)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import loaders from "../loaders"
|
import loaders from "../loaders"
|
||||||
import express from "express"
|
import express from "express"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
|
import { existsSync } from "fs"
|
||||||
import logger from "../loaders/logger"
|
import logger from "../loaders/logger"
|
||||||
import { ExecArgs } from "@medusajs/types"
|
import { ExecArgs } from "@medusajs/types"
|
||||||
|
|
||||||
@@ -9,12 +10,24 @@ type Options = {
|
|||||||
args: string[]
|
args: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function script({ file, args }: Options) {
|
export default async function exec({ file, args }: Options) {
|
||||||
logger.info(`Executing script at ${file}...`)
|
logger.info(`Executing script at ${file}...`)
|
||||||
const app = express()
|
const app = express()
|
||||||
const directory = process.cwd()
|
const directory = process.cwd()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// check if the file exists
|
||||||
|
const filePath = path.resolve(directory, file)
|
||||||
|
if (!existsSync(filePath)) {
|
||||||
|
throw new Error(`File ${filePath} doesn't exist.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const scriptToExec = (await import(path.resolve(filePath))).default
|
||||||
|
|
||||||
|
if (!scriptToExec || typeof scriptToExec !== "function") {
|
||||||
|
throw new Error(`File doesn't default export a function to execute.`)
|
||||||
|
}
|
||||||
|
|
||||||
// set worker mode
|
// set worker mode
|
||||||
process.env.MEDUSA_WORKER_MODE = "worker"
|
process.env.MEDUSA_WORKER_MODE = "worker"
|
||||||
|
|
||||||
@@ -23,14 +36,12 @@ export default async function script({ file, args }: Options) {
|
|||||||
expressApp: app,
|
expressApp: app,
|
||||||
})
|
})
|
||||||
|
|
||||||
const scriptFile = (await import(path.resolve(directory, file))).default
|
|
||||||
|
|
||||||
const scriptParams: ExecArgs = {
|
const scriptParams: ExecArgs = {
|
||||||
container,
|
container,
|
||||||
args,
|
args,
|
||||||
}
|
}
|
||||||
|
|
||||||
await scriptFile(scriptParams)
|
await scriptToExec(scriptParams)
|
||||||
|
|
||||||
logger.info(`Finished executing script.`)
|
logger.info(`Finished executing script.`)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user