1 import boxen from "boxen"
2 import chalk from "chalk"
3 import { emojify } from "node-emoji"
4 import { Ora } from "ora"
5 import ProcessManager from "./process-manager.js"
6 import terminalLink from "terminal-link"
7
8 export type FactBoxOptions = {
9 interval: NodeJS.Timeout | null
10 spinner: Ora
11 processManager: ProcessManager
12 message?: string
13 title?: string
14 verbose?: boolean
15 }
16
17 const facts = [
18 "Specify a product's availability in one or more sales channels.",
19 "Payment providers can be configured per region.",
20 "Tax-inclusive pricing allows you to set prices for products and shipping options while delegating tax calculations to Medusa.",
21 "Medusa provides multi-currency and region support, with full control over prices for each currency and region.",
22 "Organize customers by customer groups and set special prices for them.",
23 "Specify the inventory of products per location and sales channel.",
24 "Publishable-API Keys allow you to send scoped requests to the server's store API routes.",
25 "API Routes expose business logic to clients, such as storefronts and admin customizations.",
26 "Subscribers are asynchronous functions that are executed when an event is emitted.",
27 "Data models represent tables in the database. They are created using Medusa's Data Modeling Language (DML).",
28 "Medusa's store API routes are prefixed by /store. The admin API routes are prefixed by /admin.",
29 "The JS SDK allows you to send requests to the Medusa server from your storefront or admin customizations.",
30 "Modules are reusable packages of functionalities related to a single commerce domain or integration.",
31 "Modules have a main service that provides data-management and integration functionalities.",
32 "Modules allow you to replace an entire functionality with your custom logic.",
33 "Infrastructure Modules are interchangeable modules that implement features and integrations related to the Medusa server's infrastructure.",
34 "Commerce Modules are built-in modules that provide core commerce logic specific to domains like Product, Cart and Order.",
35 "Workflows are a series of queries and actions, called steps, that complete a task.",
36 "A workflow's steps can be retried or rolled back in case of an error.",
37 `Medusa provides ${terminalLink(
38 "Claude Code plugins",
39 "https://github.com/medusajs/medusa-claude-plugins"
40 )} to facilitate your development.`,
41 "Medusa provides an MCP server at https://docs.medusajs.com/mcp to support your learning and development experience with AI agents",
42 `Medusa is optimized to build custom commerce software with AI agents through its MCP server and ${terminalLink(
43 "Claude Code plugins",
44 "https://github.com/medusajs/medusa-claude-plugins"
45 )}.`,
46 ]
47
48 export const getFact = () => {
49 const randIndex = Math.floor(Math.random() * facts.length)
50
51 return facts[randIndex]
52 }
53
54 export const showFact = ({
55 spinner,
56 title,
57 verbose,
58 }: Pick<FactBoxOptions, "spinner" | "verbose"> & {
59 title: string
60 }) => {
61 const fact = getFact()
62 if (verbose) {
63 spinner.stopAndPersist({
64 symbol: chalk.cyan("⠋"),
65 text: title,
66 })
67 } else {
68 spinner.text = `${title}\n${boxen(`${fact}`, {
69 title: chalk.cyan(`${emojify(":bulb:")} Medusa Tips`),
70 titleAlignment: "center",
71 textAlignment: "center",
72 padding: 1,
73 margin: 1,
74 })}`
75 }
76 }
77
78 export const createFactBox = ({
79 spinner,
80 title,
81 processManager,
82 verbose,
83 }: Pick<FactBoxOptions, "spinner" | "processManager" | "verbose"> & {
84 title: string
85 }): NodeJS.Timeout => {
86 showFact({ spinner, title, verbose })
87 const interval = setInterval(() => {
88 showFact({ spinner, title, verbose })
89 }, 10000)
90
91 processManager.addInterval(interval)
92
93 return interval
94 }
95
96 export const resetFactBox = ({
97 interval,
98 spinner,
99 successMessage,
100 processManager,
101 newTitle,
102 verbose,
103 }: Pick<
104 FactBoxOptions,
105 "interval" | "spinner" | "processManager" | "verbose"
106 > & {
107 successMessage: string
108 newTitle?: string
109 }): NodeJS.Timeout | null => {
110 if (interval) {
111 clearInterval(interval)
112 }
113
114 spinner.succeed(chalk.green(successMessage)).start()
115 let newInterval = null
116 if (newTitle) {
117 newInterval = createFactBox({
118 spinner,
119 title: newTitle,
120 processManager,
121 verbose,
122 })
123 }
124
125 return newInterval
126 }
127
128 export function displayFactBox({
129 interval,
130 spinner,
131 processManager,
132 title = "",
133 message = "",
134 verbose = false,
135 }: FactBoxOptions): NodeJS.Timeout | null {
136 if (!message) {
137 return createFactBox({ spinner, title, processManager, verbose })
138 }
139
140 return resetFactBox({
141 interval,
142 spinner,
143 successMessage: message,
144 processManager,
145 newTitle: title,
146 verbose,
147 })
148 }