create-medusa-app 2.22.4 → 2.22.5

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.
Files changed (34) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +9 -0
  3. package/dist/index.js +0 -0
  4. package/package.json +4 -4
  5. package/src/utils/__tests__/prepare-project.test.ts +65 -0
  6. package/src/utils/__tests__/update-package-versions.test.ts +41 -0
  7. package/src/utils/prepare-project.ts +7 -0
  8. package/src/utils/project-creator/medusa-plugin-creator.ts +1 -0
  9. package/src/utils/update-package-versions.ts +7 -0
  10. package/dist/commands/create.js +0 -10
  11. package/dist/utils/claude-code-plugin.js +0 -76
  12. package/dist/utils/clone-repo.js +0 -77
  13. package/dist/utils/create-abort-controller.js +0 -11
  14. package/dist/utils/create-db.js +0 -188
  15. package/dist/utils/execute.js +0 -51
  16. package/dist/utils/facts.js +0 -87
  17. package/dist/utils/format-connection-string.js +0 -17
  18. package/dist/utils/get-current-os.js +0 -10
  19. package/dist/utils/log-message.js +0 -21
  20. package/dist/utils/logger.js +0 -8
  21. package/dist/utils/nextjs-utils.js +0 -55
  22. package/dist/utils/node-version.js +0 -15
  23. package/dist/utils/package-manager.js +0 -354
  24. package/dist/utils/postgres-client.js +0 -9
  25. package/dist/utils/prepare-project.js +0 -191
  26. package/dist/utils/process-manager.js +0 -50
  27. package/dist/utils/project-creator/creator.js +0 -46
  28. package/dist/utils/project-creator/index.js +0 -4
  29. package/dist/utils/project-creator/medusa-plugin-creator.js +0 -95
  30. package/dist/utils/project-creator/medusa-project-creator.js +0 -215
  31. package/dist/utils/project-creator/project-creator-factory.js +0 -83
  32. package/dist/utils/project-paths.js +0 -7
  33. package/dist/utils/start-medusa.js +0 -13
  34. package/dist/utils/update-package-versions.js +0 -29
@@ -1,191 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { randomBytes } from "crypto";
4
- import { EOL } from "os";
5
- import { displayFactBox } from "./facts.js";
6
- import { updatePackageVersions } from "./update-package-versions.js";
7
- import { getBackendDirectory } from "./project-paths.js";
8
- const ADMIN_EMAIL = "admin@medusa-test.com";
9
- let STORE_CORS = "http://localhost:8000";
10
- let ADMIN_CORS = "http://localhost:5173,http://localhost:9000";
11
- const DOCS_CORS = "https://docs.medusajs.com";
12
- const AUTH_CORS = [ADMIN_CORS, STORE_CORS, DOCS_CORS].join(",");
13
- STORE_CORS += `,${DOCS_CORS}`;
14
- ADMIN_CORS += `,${DOCS_CORS}`;
15
- const DEFAULT_REDIS_URL = "redis://localhost:6379";
16
- const AUTH_MFA_ENCRYPTION_KEY = "AUTH_MFA_ENCRYPTION_KEY";
17
- export default async (prepareOptions) => {
18
- if (prepareOptions.isPlugin) {
19
- return preparePlugin(prepareOptions);
20
- }
21
- return prepareProject(prepareOptions);
22
- };
23
- async function preparePlugin({ directory, projectName, spinner, processManager, abortController, verbose = false, packageManager, }) {
24
- // initialize execution options
25
- const execOptions = {
26
- cwd: directory,
27
- signal: abortController?.signal,
28
- };
29
- const factBoxOptions = {
30
- interval: null,
31
- spinner,
32
- processManager,
33
- message: "",
34
- title: "",
35
- verbose,
36
- };
37
- // Update package.json
38
- const packageJsonPath = path.join(directory, "package.json");
39
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
40
- // Update name
41
- packageJson.name = projectName;
42
- // Add packageManager field to ensure consistent version usage
43
- const packageManagerString = await packageManager.getPackageManagerString();
44
- if (packageManagerString) {
45
- packageJson.packageManager = packageManagerString;
46
- }
47
- fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
48
- factBoxOptions.interval = displayFactBox({
49
- ...factBoxOptions,
50
- spinner,
51
- title: "Installing dependencies...",
52
- processManager,
53
- });
54
- await packageManager.installDependencies(execOptions);
55
- factBoxOptions.interval = displayFactBox({
56
- ...factBoxOptions,
57
- message: "Installed Dependencies",
58
- });
59
- displayFactBox({ ...factBoxOptions, message: "Finished Preparation" });
60
- }
61
- async function prepareProject({ directory, projectName, dbName, dbConnectionString, seed, spinner, processManager, abortController, skipDb, migrations, onboardingType = "default", nextjsDirectory = "", client, verbose = false, packageManager, version, }) {
62
- const backendDirectory = getBackendDirectory(directory);
63
- // initialize execution options
64
- const execOptions = {
65
- cwd: directory,
66
- signal: abortController?.signal,
67
- };
68
- const backendExecOptions = {
69
- cwd: backendDirectory,
70
- signal: abortController?.signal,
71
- };
72
- const npxOptions = {
73
- ...backendExecOptions,
74
- env: {
75
- ...process.env,
76
- npm_config_yes: "yes",
77
- },
78
- };
79
- const factBoxOptions = {
80
- interval: null,
81
- spinner,
82
- processManager,
83
- message: "",
84
- title: "",
85
- verbose,
86
- };
87
- // Add packageManager field to ensure consistent version usage
88
- const packageManagerString = await packageManager.getPackageManagerString();
89
- // Update root package.json name and packageManager
90
- const packageJsonPath = path.join(directory, "package.json");
91
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
92
- packageJson.name = projectName;
93
- if (packageManagerString) {
94
- packageJson.packageManager = packageManagerString;
95
- }
96
- else {
97
- delete packageJson.packageManager;
98
- }
99
- fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
100
- // Update backend package.json
101
- const backendPackageJsonPath = path.join(backendDirectory, "package.json");
102
- const backendPackageJson = JSON.parse(fs.readFileSync(backendPackageJsonPath, "utf-8"));
103
- if (packageManagerString) {
104
- backendPackageJson.packageManager = packageManagerString;
105
- }
106
- // Update medusa dependencies versions
107
- if (version) {
108
- updatePackageVersions(backendPackageJson, version);
109
- }
110
- fs.writeFileSync(backendPackageJsonPath, JSON.stringify(backendPackageJson, null, 2));
111
- // initialize the invite token to return
112
- let inviteToken = undefined;
113
- // add environment variables
114
- let env = `MEDUSA_ADMIN_ONBOARDING_TYPE=${onboardingType}${EOL}STORE_CORS=${STORE_CORS}${EOL}ADMIN_CORS=${ADMIN_CORS}${EOL}AUTH_CORS=${AUTH_CORS}${EOL}REDIS_URL=${DEFAULT_REDIS_URL}${EOL}JWT_SECRET=supersecret${EOL}COOKIE_SECRET=supersecret${EOL}${AUTH_MFA_ENCRYPTION_KEY}=${randomBytes(32).toString("hex")}`;
115
- if (!skipDb) {
116
- if (dbName) {
117
- env += `${EOL}DB_NAME=${dbName}`;
118
- dbConnectionString = dbConnectionString.replace(dbName, "$DB_NAME");
119
- }
120
- env += `${EOL}DATABASE_URL=${dbConnectionString}`;
121
- }
122
- if (nextjsDirectory) {
123
- env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}`;
124
- }
125
- fs.appendFileSync(path.join(backendDirectory, `.env`), env);
126
- factBoxOptions.interval = displayFactBox({
127
- ...factBoxOptions,
128
- spinner,
129
- title: "Installing dependencies...",
130
- processManager,
131
- });
132
- await packageManager.installDependencies(execOptions, {
133
- installLegacyPeerDeps: true,
134
- });
135
- factBoxOptions.interval = displayFactBox({
136
- ...factBoxOptions,
137
- message: "Installed Dependencies",
138
- });
139
- if (!skipDb && migrations) {
140
- factBoxOptions.interval = displayFactBox({
141
- ...factBoxOptions,
142
- title: "Running Migrations...",
143
- });
144
- // run migrations
145
- const migrationExecResult = await packageManager.runMedusaCommand("db:migrate", npxOptions, {
146
- verbose,
147
- needOutput: true,
148
- });
149
- if (client) {
150
- // check the migrations table is in the database
151
- // to ensure that migrations ran
152
- let errorOccurred = false;
153
- try {
154
- const migrations = await client.query(`SELECT count(tablename) from pg_tables WHERE tablename = 'mikro_orm_migrations'`);
155
- errorOccurred = migrations.rowCount == 0;
156
- }
157
- catch (e) {
158
- // avoid error thrown if the migrations table
159
- // doesn't exist
160
- errorOccurred = true;
161
- }
162
- // ensure that migrations actually ran in case of an uncaught error
163
- if (errorOccurred &&
164
- (migrationExecResult.stderr || migrationExecResult.stdout)) {
165
- throw new Error(`An error occurred while running migrations: ${migrationExecResult.stderr || migrationExecResult.stdout}`);
166
- }
167
- }
168
- factBoxOptions.interval = displayFactBox({
169
- ...factBoxOptions,
170
- message: "Ran Migrations",
171
- });
172
- const userExecResult = (await packageManager.runMedusaCommand(`user -e ${ADMIN_EMAIL} --invite`, npxOptions, { verbose, needOutput: true }));
173
- // get invite token from stdout
174
- const match = userExecResult.stdout.match(/Invite token: (?<token>.+)/);
175
- inviteToken = match?.groups?.token;
176
- }
177
- // if installation includes Next.js, retrieve the publishable API key
178
- // from the backend and add it as an enviornment variable
179
- if (nextjsDirectory && client) {
180
- const apiKeys = await client.query(`SELECT * FROM "api_key" WHERE type = 'publishable'`);
181
- if (apiKeys.rowCount) {
182
- const nextjsEnvPath = path.join(nextjsDirectory, fs.existsSync(path.join(nextjsDirectory, ".env.local"))
183
- ? ".env.local"
184
- : ".env.template");
185
- const originalContent = fs.readFileSync(nextjsEnvPath, "utf-8");
186
- fs.writeFileSync(nextjsEnvPath, originalContent.replace("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=", `NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=${apiKeys.rows[0].token}`));
187
- }
188
- }
189
- displayFactBox({ ...factBoxOptions, message: "Finished Preparation" });
190
- return inviteToken;
191
- }
@@ -1,50 +0,0 @@
1
- export default class ProcessManager {
2
- intervals = [];
3
- static MAX_RETRIES = 3;
4
- constructor() {
5
- this.onTerminated(() => {
6
- this.intervals.forEach((interval) => {
7
- clearInterval(interval);
8
- });
9
- });
10
- }
11
- onTerminated(fn) {
12
- process.on("SIGTERM", async () => fn());
13
- process.on("SIGINT", async () => fn());
14
- }
15
- addInterval(interval) {
16
- this.intervals.push(interval);
17
- }
18
- // when running commands with npx or npm sometimes they
19
- // terminate with EAGAIN error unexpectedly
20
- // this utility function allows retrying the process if
21
- // EAGAIN occurs, or otherwise throw the error that occurs
22
- async runProcess({ process, ignoreERESOLVE }) {
23
- let processError = false;
24
- let retries = 0;
25
- do {
26
- ++retries;
27
- try {
28
- return await process();
29
- }
30
- catch (error) {
31
- if (typeof error === "object" &&
32
- error !== null &&
33
- "code" in error &&
34
- error?.code === "EAGAIN") {
35
- processError = true;
36
- }
37
- else if (ignoreERESOLVE &&
38
- typeof error === "object" &&
39
- error !== null &&
40
- "code" in error &&
41
- error?.code === "ERESOLVE") {
42
- // ignore error
43
- }
44
- else {
45
- throw error;
46
- }
47
- }
48
- } while (processError && retries <= ProcessManager.MAX_RETRIES);
49
- }
50
- }
@@ -1,46 +0,0 @@
1
- import ora from "ora";
2
- import path from "path";
3
- import createAbortController from "../create-abort-controller.js";
4
- import ProcessManager from "../process-manager.js";
5
- import PackageManager from "../package-manager.js";
6
- // Base class for common project functionality
7
- export class BaseProjectCreator {
8
- options;
9
- args;
10
- spinner;
11
- processManager;
12
- packageManager;
13
- abortController;
14
- factBoxOptions;
15
- projectName;
16
- projectPath;
17
- isProjectCreated = false;
18
- printedMessage = false;
19
- constructor(projectName, options, args) {
20
- this.options = options;
21
- this.args = args;
22
- this.spinner = ora();
23
- this.processManager = new ProcessManager();
24
- this.packageManager = new PackageManager(this.processManager, {
25
- verbose: options.verbose,
26
- useNpm: options.useNpm,
27
- usePnpm: options.usePnpm,
28
- useYarn: options.useYarn,
29
- });
30
- this.abortController = createAbortController(this.processManager);
31
- this.projectName = projectName;
32
- const basePath = typeof options.directoryPath === "string" ? options.directoryPath : "";
33
- this.projectPath = path.join(basePath, projectName);
34
- this.factBoxOptions = {
35
- interval: null,
36
- spinner: this.spinner,
37
- processManager: this.processManager,
38
- message: "",
39
- title: "",
40
- verbose: options.verbose || false,
41
- };
42
- }
43
- getProjectPath(projectName) {
44
- return path.join(this.options.directoryPath ?? "", projectName);
45
- }
46
- }
@@ -1,4 +0,0 @@
1
- export * from "./creator.js";
2
- export * from "./medusa-plugin-creator.js";
3
- export * from "./medusa-project-creator.js";
4
- export * from "./project-creator-factory.js";
@@ -1,95 +0,0 @@
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
- });
63
- }
64
- handleError(e) {
65
- if (isAbortError(e)) {
66
- process.exit();
67
- }
68
- this.spinner.stop();
69
- logMessage({
70
- message: `An error occurred while preparing plugin: ${e}`,
71
- type: "error",
72
- });
73
- }
74
- showSuccessMessage() {
75
- logMessage({
76
- 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.`), {
77
- titleAlignment: "center",
78
- textAlignment: "center",
79
- padding: 1,
80
- margin: 1,
81
- float: "center",
82
- }),
83
- });
84
- }
85
- setupProcessManager() {
86
- this.processManager.onTerminated(async () => {
87
- this.spinner.stop();
88
- if (!this.printedMessage && this.isProjectCreated) {
89
- this.printedMessage = true;
90
- this.showSuccessMessage();
91
- }
92
- return;
93
- });
94
- }
95
- }
@@ -1,215 +0,0 @@
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
- }
@@ -1,83 +0,0 @@
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
- }