Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(create-medusa-app): allow passing project name on command line #10755

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading