docs: add routing page (#9550)
- Add a new homepage to `book` project for the routing page - Move all main doc pages to be under `/v2/learn` (and added redirects + fixed links across docs) - Other: add admin components to resources dropdown + fixes to search on mobile. Closes DX-955 Preview: https://docs-v2-git-docs-router-page-medusajs.vercel.app/v2
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Custom CLI Scripts`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this chapter, you'll learn how create and execute custom scripts from Medusa's CLI tool.
|
||||
|
||||
## What is a Custom CLI Script?
|
||||
|
||||
A custom CLI script is a function to execute through Medusa's CLI tool. This is useful when creating custom Medusa tooling to run through the CLI.
|
||||
|
||||
---
|
||||
|
||||
## 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 default export a function.
|
||||
|
||||
For example, create the file `src/scripts/my-script.ts` with the following content:
|
||||
|
||||
```ts title="src/scripts/my-script.ts"
|
||||
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.
|
||||
|
||||
---
|
||||
|
||||
## How to Run Custom CLI Script?
|
||||
|
||||
To run the custom CLI script, run the Medusa CLI's `exec` command:
|
||||
|
||||
```bash
|
||||
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:
|
||||
|
||||
```ts
|
||||
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:
|
||||
|
||||
```bash
|
||||
npx medusa exec ./src/scripts/my-script.ts arg1 arg2
|
||||
```
|
||||
Reference in New Issue
Block a user