From fbc369705d4feae21d77ea2fc59173ac9519cee6 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Mon, 25 Mar 2024 14:51:15 +0200 Subject: [PATCH] feat(create-medusa-app): Added --v2 option (#6729) ## What Added a `--v2` option to the `create-medusa-app` command that clones the [feat/v2](https://github.com/medusajs/medusa-starter-default/pull/150) branch of the starter default and makes minor changes to the setup process. ## Why This option is supposed to make it easier to test out internally a Medusa setup with all the commerce modules. The starter's branch installs and configures the modules + the V2 feature flag. ## Testing To test it out, run the snapshot with the `--v2` option. ## Notes - I couldn't install the new admin dashboard in the starter branch as it's not on NPM yet (at the time of writing this), so at the moment, this opens the current admin dashboard instead. - When trying to create an invite using the CLI tool I get the error `Unable to resolve inviteService`. Not sure if we should also make changes to the CLI tool to allow creating an invite when V2 is enabled (I know now invites are creating within the User Module, so maybe when the V2 feature flag is enabled that should be resolved instead), but for now I just disabled running the create invite command as this is mainly for internal testing. Let me know what the suggested approach is here. --- .changeset/big-kings-trade.md | 5 +++++ .../create-medusa-app/src/commands/create.ts | 4 ++++ packages/create-medusa-app/src/index.ts | 5 +++++ .../create-medusa-app/src/utils/clone-repo.ts | 17 ++++++++++++++--- .../src/utils/prepare-project.ts | 11 +++++++++-- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 .changeset/big-kings-trade.md diff --git a/.changeset/big-kings-trade.md b/.changeset/big-kings-trade.md new file mode 100644 index 0000000000..d57e5f5196 --- /dev/null +++ b/.changeset/big-kings-trade.md @@ -0,0 +1,5 @@ +--- +"create-medusa-app": patch +--- + +feat(create-medusa-app): Add `--v2` option diff --git a/packages/create-medusa-app/src/commands/create.ts b/packages/create-medusa-app/src/commands/create.ts index f642e32afc..80d71ccae6 100644 --- a/packages/create-medusa-app/src/commands/create.ts +++ b/packages/create-medusa-app/src/commands/create.ts @@ -42,6 +42,7 @@ export type CreateOptions = { migrations?: boolean directoryPath?: string withNextjsStarter?: boolean + v2?: boolean } export default async ({ @@ -54,6 +55,7 @@ export default async ({ migrations, directoryPath, withNextjsStarter = false, + v2 = false, }: CreateOptions) => { track("CREATE_CLI_CMA") @@ -134,6 +136,7 @@ export default async ({ repoUrl, abortController, spinner, + v2, }) } catch { return @@ -184,6 +187,7 @@ export default async ({ onboardingType: installNextjs ? "nextjs" : "default", nextjsDirectory, client, + v2, }) } catch (e: any) { if (isAbortError(e)) { diff --git a/packages/create-medusa-app/src/index.ts b/packages/create-medusa-app/src/index.ts index 35a63a756d..3963e5e098 100644 --- a/packages/create-medusa-app/src/index.ts +++ b/packages/create-medusa-app/src/index.ts @@ -38,6 +38,11 @@ program "Install the Next.js starter along with the Medusa backend", false ) + .option( + "--v2", + "Install Medusa with the V2 feature flag enabled. WARNING: Medusa V2 is still in development and shouldn't be used in production.", + false + ) .parse() void create(program.opts()) diff --git a/packages/create-medusa-app/src/utils/clone-repo.ts b/packages/create-medusa-app/src/utils/clone-repo.ts index 872237d987..f0a85efe16 100644 --- a/packages/create-medusa-app/src/utils/clone-repo.ts +++ b/packages/create-medusa-app/src/utils/clone-repo.ts @@ -9,18 +9,26 @@ type CloneRepoOptions = { directoryName?: string repoUrl?: string abortController?: AbortController + v2?: boolean } const DEFAULT_REPO = "https://github.com/medusajs/medusa-starter-default" +const V2_BRANCH = "feat/v2" export default async function cloneRepo({ directoryName = "", repoUrl, abortController, + v2 = false, }: CloneRepoOptions) { - await promiseExec(`git clone ${repoUrl || DEFAULT_REPO} ${directoryName}`, { - signal: abortController?.signal, - }) + await promiseExec( + `git clone ${repoUrl || DEFAULT_REPO}${ + v2 ? ` -b ${V2_BRANCH}` : "" + } ${directoryName}`, + { + signal: abortController?.signal, + } + ) } export async function runCloneRepo({ @@ -28,17 +36,20 @@ export async function runCloneRepo({ repoUrl, abortController, spinner, + v2 = false, }: { projectName: string repoUrl: string abortController: AbortController spinner: Ora + v2?: boolean }) { try { await cloneRepo({ directoryName: projectName, repoUrl, abortController, + v2, }) deleteGitDirectory(projectName) diff --git a/packages/create-medusa-app/src/utils/prepare-project.ts b/packages/create-medusa-app/src/utils/prepare-project.ts index e76005f947..1dd041ae59 100644 --- a/packages/create-medusa-app/src/utils/prepare-project.ts +++ b/packages/create-medusa-app/src/utils/prepare-project.ts @@ -25,6 +25,7 @@ type PrepareOptions = { onboardingType?: "default" | "nextjs" nextjsDirectory?: string client: Client | null + v2?: boolean } export default async ({ @@ -41,6 +42,7 @@ export default async ({ onboardingType = "default", nextjsDirectory = "", client, + v2 = false, }: PrepareOptions) => { // initialize execution options const execOptions = { @@ -69,6 +71,9 @@ export default async ({ if (!skipDb) { let env = `DATABASE_TYPE=postgres${EOL}DATABASE_URL=${dbConnectionString}${EOL}MEDUSA_ADMIN_ONBOARDING_TYPE=${onboardingType}${EOL}STORE_CORS=http://localhost:8000,http://localhost:7001` + if (v2) { + env += `${EOL}POSTGRES_URL=${dbConnectionString}` + } if (nextjsDirectory) { env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}` } @@ -153,7 +158,9 @@ export default async ({ // to ensure that migrations ran let errorOccurred = false try { - const migrations = await client.query(`SELECT * FROM "migrations"`) + const migrations = await client.query( + `SELECT * FROM "${v2 ? "mikro_orm_migrations" : "migrations"}"` + ) errorOccurred = migrations.rowCount == 0 } catch (e) { // avoid error thrown if the migrations table @@ -179,7 +186,7 @@ export default async ({ }) } - if (admin && !skipDb && migrations) { + if (admin && !skipDb && migrations && !v2) { // create admin user factBoxOptions.interval = displayFactBox({ ...factBoxOptions,