diff --git a/.changeset/light-dodos-knock.md b/.changeset/light-dodos-knock.md new file mode 100644 index 0000000000000..28c84f2071b16 --- /dev/null +++ b/.changeset/light-dodos-knock.md @@ -0,0 +1,5 @@ +--- +"create-medusa-app": patch +--- + +feat(create-medusa-app): allow passing project name on command line diff --git a/packages/cli/create-medusa-app/src/commands/create.ts b/packages/cli/create-medusa-app/src/commands/create.ts index ba0a95c801cc5..69cf0b0235bff 100644 --- a/packages/cli/create-medusa-app/src/commands/create.ts +++ b/packages/cli/create-medusa-app/src/commands/create.ts @@ -44,17 +44,20 @@ export type CreateOptions = { verbose?: boolean } -export default async ({ - repoUrl = "", - seed, - skipDb, - dbUrl, - browser, - migrations, - directoryPath, - withNextjsStarter = false, - verbose = false, -}: CreateOptions) => { +export default async ( + args: string[], + { + repoUrl = "", + seed, + skipDb, + dbUrl, + browser, + migrations, + directoryPath, + withNextjsStarter = false, + verbose = false, + }: CreateOptions +) => { const nodeVersion = getNodeVersion() if (nodeVersion < MIN_SUPPORTED_NODE_VERSION) { logMessage({ @@ -98,7 +101,20 @@ export default async ({ return }) - const projectName = await askForProjectName(directoryPath) + let askProjectName = args.length === 0 + if (args.length > 0) { + // check if project directory already exists + const projectPath = getProjectPath(args[0], directoryPath) + if (fs.existsSync(projectPath) && fs.lstatSync(projectPath).isDirectory()) { + logMessage({ + message: `A directory already exists with the name ${args[0]}. Please enter a different project name.`, + type: "warn", + }) + askProjectName = true + } + } + + const projectName = askProjectName ? await askForProjectName(directoryPath) : args[0] const projectPath = getProjectPath(projectName, directoryPath) const installNextjs = withNextjsStarter || (await askForNextjsStarter()) diff --git a/packages/cli/create-medusa-app/src/index.ts b/packages/cli/create-medusa-app/src/index.ts index b62a57a89a98d..36db681b7d180 100644 --- a/packages/cli/create-medusa-app/src/index.ts +++ b/packages/cli/create-medusa-app/src/index.ts @@ -4,6 +4,7 @@ import create from "./commands/create.js" program .description("Create a new Medusa project") + .argument("[project-name]", "Name of the project to create.") .option("--repo-url ", "URL of repository to use to setup project.") .option("--seed", "Seed the created database with demo data.") .option( @@ -41,4 +42,4 @@ program ) .parse() -void create(program.opts()) +void create(program.args, program.opts()) diff --git a/packages/cli/create-medusa-app/src/utils/log-message.ts b/packages/cli/create-medusa-app/src/utils/log-message.ts index 62602f8917cd7..b91fa99e03ad8 100644 --- a/packages/cli/create-medusa-app/src/utils/log-message.ts +++ b/packages/cli/create-medusa-app/src/utils/log-message.ts @@ -4,7 +4,7 @@ import { logger } from "./logger.js" type LogOptions = { message: string - type?: "error" | "success" | "info" | "warning" | "verbose" + type?: "error" | "success" | "info" | "warn" | "verbose" } export default ({ message, type = "info" }: LogOptions) => { @@ -15,8 +15,8 @@ export default ({ message, type = "info" }: LogOptions) => { case "success": logger.info(chalk.green(message)) break - case "warning": - logger.warning(chalk.yellow(message)) + case "warn": + logger.warn(chalk.yellow(message)) break case "verbose": logger.info(`${chalk.bgYellowBright("VERBOSE LOG:")} ${message}`)