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:
committed by
GitHub
parent
eed72db502
commit
779ed018b9
5
.changeset/smart-singers-dress.md
Normal file
5
.changeset/smart-singers-dress.md
Normal 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.")
|
||||
.option("--plugin", "Create a plugin instead of a 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(
|
||||
"--skip-db",
|
||||
|
||||
@@ -8,6 +8,7 @@ import execute from "./execute.js"
|
||||
import { displayFactBox, FactBoxOptions } from "./facts.js"
|
||||
import logMessage from "./log-message.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_BRANCH = "main"
|
||||
@@ -31,6 +32,7 @@ type InstallOptions = {
|
||||
factBoxOptions: FactBoxOptions
|
||||
verbose?: boolean
|
||||
processManager: ProcessManager
|
||||
version?: string
|
||||
}
|
||||
|
||||
export async function installNextjsStarter({
|
||||
@@ -39,6 +41,7 @@ export async function installNextjsStarter({
|
||||
factBoxOptions,
|
||||
verbose = false,
|
||||
processManager,
|
||||
version,
|
||||
}: InstallOptions): Promise<string> {
|
||||
factBoxOptions.interval = displayFactBox({
|
||||
...factBoxOptions,
|
||||
@@ -70,6 +73,12 @@ export async function installNextjsStarter({
|
||||
],
|
||||
{ verbose }
|
||||
)
|
||||
|
||||
if (version) {
|
||||
const packageJsonPath = path.join(nextjsDirectory, "package.json")
|
||||
updatePackageVersions(packageJsonPath, version, { applyChanges: true })
|
||||
}
|
||||
|
||||
const execOptions = {
|
||||
signal: abortController?.signal,
|
||||
cwd: nextjsDirectory,
|
||||
|
||||
@@ -7,6 +7,7 @@ import { displayFactBox, FactBoxOptions } from "./facts.js"
|
||||
import ProcessManager from "./process-manager.js"
|
||||
import type { Client } from "pg"
|
||||
import PackageManager from "./package-manager.js"
|
||||
import { updatePackageVersions } from "./update-package-versions.js"
|
||||
|
||||
const ADMIN_EMAIL = "admin@medusa-test.com"
|
||||
let STORE_CORS = "http://localhost:8000"
|
||||
@@ -45,6 +46,7 @@ type PrepareProjectOptions = {
|
||||
client: Client | null
|
||||
verbose?: boolean
|
||||
packageManager: PackageManager
|
||||
version?: string
|
||||
}
|
||||
|
||||
type PrepareOptions = PreparePluginOptions | PrepareProjectOptions
|
||||
@@ -128,6 +130,7 @@ async function prepareProject({
|
||||
client,
|
||||
verbose = false,
|
||||
packageManager,
|
||||
version,
|
||||
}: PrepareProjectOptions) {
|
||||
// initialize execution options
|
||||
const execOptions = {
|
||||
@@ -159,6 +162,11 @@ async function prepareProject({
|
||||
// Update name
|
||||
packageJson.name = projectName
|
||||
|
||||
// Update medusa dependencies versions
|
||||
if (version) {
|
||||
updatePackageVersions(packageJson, version)
|
||||
}
|
||||
|
||||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
|
||||
|
||||
// initialize the invite token to return
|
||||
|
||||
@@ -16,6 +16,7 @@ export interface ProjectOptions {
|
||||
withNextjsStarter?: boolean
|
||||
verbose?: boolean
|
||||
plugin?: boolean
|
||||
version?: string
|
||||
}
|
||||
|
||||
export interface ProjectCreator {
|
||||
|
||||
@@ -96,6 +96,7 @@ export class MedusaProjectCreator
|
||||
factBoxOptions: this.factBoxOptions,
|
||||
verbose: this.options.verbose,
|
||||
processManager: this.processManager,
|
||||
version: this.options.version,
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -154,6 +155,7 @@ export class MedusaProjectCreator
|
||||
client: this.client,
|
||||
verbose: this.options.verbose,
|
||||
packageManager: this.packageManager,
|
||||
version: this.options.version,
|
||||
})
|
||||
} finally {
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user