1 import { track } from "@medusajs/telemetry"
2 import boxen from "boxen"
3 import chalk from "chalk"
4 import { emojify } from "node-emoji"
5 import { EOL } from "os"
6 import { runCloneRepo } from "../clone-repo.js"
7 import { isAbortError } from "../create-abort-controller.js"
8 import { displayFactBox } from "../facts.js"
9 import logMessage from "../log-message.js"
10 import prepareProject from "../prepare-project.js"
11 import {
12 BaseProjectCreator,
13 ProjectCreator,
14 ProjectOptions,
15 } from "./creator.js"
16 import terminalLink from "terminal-link"
17 import { promptClaudeCodePlugin } from "../claude-code-plugin.js"
18
19 // Plugin Project Creator
20 export class PluginProjectCreator
21 extends BaseProjectCreator
22 implements ProjectCreator
23 {
24 constructor(projectName: string, options: ProjectOptions, args: string[]) {
25 super(projectName, options, args)
26 this.setupProcessManager()
27 }
28
29 async create(): Promise<void> {
30 track("CREATE_CLI_CMP")
31 await promptClaudeCodePlugin()
32
33 logMessage({
34 message: `${emojify(
35 ":rocket:"
36 )} Starting plugin setup, this may take a few minutes.`,
37 })
38
39 this.spinner.start()
40 this.factBoxOptions.interval = displayFactBox({
41 ...this.factBoxOptions,
42 title: "Setting up plugin...",
43 })
44
45 try {
46 await this.cloneAndPreparePlugin()
47 this.spinner.succeed(chalk.green("Plugin Prepared"))
48 this.showSuccessMessage()
49 } catch (e: any) {
50 this.handleError(e)
51 }
52 }
53
54 private async cloneAndPreparePlugin(): Promise<void> {
55 await runCloneRepo({
56 projectName: this.projectPath,
57 repoUrl: this.options.repoUrl ?? "",
58 abortController: this.abortController,
59 spinner: this.spinner,
60 verbose: this.options.verbose,
61 isPlugin: true,
62 })
63
64 this.factBoxOptions.interval = displayFactBox({
65 ...this.factBoxOptions,
66 message: "Created plugin directory",
67 })
68
69 await prepareProject({
70 isPlugin: true,
71 directory: this.projectPath,
72 projectName: this.projectName,
73 spinner: this.spinner,
74 processManager: this.processManager,
75 abortController: this.abortController,
76 verbose: this.options.verbose,
77 packageManager: this.packageManager,
78 })
79 }
80
81 private handleError(e: any): void {
82 if (isAbortError(e)) {
83 process.exit()
84 }
85
86 this.spinner.stop()
87 logMessage({
88 message: `An error occurred while preparing plugin: ${e}`,
89 type: "error",
90 })
91 }
92
93 protected showSuccessMessage(): void {
94 logMessage({
95 message: boxen(
96 chalk.green(
97 `Change to the \`${
98 this.projectName
99 }\` directory to explore your Medusa plugin.${EOL}${EOL}Check out the ${terminalLink(
100 "Medusa plugin documentation",
101 "https://docs.medusajs.com/learn/fundamentals/plugins"
102 )} to start your development.${EOL}${EOL}Star us on ${terminalLink(
103 "GitHub",
104 "https://github.com/medusajs/medusa/stargazers"
105 )} if you like what we're building.`
106 ),
107 {
108 titleAlignment: "center",
109 textAlignment: "center",
110 padding: 1,
111 margin: 1,
112 float: "center",
113 }
114 ),
115 })
116 }
117
118 protected setupProcessManager(): void {
119 this.processManager.onTerminated(async () => {
120 this.spinner.stop()
121
122 if (!this.printedMessage && this.isProjectCreated) {
123 this.printedMessage = true
124 this.showSuccessMessage()
125 }
126 return
127 })
128 }
129 }