|
11 місяців тому | |
---|---|---|
.. | ||
README.md | 11 місяців тому | |
seed.ts | 11 місяців тому |
A custom CLI script is a function to execute through Medusa's CLI tool. This is useful when creating custom Medusa tooling to run as a CLI tool.
To create a custom CLI script, create a TypeScript or JavaScript file under the src/scripts
directory. The file must default export a function.
For example, create the file src/scripts/my-script.ts
with the following content:
import {
ExecArgs,
IProductModuleService
} from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
export default async function myScript ({
container
}: ExecArgs) {
const productModuleService: IProductModuleService =
container.resolve(Modules.PRODUCT)
const [, count] = await productModuleService.listAndCountProducts()
console.log(`You have ${count} product(s)`)
}
The function receives as a parameter an object having a container
property, which is an instance of the Medusa Container. Use it to resolve resources in your Medusa application.
To run the custom CLI script, run the exec
command:
npx medusa exec ./src/scripts/my-script.ts
Your script can accept arguments from the command line. Arguments are passed to the function's object parameter in the args
property.
For example:
import { ExecArgs } from "@medusajs/framework/types"
export default async function myScript ({
args
}: ExecArgs) {
console.log(`The arguments you passed: ${args}`)
}
Then, pass the arguments in the exec
command after the file path:
npx medusa exec ./src/scripts/my-script.ts arg1 arg2