|
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
|
): Record<string, any> { |
|
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 (shouldUpdateVersion(dependency)) { |
|
16
|
packageJson.dependencies[dependency] = version |
|
17
|
} |
|
18
|
} |
|
19
|
} |
|
20
|
if (packageJson.devDependencies) { |
|
21
|
for (const dependency of Object.keys(packageJson.devDependencies)) { |
|
22
|
if (shouldUpdateVersion(dependency)) { |
|
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
|
|
|
32
|
return packageJson |
|
33
|
} |
|
34
|
|
|
35
|
function shouldUpdateVersion(dependency: string): boolean { |
|
36
|
// UI package follows different versioning, so we can't update it following |
|
37
|
// the same logic as other Medusa packages |
|
38
|
return dependency.startsWith("@medusajs/") && dependency !== "@medusajs/ui" |
|
39
|
} |