Skip to content

Commit 779ed01

Browse files
authored
feat(create-medusa-app): Allow to create project with specific medusa version (medusajs#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
1 parent eed72db commit 779ed01

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

.changeset/smart-singers-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-medusa-app": patch
3+
---
4+
5+
feat(create-medusa-app): Allow to create project with specific medusa version

packages/cli/create-medusa-app/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ program
77
.argument("[project-name]", "Name of the project to create.")
88
.option("--plugin", "Create a plugin instead of a project.")
99
.option("--repo-url <url>", "URL of repository to use to setup project.")
10+
.option("--version <version>", "The version of Medusa packages to install.")
1011
.option("--seed", "Seed the created database with demo data.")
1112
.option(
1213
"--skip-db",

packages/cli/create-medusa-app/src/utils/nextjs-utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import execute from "./execute.js"
88
import { displayFactBox, FactBoxOptions } from "./facts.js"
99
import logMessage from "./log-message.js"
1010
import ProcessManager from "./process-manager.js"
11+
import { updatePackageVersions } from "./update-package-versions.js"
1112

1213
const NEXTJS_REPO = "https://github.com/medusajs/nextjs-starter-medusa"
1314
const NEXTJS_BRANCH = "main"
@@ -31,6 +32,7 @@ type InstallOptions = {
3132
factBoxOptions: FactBoxOptions
3233
verbose?: boolean
3334
processManager: ProcessManager
35+
version?: string
3436
}
3537

3638
export async function installNextjsStarter({
@@ -39,6 +41,7 @@ export async function installNextjsStarter({
3941
factBoxOptions,
4042
verbose = false,
4143
processManager,
44+
version,
4245
}: InstallOptions): Promise<string> {
4346
factBoxOptions.interval = displayFactBox({
4447
...factBoxOptions,
@@ -70,6 +73,12 @@ export async function installNextjsStarter({
7073
],
7174
{ verbose }
7275
)
76+
77+
if (version) {
78+
const packageJsonPath = path.join(nextjsDirectory, "package.json")
79+
updatePackageVersions(packageJsonPath, version, { applyChanges: true })
80+
}
81+
7382
const execOptions = {
7483
signal: abortController?.signal,
7584
cwd: nextjsDirectory,

packages/cli/create-medusa-app/src/utils/prepare-project.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { displayFactBox, FactBoxOptions } from "./facts.js"
77
import ProcessManager from "./process-manager.js"
88
import type { Client } from "pg"
99
import PackageManager from "./package-manager.js"
10+
import { updatePackageVersions } from "./update-package-versions.js"
1011

1112
const ADMIN_EMAIL = "[email protected]"
1213
let STORE_CORS = "http://localhost:8000"
@@ -45,6 +46,7 @@ type PrepareProjectOptions = {
4546
client: Client | null
4647
verbose?: boolean
4748
packageManager: PackageManager
49+
version?: string
4850
}
4951

5052
type PrepareOptions = PreparePluginOptions | PrepareProjectOptions
@@ -128,6 +130,7 @@ async function prepareProject({
128130
client,
129131
verbose = false,
130132
packageManager,
133+
version,
131134
}: PrepareProjectOptions) {
132135
// initialize execution options
133136
const execOptions = {
@@ -159,6 +162,11 @@ async function prepareProject({
159162
// Update name
160163
packageJson.name = projectName
161164

165+
// Update medusa dependencies versions
166+
if (version) {
167+
updatePackageVersions(packageJson, version)
168+
}
169+
162170
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
163171

164172
// initialize the invite token to return

packages/cli/create-medusa-app/src/utils/project-creator/creator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ProjectOptions {
1616
withNextjsStarter?: boolean
1717
verbose?: boolean
1818
plugin?: boolean
19+
version?: string
1920
}
2021

2122
export interface ProjectCreator {

packages/cli/create-medusa-app/src/utils/project-creator/medusa-project-creator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class MedusaProjectCreator
9696
factBoxOptions: this.factBoxOptions,
9797
verbose: this.options.verbose,
9898
processManager: this.processManager,
99+
version: this.options.version,
99100
})
100101
}
101102
} catch (e) {
@@ -154,6 +155,7 @@ export class MedusaProjectCreator
154155
client: this.client,
155156
verbose: this.options.verbose,
156157
packageManager: this.packageManager,
158+
version: this.options.version,
157159
})
158160
} finally {
159161
await this.client?.end()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { readFileSync, writeFileSync } from "fs"
2+
3+
export function updatePackageVersions(
4+
packageJsonOrPath: string | Record<string, any>,
5+
version: string,
6+
{ applyChanges = false }: { applyChanges?: boolean } = {}
7+
) {
8+
const packageJson =
9+
typeof packageJsonOrPath === "string"
10+
? JSON.parse(readFileSync(packageJsonOrPath, "utf-8"))
11+
: packageJsonOrPath
12+
13+
if (packageJson.dependencies) {
14+
for (const dependency of Object.keys(packageJson.dependencies)) {
15+
if (dependency.startsWith("@medusajs/")) {
16+
packageJson.dependencies[dependency] = version
17+
}
18+
}
19+
}
20+
if (packageJson.devDependencies) {
21+
for (const dependency of Object.keys(packageJson.devDependencies)) {
22+
if (dependency.startsWith("@medusajs/")) {
23+
packageJson.devDependencies[dependency] = version
24+
}
25+
}
26+
}
27+
28+
if (applyChanges && typeof packageJsonOrPath === "string") {
29+
writeFileSync(packageJsonOrPath, JSON.stringify(packageJson, null, 2))
30+
}
31+
}

0 commit comments

Comments
 (0)