Kaynağa Gözat

rename scripts directory

Shahed Nasser 1 yıl önce
ebeveyn
işleme
68fc53eff2
3 değiştirilmiş dosya ile 69 ekleme ve 1 silme
  1. 1 1
      package.json
  2. 68 0
      src/scripts/README.md
  3. 0 0
      src/scripts/seed.ts

+ 1 - 1
package.json

@@ -14,7 +14,7 @@
   ],
   "scripts": {
     "build": "medusa build",
-    "seed": "medusa exec ./dist/helpers/seed.js",
+    "seed": "medusa exec ./dist/scripts/seed.js",
     "start": "medusa start",
     "dev": "node ./develop.mjs"
   },

+ 68 - 0
src/scripts/README.md

@@ -0,0 +1,68 @@
+# 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 as a CLI tool.
+
+## 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/types"
+import { ModuleRegistrationName } from "@medusajs/modules-sdk"
+
+export default async function myScript ({
+  container
+}: ExecArgs) {
+  const productModuleService: IProductModuleService = 
+    container.resolve(ModuleRegistrationName.PRODUCT)
+
+  const [, count] = await productModuleService.listAndCount()
+
+  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, `build` your code then run the `exec` command:
+
+```bash
+npm run build
+npx medusa exec ./dist/scripts/my-script.js
+```
+
+Notice that you pass the path to the file in the `dist` directory.
+
+---
+
+## 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/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
+npm run build
+npx medusa exec ./dist/scripts/my-script.js arg1 arg2
+```

+ 0 - 0
src/helpers/seed.ts → src/scripts/seed.ts