|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Custom CLI Scripts`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to create and execute custom scripts using Medusa's CLI tool. |
|
8
|
|
|
9
|
## What is a Custom CLI Script? |
|
10
|
|
|
11
|
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](../medusa-container/page.mdx) and can be executed using Medusa's CLI. |
|
12
|
|
|
13
|
For example, you can create a custom CLI script that: |
|
14
|
|
|
15
|
- [Seeds data into the database](./seed-data/page.mdx). |
|
16
|
- Runs a script before starting the Medusa application. |
|
17
|
|
|
18
|
--- |
|
19
|
|
|
20
|
## How to Create a Custom CLI Script? |
|
21
|
|
|
22
|
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. |
|
23
|
|
|
24
|
For example, create the file `src/scripts/my-script.ts` with the following content: |
|
25
|
|
|
26
|
```ts title="src/scripts/my-script.ts" |
|
27
|
import { |
|
28
|
ExecArgs, |
|
29
|
} from "@medusajs/framework/types" |
|
30
|
import { Modules } from "@medusajs/framework/utils" |
|
31
|
|
|
32
|
export default async function myScript({ container }: ExecArgs) { |
|
33
|
const productModuleService = container.resolve( |
|
34
|
Modules.PRODUCT |
|
35
|
) |
|
36
|
|
|
37
|
const [, count] = await productModuleService |
|
38
|
.listAndCountProducts() |
|
39
|
|
|
40
|
console.log(`You have ${count} product(s)`) |
|
41
|
} |
|
42
|
``` |
|
43
|
|
|
44
|
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. |
|
45
|
|
|
46
|
--- |
|
47
|
|
|
48
|
## How to Run a Custom CLI Script? |
|
49
|
|
|
50
|
To run a custom CLI script, run the Medusa CLI's `exec` command: |
|
51
|
|
|
52
|
```bash npx2yarn |
|
53
|
npx medusa exec ./src/scripts/my-script.ts |
|
54
|
``` |
|
55
|
|
|
56
|
--- |
|
57
|
|
|
58
|
## Custom CLI Script Arguments |
|
59
|
|
|
60
|
Your script can accept arguments from the command line. Arguments are passed to the function's object parameter in the `args` property. |
|
61
|
|
|
62
|
For example, create the following CLI script that logs the command line arguments: |
|
63
|
|
|
64
|
```ts |
|
65
|
import { ExecArgs } from "@medusajs/framework/types" |
|
66
|
|
|
67
|
export default async function myScript({ args }: ExecArgs) { |
|
68
|
console.log(`The arguments you passed: ${args}`) |
|
69
|
} |
|
70
|
``` |
|
71
|
|
|
72
|
Then, run the script with the `exec` command and pass arguments after the script's path. |
|
73
|
|
|
74
|
```bash npx2yarn |
|
75
|
npx medusa exec ./src/scripts/my-script.ts arg1 arg2 |
|
76
|
``` |
|
77
|
|
|
78
|
--- |
|
79
|
|
|
80
|
## Run Custom Script on Application Startup |
|
81
|
|
|
82
|
In some cases, you may need to perform an action when the Medusa application starts. |
|
83
|
|
|
84
|
If the action is related to a module, you should use a [loader](../modules/loaders/page.mdx). Otherwise, you can create a custom CLI script and run it before starting the Medusa application. |
|
85
|
|
|
86
|
To run a custom script on application startup, modify the `dev` and `start` commands in `package.json` to execute your script first. |
|
87
|
|
|
88
|
For example: |
|
89
|
|
|
90
|
```json title="package.json" |
|
91
|
{ |
|
92
|
"scripts": { |
|
93
|
"startup": "medusa exec ./src/scripts/startup.ts", |
|
94
|
"dev": "npm run startup && medusa develop", |
|
95
|
"start": "npm run startup && medusa start" |
|
96
|
} |
|
97
|
} |
|
98
|
``` |
|
99
|
|
|
100
|
The `startup` script will run every time you run the Medusa application. |