Skip to content

Commit b44a211

Browse files
committed
feat(create-medusa-app): Allow to create project with specific medusa version
1 parent eed72db commit b44a211

5 files changed

Lines changed: 49 additions & 0 deletions

File tree

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type InstallOptions = {
3131
factBoxOptions: FactBoxOptions
3232
verbose?: boolean
3333
processManager: ProcessManager
34+
version?: string
3435
}
3536

3637
export async function installNextjsStarter({
@@ -39,6 +40,7 @@ export async function installNextjsStarter({
3940
factBoxOptions,
4041
verbose = false,
4142
processManager,
43+
version,
4244
}: InstallOptions): Promise<string> {
4345
factBoxOptions.interval = displayFactBox({
4446
...factBoxOptions,
@@ -70,6 +72,29 @@ export async function installNextjsStarter({
7072
],
7173
{ verbose }
7274
)
75+
76+
if (version) {
77+
const packageJsonPath = path.join(nextjsDirectory, "package.json")
78+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"))
79+
80+
if (packageJson.dependencies) {
81+
for (const dependency of Object.keys(packageJson.dependencies)) {
82+
if (dependency.startsWith("@medusajs/")) {
83+
packageJson.dependencies[dependency] = version
84+
}
85+
}
86+
}
87+
if (packageJson.devDependencies) {
88+
for (const dependency of Object.keys(packageJson.devDependencies)) {
89+
if (dependency.startsWith("@medusajs/")) {
90+
packageJson.devDependencies[dependency] = version
91+
}
92+
}
93+
}
94+
95+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
96+
}
97+
7398
const execOptions = {
7499
signal: abortController?.signal,
75100
cwd: nextjsDirectory,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type PrepareProjectOptions = {
4545
client: Client | null
4646
verbose?: boolean
4747
packageManager: PackageManager
48+
version?: string
4849
}
4950

5051
type PrepareOptions = PreparePluginOptions | PrepareProjectOptions
@@ -128,6 +129,7 @@ async function prepareProject({
128129
client,
129130
verbose = false,
130131
packageManager,
132+
version,
131133
}: PrepareProjectOptions) {
132134
// initialize execution options
133135
const execOptions = {
@@ -159,6 +161,24 @@ async function prepareProject({
159161
// Update name
160162
packageJson.name = projectName
161163

164+
// Update medusa dependencies versions
165+
if (version) {
166+
if (packageJson.dependencies) {
167+
for (const dependency of Object.keys(packageJson.dependencies)) {
168+
if (dependency.startsWith("@medusajs/")) {
169+
packageJson.dependencies[dependency] = version
170+
}
171+
}
172+
}
173+
if (packageJson.devDependencies) {
174+
for (const dependency of Object.keys(packageJson.devDependencies)) {
175+
if (dependency.startsWith("@medusajs/")) {
176+
packageJson.devDependencies[dependency] = version
177+
}
178+
}
179+
}
180+
}
181+
162182
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
163183

164184
// 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()

0 commit comments

Comments
 (0)