3.11. Custom CLI Scripts

In this chapter, you'll learn how to create and execute custom scripts using Medusa's CLI tool.

What is a Custom CLI Script?#

A custom CLI script is a function that you can execute using Medusa's CLI tool. It is useful when you need a script that has access to the Medusa container and can be executed using Medusa's CLI.

For example, you can create a custom CLI script that:


How to Create a Custom CLI Script?#

To create a custom CLI script, create a TypeScript or JavaScript file under the src/scripts directory. The file must export a function by default.

For example, create the file src/scripts/my-script.ts with the following content:

src/scripts/my-script.ts
1import { 2  ExecArgs,3  IProductModuleService,4} from "@medusajs/framework/types"5import { Modules } from "@medusajs/framework/utils"6
7export default async function myScript({ container }: ExecArgs) {8  const productModuleService: IProductModuleService = container.resolve(9    Modules.PRODUCT10  )11
12  const [, count] = await productModuleService13    .listAndCountProducts()14
15  console.log(`You have ${count} product(s)`)16}

The function receives as a parameter an object with a container property, which is an instance of the Medusa Container. Use it to resolve resources in your Medusa application.


How to Run a Custom CLI Script?#

To run a custom CLI script, run the Medusa CLI's exec command:

Terminal
npx medusa exec ./src/scripts/my-script.ts

Custom CLI Script Arguments#

Your script can accept arguments from the command line. Arguments are passed to the function's object parameter in the args property.

For example, create the following CLI script that logs the command line arguments:

Code
1import { ExecArgs } from "@medusajs/framework/types"2
3export default async function myScript({ args }: ExecArgs) {4  console.log(`The arguments you passed: ${args}`)5}

Then, run the script with the exec command and pass arguments after the script's path.

Terminal
npx medusa exec ./src/scripts/my-script.ts arg1 arg2

Run Custom Script on Application Startup#

In some cases, you may need to perform an action when the Medusa application starts.

If the action is related to a module, you should use a loader. Otherwise, you can create a custom CLI script and run it before starting the Medusa application.

To run a custom script on application startup, modify the dev and start commands in package.json to execute your script first.

For example:

package.json
1{2  "scripts": {3    "startup": "medusa exec ./src/scripts/startup.ts",4    "dev": "npm run startup && medusa develop",5    "start": "npm run startup && medusa start"6  }7}

The startup script will run every time you run the Medusa application.

Was this chapter helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
close