feat(create-medusa-app): Allow to create project with specific medusa version (#12882)

* feat(create-medusa-app): Allow to create project with specific medusa version

* feat(create-medusa-app): Allow to create project with specific medusa version

* feat(create-medusa-app): Allow to create project with specific medusa version

* naming

* Create smart-singers-dress.md
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2025-07-03 15:58:18 +02:00
1 parent eed72db502
commit 779ed018b9
7 files changed
+57

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"create-medusa-app": patch
---
feat(create-medusa-app): Allow to create project with specific medusa version
@@ -7,6 +7,7 @@ program
.argument("[project-name]", "Name of the project to create.") .argument("[project-name]", "Name of the project to create.")
.option("--plugin", "Create a plugin instead of a project.") .option("--plugin", "Create a plugin instead of a project.")
.option("--repo-url <url>", "URL of repository to use to setup project.") .option("--repo-url <url>", "URL of repository to use to setup project.")
.option("--version <version>", "The version of Medusa packages to install.")
.option("--seed", "Seed the created database with demo data.") .option("--seed", "Seed the created database with demo data.")
.option( .option(
"--skip-db", "--skip-db",
@@ -8,6 +8,7 @@ import execute from "./execute.js"
import { displayFactBox, FactBoxOptions } from "./facts.js" import { displayFactBox, FactBoxOptions } from "./facts.js"
import logMessage from "./log-message.js" import logMessage from "./log-message.js"
import ProcessManager from "./process-manager.js" import ProcessManager from "./process-manager.js"
import { updatePackageVersions } from "./update-package-versions.js"
const NEXTJS_REPO = "https://github.com/medusajs/nextjs-starter-medusa" const NEXTJS_REPO = "https://github.com/medusajs/nextjs-starter-medusa"
const NEXTJS_BRANCH = "main" const NEXTJS_BRANCH = "main"
@@ -31,6 +32,7 @@ type InstallOptions = {
factBoxOptions: FactBoxOptions factBoxOptions: FactBoxOptions
verbose?: boolean verbose?: boolean
processManager: ProcessManager processManager: ProcessManager
version?: string
} }
export async function installNextjsStarter({ export async function installNextjsStarter({
@@ -39,6 +41,7 @@ export async function installNextjsStarter({
factBoxOptions, factBoxOptions,
verbose = false, verbose = false,
processManager, processManager,
version,
}: InstallOptions): Promise<string> { }: InstallOptions): Promise<string> {
factBoxOptions.interval = displayFactBox({ factBoxOptions.interval = displayFactBox({
...factBoxOptions, ...factBoxOptions,
@@ -70,6 +73,12 @@ export async function installNextjsStarter({
], ],
{ verbose } { verbose }
) )
if (version) {
const packageJsonPath = path.join(nextjsDirectory, "package.json")
updatePackageVersions(packageJsonPath, version, { applyChanges: true })
}
const execOptions = { const execOptions = {
signal: abortController?.signal, signal: abortController?.signal,
cwd: nextjsDirectory, cwd: nextjsDirectory,
@@ -7,6 +7,7 @@ import { displayFactBox, FactBoxOptions } from "./facts.js"
import ProcessManager from "./process-manager.js" import ProcessManager from "./process-manager.js"
import type { Client } from "pg" import type { Client } from "pg"
import PackageManager from "./package-manager.js" import PackageManager from "./package-manager.js"
import { updatePackageVersions } from "./update-package-versions.js"
const ADMIN_EMAIL = "admin@medusa-test.com" const ADMIN_EMAIL = "admin@medusa-test.com"
let STORE_CORS = "http://localhost:8000" let STORE_CORS = "http://localhost:8000"
@@ -45,6 +46,7 @@ type PrepareProjectOptions = {
client: Client | null client: Client | null
verbose?: boolean verbose?: boolean
packageManager: PackageManager packageManager: PackageManager
version?: string
} }
type PrepareOptions = PreparePluginOptions | PrepareProjectOptions type PrepareOptions = PreparePluginOptions | PrepareProjectOptions
@@ -128,6 +130,7 @@ async function prepareProject({
client, client,
verbose = false, verbose = false,
packageManager, packageManager,
version,
}: PrepareProjectOptions) { }: PrepareProjectOptions) {
// initialize execution options // initialize execution options
const execOptions = { const execOptions = {
@@ -159,6 +162,11 @@ async function prepareProject({
// Update name // Update name
packageJson.name = projectName packageJson.name = projectName
// Update medusa dependencies versions
if (version) {
updatePackageVersions(packageJson, version)
}
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)) fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
// initialize the invite token to return // initialize the invite token to return
@@ -16,6 +16,7 @@ export interface ProjectOptions {
withNextjsStarter?: boolean withNextjsStarter?: boolean
verbose?: boolean verbose?: boolean
plugin?: boolean plugin?: boolean
version?: string
} }
export interface ProjectCreator { export interface ProjectCreator {
@@ -96,6 +96,7 @@ export class MedusaProjectCreator
factBoxOptions: this.factBoxOptions, factBoxOptions: this.factBoxOptions,
verbose: this.options.verbose, verbose: this.options.verbose,
processManager: this.processManager, processManager: this.processManager,
version: this.options.version,
}) })
} }
} catch (e) { } catch (e) {
@@ -154,6 +155,7 @@ export class MedusaProjectCreator
client: this.client, client: this.client,
verbose: this.options.verbose, verbose: this.options.verbose,
packageManager: this.packageManager, packageManager: this.packageManager,
version: this.options.version,
}) })
} finally { } finally {
await this.client?.end() await this.client?.end()
@@ -0,0 +1,31 @@
import { readFileSync, writeFileSync } from "fs"
export function updatePackageVersions(
packageJsonOrPath: string | Record<string, any>,
version: string,
{ applyChanges = false }: { applyChanges?: boolean } = {}
) {
const packageJson =
typeof packageJsonOrPath === "string"
? JSON.parse(readFileSync(packageJsonOrPath, "utf-8"))
: packageJsonOrPath
if (packageJson.dependencies) {
for (const dependency of Object.keys(packageJson.dependencies)) {
if (dependency.startsWith("@medusajs/")) {
packageJson.dependencies[dependency] = version
}
}
}
if (packageJson.devDependencies) {
for (const dependency of Object.keys(packageJson.devDependencies)) {
if (dependency.startsWith("@medusajs/")) {
packageJson.devDependencies[dependency] = version
}
}
}
if (applyChanges && typeof packageJsonOrPath === "string") {
writeFileSync(packageJsonOrPath, JSON.stringify(packageJson, null, 2))
}
}