create-medusa-app 2.22.5 → 2.22.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
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 { BaseProjectCreator, } from "./creator.js";
12
+ import terminalLink from "terminal-link";
13
+ import { promptClaudeCodePlugin } from "../claude-code-plugin.js";
14
+ // Plugin Project Creator
15
+ export class PluginProjectCreator extends BaseProjectCreator {
16
+ constructor(projectName, options, args) {
17
+ super(projectName, options, args);
18
+ this.setupProcessManager();
19
+ }
20
+ async create() {
21
+ track("CREATE_CLI_CMP");
22
+ await promptClaudeCodePlugin();
23
+ logMessage({
24
+ message: `${emojify(":rocket:")} Starting plugin setup, this may take a few minutes.`,
25
+ });
26
+ this.spinner.start();
27
+ this.factBoxOptions.interval = displayFactBox({
28
+ ...this.factBoxOptions,
29
+ title: "Setting up plugin...",
30
+ });
31
+ try {
32
+ await this.cloneAndPreparePlugin();
33
+ this.spinner.succeed(chalk.green("Plugin Prepared"));
34
+ this.showSuccessMessage();
35
+ }
36
+ catch (e) {
37
+ this.handleError(e);
38
+ }
39
+ }
40
+ async cloneAndPreparePlugin() {
41
+ await runCloneRepo({
42
+ projectName: this.projectPath,
43
+ repoUrl: this.options.repoUrl ?? "",
44
+ abortController: this.abortController,
45
+ spinner: this.spinner,
46
+ verbose: this.options.verbose,
47
+ isPlugin: true,
48
+ });
49
+ this.factBoxOptions.interval = displayFactBox({
50
+ ...this.factBoxOptions,
51
+ message: "Created plugin directory",
52
+ });
53
+ await prepareProject({
54
+ isPlugin: true,
55
+ directory: this.projectPath,
56
+ projectName: this.projectName,
57
+ spinner: this.spinner,
58
+ processManager: this.processManager,
59
+ abortController: this.abortController,
60
+ verbose: this.options.verbose,
61
+ packageManager: this.packageManager,
62
+ version: this.options.version,
63
+ });
64
+ }
65
+ handleError(e) {
66
+ if (isAbortError(e)) {
67
+ process.exit();
68
+ }
69
+ this.spinner.stop();
70
+ logMessage({
71
+ message: `An error occurred while preparing plugin: ${e}`,
72
+ type: "error",
73
+ });
74
+ }
75
+ showSuccessMessage() {
76
+ logMessage({
77
+ message: boxen(chalk.green(`Change to the \`${this.projectName}\` directory to explore your Medusa plugin.${EOL}${EOL}Check out the ${terminalLink("Medusa plugin documentation", "https://docs.medusajs.com/learn/fundamentals/plugins")} to start your development.${EOL}${EOL}Star us on ${terminalLink("GitHub", "https://github.com/medusajs/medusa/stargazers")} if you like what we're building.`), {
78
+ titleAlignment: "center",
79
+ textAlignment: "center",
80
+ padding: 1,
81
+ margin: 1,
82
+ float: "center",
83
+ }),
84
+ });
85
+ }
86
+ setupProcessManager() {
87
+ this.processManager.onTerminated(async () => {
88
+ this.spinner.stop();
89
+ if (!this.printedMessage && this.isProjectCreated) {
90
+ this.printedMessage = true;
91
+ this.showSuccessMessage();
92
+ }
93
+ return;
94
+ });
95
+ }
96
+ }
@@ -0,0 +1,215 @@
1
+ import { track } from "@medusajs/telemetry";
2
+ import boxen from "boxen";
3
+ import chalk from "chalk";
4
+ import fs from "fs";
5
+ import { emojify } from "node-emoji";
6
+ import open from "open";
7
+ import { EOL } from "os";
8
+ import slugifyType from "slugify";
9
+ import waitOn from "wait-on";
10
+ import { promptClaudeCodePlugin } from "../claude-code-plugin.js";
11
+ import { runCloneRepo } from "../clone-repo.js";
12
+ import { isAbortError } from "../create-abort-controller.js";
13
+ import { getDbClientAndCredentials, runCreateDb } from "../create-db.js";
14
+ import { displayFactBox } from "../facts.js";
15
+ import logMessage from "../log-message.js";
16
+ import { askForNextjsStarter, installNextjsStarter, startNextjsStarter, } from "../nextjs-utils.js";
17
+ import prepareProject from "../prepare-project.js";
18
+ import startMedusa from "../start-medusa.js";
19
+ import { getBackendDirectory, getStorefrontDirectory, } from "../project-paths.js";
20
+ import { BaseProjectCreator, } from "./creator.js";
21
+ import terminalLink from "terminal-link";
22
+ const slugify = slugifyType.default;
23
+ // Medusa Project Creator
24
+ export class MedusaProjectCreator extends BaseProjectCreator {
25
+ client = null;
26
+ dbConnectionString = "";
27
+ isDbInitialized = false;
28
+ nextjsDirectory = "";
29
+ inviteToken;
30
+ constructor(projectName, options, args) {
31
+ super(projectName, options, args);
32
+ this.setupProcessManager();
33
+ }
34
+ async create({ verbose }) {
35
+ track("CREATE_CLI_CMA");
36
+ try {
37
+ await this.initializeProject();
38
+ await this.setupProject();
39
+ await this.startServices();
40
+ }
41
+ catch (e) {
42
+ this.handleError(e, verbose);
43
+ }
44
+ }
45
+ async initializeProject() {
46
+ const installNextjs = this.options.withNextjsStarter || (await askForNextjsStarter());
47
+ await promptClaudeCodePlugin();
48
+ if (!this.options.skipDb) {
49
+ await this.setupDatabase();
50
+ }
51
+ logMessage({
52
+ message: `${emojify(":rocket:")} Starting project setup, this may take a few minutes.`,
53
+ });
54
+ this.spinner.start();
55
+ this.factBoxOptions.interval = displayFactBox({
56
+ ...this.factBoxOptions,
57
+ title: "Setting up project...",
58
+ });
59
+ await runCloneRepo({
60
+ projectName: this.projectPath,
61
+ repoUrl: this.options.repoUrl ?? "",
62
+ abortController: this.abortController,
63
+ spinner: this.spinner,
64
+ verbose: this.options.verbose,
65
+ });
66
+ this.factBoxOptions.interval = displayFactBox({
67
+ ...this.factBoxOptions,
68
+ message: "Created project directory",
69
+ });
70
+ const storefrontDirectory = getStorefrontDirectory(this.projectPath);
71
+ if (!installNextjs) {
72
+ if (fs.existsSync(storefrontDirectory)) {
73
+ fs.rmSync(storefrontDirectory, { recursive: true, force: true });
74
+ }
75
+ }
76
+ else {
77
+ this.nextjsDirectory = storefrontDirectory;
78
+ await installNextjsStarter({
79
+ storefrontDirectory,
80
+ abortController: this.abortController,
81
+ factBoxOptions: this.factBoxOptions,
82
+ verbose: this.options.verbose,
83
+ packageManager: this.packageManager,
84
+ version: this.options.version,
85
+ });
86
+ }
87
+ }
88
+ async setupDatabase() {
89
+ let dbName = `medusa-${slugify(this.projectName)}`;
90
+ const { client, dbConnectionString, ...rest } = await getDbClientAndCredentials({
91
+ dbName,
92
+ dbUrl: this.options.dbUrl,
93
+ verbose: this.options.verbose,
94
+ });
95
+ this.client = client;
96
+ this.dbConnectionString = dbConnectionString;
97
+ this.isDbInitialized = true;
98
+ dbName = rest.dbName || dbName;
99
+ if (!this.options.dbUrl) {
100
+ this.factBoxOptions.interval = displayFactBox({
101
+ ...this.factBoxOptions,
102
+ message: "Creating database...",
103
+ });
104
+ this.client = await runCreateDb({
105
+ client: this.client,
106
+ dbName,
107
+ spinner: this.spinner,
108
+ });
109
+ this.factBoxOptions.interval = displayFactBox({
110
+ ...this.factBoxOptions,
111
+ message: `Database ${dbName} created`,
112
+ });
113
+ }
114
+ }
115
+ async setupProject() {
116
+ try {
117
+ this.inviteToken = await prepareProject({
118
+ isPlugin: false,
119
+ projectName: this.projectName,
120
+ directory: this.projectPath,
121
+ dbConnectionString: this.dbConnectionString,
122
+ seed: this.options.seed,
123
+ spinner: this.spinner,
124
+ processManager: this.processManager,
125
+ abortController: this.abortController,
126
+ skipDb: this.options.skipDb,
127
+ migrations: this.options.migrations,
128
+ onboardingType: this.nextjsDirectory ? "nextjs" : "default",
129
+ nextjsDirectory: this.nextjsDirectory,
130
+ client: this.client,
131
+ verbose: this.options.verbose,
132
+ packageManager: this.packageManager,
133
+ version: this.options.version,
134
+ });
135
+ }
136
+ finally {
137
+ await this.client?.end();
138
+ }
139
+ this.spinner.succeed(chalk.green("Project Prepared"));
140
+ }
141
+ async startServices() {
142
+ if (this.options.skipDb || !this.options.browser) {
143
+ this.showSuccessMessage();
144
+ process.exit();
145
+ }
146
+ logMessage({
147
+ message: "Starting Medusa...",
148
+ });
149
+ startMedusa({
150
+ directory: getBackendDirectory(this.projectPath),
151
+ abortController: this.abortController,
152
+ packageManager: this.packageManager,
153
+ });
154
+ if (this.nextjsDirectory) {
155
+ startNextjsStarter({
156
+ directory: this.nextjsDirectory,
157
+ abortController: this.abortController,
158
+ verbose: this.options.verbose,
159
+ packageManager: this.packageManager,
160
+ });
161
+ }
162
+ this.isProjectCreated = true;
163
+ await this.openBrowser();
164
+ }
165
+ async openBrowser() {
166
+ await waitOn({
167
+ resources: ["http://localhost:9000/health"],
168
+ }).then(async () => open(this.inviteToken
169
+ ? `http://localhost:9000/app/invite?token=${this.inviteToken}&first_run=true`
170
+ : "http://localhost:9000/app"));
171
+ }
172
+ handleError(e, verbose) {
173
+ if (isAbortError(e)) {
174
+ process.exit();
175
+ }
176
+ const showStack = verbose || e.message?.includes("npm") || e.message?.includes("yarn");
177
+ this.spinner.stop();
178
+ logMessage({
179
+ message: `An error occurred: ${e}`,
180
+ type: "error",
181
+ stack: showStack ? e.stack?.replace(e.toString(), "") : "",
182
+ });
183
+ }
184
+ showSuccessMessage() {
185
+ const commandStr = this.packageManager.getCommandStr(`dev`);
186
+ logMessage({
187
+ message: boxen(chalk.green(`Change to the \`${this.projectName}\` directory to explore your Medusa project.${EOL}${EOL}Start your Medusa application again with the following command:${EOL}${EOL}${commandStr}${EOL}${EOL}${this.inviteToken
188
+ ? `After you start the Medusa application, you can create an admin user with the URL http://localhost:9000/app/invite?token=${this.inviteToken}&first_run=true${EOL}${EOL}`
189
+ : ""}${this.nextjsDirectory?.length
190
+ ? `The storefront is available in the \`${this.nextjsDirectory}\` directory. Change to that directory and start it with the following command:${EOL}${EOL}${commandStr}${EOL}${EOL}`
191
+ : ""}Check out the Medusa ${terminalLink("documentation", "https://docs.medusajs.com/")} to start your development:${EOL}${EOL}Star us on ${terminalLink("GitHub", "https://github.com/medusajs/medusa/stargazers")} if you like what we're building.`), {
192
+ titleAlignment: "center",
193
+ textAlignment: "center",
194
+ padding: 1,
195
+ margin: 1,
196
+ float: "center",
197
+ }),
198
+ });
199
+ }
200
+ setupProcessManager() {
201
+ this.processManager.onTerminated(async () => {
202
+ this.spinner.stop();
203
+ // prevent an error from occurring if
204
+ // client hasn't been declared yet
205
+ if (this.isDbInitialized && this.client) {
206
+ await this.client.end();
207
+ }
208
+ if (!this.printedMessage && this.isProjectCreated) {
209
+ this.printedMessage = true;
210
+ this.showSuccessMessage();
211
+ }
212
+ return;
213
+ });
214
+ }
215
+ }
@@ -0,0 +1,83 @@
1
+ import fs from "fs";
2
+ import inquirer from "inquirer";
3
+ import path from "path";
4
+ import slugifyType from "slugify";
5
+ import logMessage from "../log-message.js";
6
+ import { getNodeVersion, isNodeVersionSupported, MIN_SUPPORTED_NODE_VERSION, } from "../node-version.js";
7
+ import { PluginProjectCreator } from "./medusa-plugin-creator.js";
8
+ import { MedusaProjectCreator } from "./medusa-project-creator.js";
9
+ import terminalLink from "terminal-link";
10
+ const slugify = slugifyType.default;
11
+ export class ProjectCreatorFactory {
12
+ static async create(args, options) {
13
+ ProjectCreatorFactory.validateNodeVersion();
14
+ const projectName = await ProjectCreatorFactory.getProjectName(args, options.directoryPath, options.plugin);
15
+ return options.plugin
16
+ ? new PluginProjectCreator(projectName, options, args)
17
+ : new MedusaProjectCreator(projectName, options, args);
18
+ }
19
+ static validateNodeVersion() {
20
+ const nodeVersion = getNodeVersion();
21
+ if (!isNodeVersionSupported(nodeVersion)) {
22
+ logMessage({
23
+ message: `Medusa requires at least v${MIN_SUPPORTED_NODE_VERSION} of Node.js. You're using v${nodeVersion}. Please ${terminalLink("install Node.js", "https://nodejs.org/en/download")} v${MIN_SUPPORTED_NODE_VERSION} or later and try again.`,
24
+ type: "error",
25
+ });
26
+ }
27
+ }
28
+ static async getProjectName(args, directoryPath, isPlugin) {
29
+ let askProjectName = args.length === 0;
30
+ if (args.length > 0) {
31
+ const projectPath = path.join(directoryPath || "", args[0]);
32
+ if (fs.existsSync(projectPath) &&
33
+ fs.lstatSync(projectPath).isDirectory()) {
34
+ logMessage({
35
+ message: `A directory already exists with the name ${args[0]}. Please enter a different ${isPlugin ? "plugin" : "project"} name.`,
36
+ type: "warn",
37
+ });
38
+ askProjectName = true;
39
+ }
40
+ else if (args[0].includes(".")) {
41
+ // We don't allow projects to have a dot in the name, as this causes issues for
42
+ // for MikroORM path resolutions.
43
+ logMessage({
44
+ message: `Project names cannot contain a dot (.) character. Please enter a different ${isPlugin ? "plugin" : "project"} name.`,
45
+ type: "error",
46
+ });
47
+ askProjectName = true;
48
+ }
49
+ }
50
+ return askProjectName
51
+ ? await askForProjectName(directoryPath, isPlugin)
52
+ : args[0];
53
+ }
54
+ }
55
+ async function askForProjectName(directoryPath, isPlugin) {
56
+ const { projectName } = await inquirer.prompt([
57
+ {
58
+ type: "input",
59
+ name: "projectName",
60
+ message: `What's the name of your ${isPlugin ? "plugin" : "project"}?`,
61
+ default: isPlugin ? "my-medusa-plugin" : "my-medusa-store",
62
+ filter: (input) => {
63
+ return slugify(input).toLowerCase();
64
+ },
65
+ validate: (input) => {
66
+ // We don't allow projects to have a dot in the name, as this causes issues for
67
+ // for MikroORM path resolutions.
68
+ if (input.includes(".")) {
69
+ return `Project names cannot contain a dot (.) character. Please enter a different ${isPlugin ? "plugin" : "project"} name.`;
70
+ }
71
+ if (!input.length) {
72
+ return `Please enter a ${isPlugin ? "plugin" : "project"} name`;
73
+ }
74
+ const projectPath = path.join(directoryPath || "", input);
75
+ return fs.existsSync(projectPath) &&
76
+ fs.lstatSync(projectPath).isDirectory()
77
+ ? `A directory already exists with the same name. Please enter a different ${isPlugin ? "plugin" : "project"} name.`
78
+ : true;
79
+ },
80
+ },
81
+ ]);
82
+ return projectName;
83
+ }
@@ -0,0 +1,7 @@
1
+ import path from "path";
2
+ export function getBackendDirectory(projectDirectory) {
3
+ return path.join(projectDirectory, "apps", "backend");
4
+ }
5
+ export function getStorefrontDirectory(projectDirectory) {
6
+ return path.join(projectDirectory, "apps", "storefront");
7
+ }
@@ -0,0 +1,13 @@
1
+ import { exec } from "child_process";
2
+ export default ({ directory, abortController, packageManager, }) => {
3
+ const command = packageManager.getCommandStr(`dev`);
4
+ const childProcess = exec(command, {
5
+ cwd: directory,
6
+ signal: abortController?.signal,
7
+ env: {
8
+ ...process.env,
9
+ },
10
+ });
11
+ childProcess.stdout?.pipe(process.stdout);
12
+ childProcess.stderr?.pipe(process.stderr);
13
+ };
@@ -0,0 +1,36 @@
1
+ import { readFileSync, writeFileSync } from "fs";
2
+ export function updatePackageVersions(packageJsonOrPath, version, { applyChanges = false } = {}) {
3
+ const packageJson = typeof packageJsonOrPath === "string"
4
+ ? JSON.parse(readFileSync(packageJsonOrPath, "utf-8"))
5
+ : packageJsonOrPath;
6
+ if (packageJson.dependencies) {
7
+ for (const dependency of Object.keys(packageJson.dependencies)) {
8
+ if (shouldUpdateVersion(dependency)) {
9
+ packageJson.dependencies[dependency] = version;
10
+ }
11
+ }
12
+ }
13
+ if (packageJson.devDependencies) {
14
+ for (const dependency of Object.keys(packageJson.devDependencies)) {
15
+ if (shouldUpdateVersion(dependency)) {
16
+ packageJson.devDependencies[dependency] = version;
17
+ }
18
+ }
19
+ }
20
+ if (packageJson.peerDependencies) {
21
+ for (const dependency of Object.keys(packageJson.peerDependencies)) {
22
+ if (shouldUpdateVersion(dependency)) {
23
+ packageJson.peerDependencies[dependency] = version;
24
+ }
25
+ }
26
+ }
27
+ if (applyChanges && typeof packageJsonOrPath === "string") {
28
+ writeFileSync(packageJsonOrPath, JSON.stringify(packageJson, null, 2));
29
+ }
30
+ return packageJson;
31
+ }
32
+ function shouldUpdateVersion(dependency) {
33
+ // UI package follows different versioning, so we can't update it following
34
+ // the same logic as other Medusa packages
35
+ return dependency.startsWith("@medusajs/") && dependency !== "@medusajs/ui";
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-medusa-app",
3
- "version": "2.22.5",
3
+ "version": "2.22.6",
4
4
  "description": "Create a Medusa project using a single command.",
5
5
  "type": "module",
6
6
  "exports": "./dist/index.js",
@@ -14,8 +14,8 @@
14
14
  "test": "../../../node_modules/.bin/jest --passWithNoTests src"
15
15
  },
16
16
  "dependencies": {
17
- "@medusajs/deps": "2.22.5",
18
- "@medusajs/telemetry": "2.22.5",
17
+ "@medusajs/deps": "2.22.6",
18
+ "@medusajs/telemetry": "2.22.6",
19
19
  "boxen": "^9.0.0",
20
20
  "chalk": "^6.0.1",
21
21
  "commander": "^15.0.0",
@@ -45,4 +45,4 @@
45
45
  "access": "public"
46
46
  },
47
47
  "gitHead": "41a5425405aea5045a26def95c0dc00cf4a5a44d"
48
- }
48
+ }