Skip to content

Commit

Permalink
feat(create-medusa-app): allow passing project name on command line (#…
Browse files Browse the repository at this point in the history
…10755)

Allow passing the project name on the command line:

```bash
npx create-medusa-app@latest my-project
```
  • Loading branch information
shahednasser authored Dec 29, 2024
1 parent 1c355da commit 1e6d56b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-dodos-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-medusa-app": patch
---

feat(create-medusa-app): allow passing project name on command line
40 changes: 28 additions & 12 deletions packages/cli/create-medusa-app/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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())

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/create-medusa-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>", "URL of repository to use to setup project.")
.option("--seed", "Seed the created database with demo data.")
.option(
Expand Down Expand Up @@ -41,4 +42,4 @@ program
)
.parse()

void create(program.opts())
void create(program.args, program.opts())
6 changes: 3 additions & 3 deletions packages/cli/create-medusa-app/src/utils/log-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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}`)
Expand Down

0 comments on commit 1e6d56b

Please sign in to comment.