Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa,medusa-cli): add an exec command #7376

Merged
merged 5 commits into from
May 21, 2024
Merged

Conversation

shahednasser
Copy link
Member

@shahednasser shahednasser commented May 20, 2024

Edit: After discussing it, the command is now renamed to exec.

Added a script command to Medusa's CLI tool that accepts a file parameter as a path to run its exported function. It passes the function the Medusa Container as a parameter to the function.

An example of a script:

// src/helpers/hello-world.ts
import { ExecArgs } from "@medusajs/types"

export default function seed({
  container,
  args
}: ExecArgs) {
  console.log("Hello, world!")
}

You can then run it with the following command:

yarn build
npx medusa script ./dist/helpers/hello-world.js

@shahednasser shahednasser requested a review from a team as a code owner May 20, 2024 16:11
Copy link

vercel bot commented May 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medusa-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 21, 2024 7:18am
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) May 21, 2024 7:18am
docs-ui ⬜️ Ignored (Inspect) Visit Preview May 21, 2024 7:18am
medusa-docs ⬜️ Ignored (Inspect) Visit Preview May 21, 2024 7:18am

Copy link

changeset-bot bot commented May 20, 2024

⚠️ No Changeset found

Latest commit: 5f7a8d8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -22,7 +22,6 @@ import { SubscriberLoader } from "./helpers/subscribers"
type Options = {
directory: string
expressApp: Express
isTest: boolean
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: removed this as it's a required option for the loaders function but didn't find it used anywhere

@shahednasser shahednasser marked this pull request as draft May 20, 2024 16:13
@shahednasser
Copy link
Member Author

/snapshot-this

Copy link
Contributor

🚀 A snapshot release has been made for this PR

Test the snapshots by updating your package.json with the newly published versions:

yarn add @medusajs/admin-sdk@0.0.2-snapshot-20240520162050
yarn add @medusajs/dashboard@0.0.2-snapshot-20240520162050
yarn add babel-preset-medusa-package@1.1.20-snapshot-20240520162050
yarn add create-medusa-app@1.2.9-snapshot-20240520162050
yarn add @medusajs/medusa-cli@1.3.23-snapshot-20240520162050
yarn add medusa-dev-cli@0.0.33-snapshot-20240520162050
yarn add @medusajs/medusa-oas-cli@1.0.0-snapshot-20240520162050
yarn add @medusajs/openapi-typescript-codegen@0.2.2-snapshot-20240520162050
yarn add @medusajs/core-flows@0.0.10-snapshot-20240520162050
yarn add @medusajs/js-sdk@0.0.2-snapshot-20240520162050
yarn add medusa-test-utils@1.1.45-snapshot-20240520162050
yarn add @medusajs/modules-sdk@1.13.0-snapshot-20240520162050
yarn add @medusajs/orchestration@0.5.8-snapshot-20240520162050
yarn add @medusajs/types@1.12.0-snapshot-20240520162050
yarn add @medusajs/utils@1.12.0-snapshot-20240520162050
yarn add @medusajs/workflows-sdk@0.1.7-snapshot-20240520162050
yarn add @medusajs/icons@1.2.2-snapshot-20240520162050
yarn add @medusajs/ui@3.0.1-snapshot-20240520162050
yarn add @medusajs/ui-preset@1.1.4-snapshot-20240520162050
yarn add @medusajs/medusa@1.20.6-snapshot-20240520162050
yarn add medusa-core-utils@1.2.3-snapshot-20240520162050
yarn add medusa-telemetry@0.0.18-snapshot-20240520162050
yarn add @medusajs/api-key@0.1.3-snapshot-20240520162050
yarn add @medusajs/auth@0.0.4-snapshot-20240520162050
yarn add @medusajs/cache-inmemory@1.8.11-snapshot-20240520162050
yarn add @medusajs/cache-redis@1.9.2-snapshot-20240520162050
yarn add @medusajs/cart@0.0.4-snapshot-20240520162050
yarn add @medusajs/currency@0.1.3-snapshot-20240520162050
yarn add @medusajs/customer@0.0.4-snapshot-20240520162050
yarn add @medusajs/event-bus-local@1.9.9-snapshot-20240520162050
yarn add @medusajs/event-bus-redis@1.8.14-snapshot-20240520162050
yarn add @medusajs/file@0.0.2-snapshot-20240520162050
yarn add @medusajs/fulfillment@0.1.3-snapshot-20240520162050
yarn add @medusajs/inventory-next@0.0.4-snapshot-20240520162050
yarn add @medusajs/link-modules@0.2.12-snapshot-20240520162050
yarn add @medusajs/notification@0.1.3-snapshot-20240520162050
yarn add @medusajs/order@0.1.3-snapshot-20240520162050
yarn add @medusajs/payment@0.0.4-snapshot-20240520162050
yarn add @medusajs/pricing@0.1.13-snapshot-20240520162050
yarn add @medusajs/product@0.3.13-snapshot-20240520162050
yarn add @medusajs/promotion@0.0.5-snapshot-20240520162050
yarn add @medusajs/file-local-next@0.0.3-snapshot-20240520162050
yarn add @medusajs/file-s3@0.0.3-snapshot-20240520162050
yarn add @medusajs/fulfillment-manual@0.0.3-snapshot-20240520162050
yarn add @medusajs/notification-local@0.0.2-snapshot-20240520162050
yarn add @medusajs/notification-sendgrid@0.0.2-snapshot-20240520162050
yarn add @medusajs/payment-stripe@0.0.3-snapshot-20240520162050
yarn add @medusajs/region@0.1.2-snapshot-20240520162050
yarn add @medusajs/sales-channel@0.1.2-snapshot-20240520162050
yarn add @medusajs/stock-location-next@0.0.4-snapshot-20240520162050
yarn add @medusajs/store@0.1.2-snapshot-20240520162050
yarn add @medusajs/tax@0.1.2-snapshot-20240520162050
yarn add @medusajs/user@0.0.4-snapshot-20240520162050
yarn add @medusajs/workflow-engine-inmemory@0.0.5-snapshot-20240520162050
yarn add @medusajs/workflow-engine-redis@0.0.5-snapshot-20240520162050

Latest commit: 025536e

packages/cli/medusa-cli/src/create-cli.ts Outdated Show resolved Hide resolved
packages/medusa/src/commands/script.ts Outdated Show resolved Hide resolved
@shahednasser
Copy link
Member Author

shahednasser commented May 21, 2024

I addressed the PR feedback and renamed the command to exec (I think it makes a lot of sense 😄)

@olivermrbl I also added a new feature to it, which is the ability to pass arguments from the command line to the script. I imagine that a lot of usage for this command could be to create a custom CLI command, which, in many cases, would perform something with CLI arguments. Now, any arguments passed after the file like this:

npx medusa bash ./dist/helpers/hello-world.js test

The script receives it as a second parameter:

// src/helpers/hello-world.ts
import { MedusaContainer } from "@medusajs/medusa"

export default function seed(container: MedusaContainer, args:  string[]) {
  console.log("Hello, world!")
}

Let me know what you think

@shahednasser shahednasser changed the title feat(medusa,medusa-cli): add a script command feat(medusa,medusa-cli): add an exec command May 21, 2024
Copy link
Contributor

@olivermrbl olivermrbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this is great @shahednasser!

QQ: Should we use the Logger instead of the console?

Copy link
Member

@adrien2p adrien2p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 💪 should we add a type somewhere for the input args for the user to use in there function definition? just so that they know how they will receive the args from the command line

Maybe also we should use the logger if possible

@shahednasser
Copy link
Member Author

QQ: Should we use the Logger instead of the console?

Makes sense, I'll make the change 👍🏻

should we add a type somewhere for the input args for the user to use in there function definition?

@adrien2p Yea we can do that, maybe change the parameter they receive to be an object with a container and args properties instead of two parameters

@adrien2p
Copy link
Member

QQ: Should we use the Logger instead of the console?

Makes sense, I'll make the change 👍🏻

should we add a type somewhere for the input args for the user to use in there function definition?

@adrien2p Yea we can do that, maybe change the parameter they receive to be an object with a container and args properties instead of two parameters

Yes that's perfect. Also, I can see in the description that MedusaContainer is imported from medusa but it comes from the types package 👍

@shahednasser
Copy link
Member Author

@adrien2p true, but it now changes to ExecArgs from the types package anyway. Updated the description to reflect that 👍🏻

@olivermrbl
Copy link
Contributor

Merging now, if cool with you all?

Comment on lines +25 to +27

const scriptFile = (await import(path.resolve(directory, file))).default

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought(non-blocking): maybe some light validation would be good so we can shoot off a nice error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add it in a separate PR later to not block merging for now 👍🏻

@shahednasser
Copy link
Member Author

Fine by me 👍🏻

@olivermrbl olivermrbl merged commit 73c917f into develop May 21, 2024
17 checks passed
@olivermrbl olivermrbl deleted the feat/medusa-script branch May 21, 2024 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants