Skip to content

Commit 33821e5

Browse files
committed
fix: create-dojo update catalog versions
1 parent 8353009 commit 33821e5

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

packages/create-dojo/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"cross-spawn": "^7.0.3",
2828
"fs-extra": "^11.2.0",
2929
"prompts": "^2.4.2",
30-
"type-fest": "^4.26.1"
30+
"type-fest": "^4.26.1",
31+
"js-yaml": "^4.1.0",
32+
"node-fetch": "^2.6.9"
3133
},
3234
"devDependencies": {
3335
"@types/cross-spawn": "^6.0.6",

packages/create-dojo/src/commands/start.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { promises as fs } from "fs";
44
import spawn from "cross-spawn";
55
import https from "https";
66
import { input, select } from "@inquirer/prompts";
7+
import { getCatalogVersions } from "../utils/get-package-info";
78

89
const templates = [
910
{
@@ -96,19 +97,59 @@ async function rewritePackageJson(projectName: string, clientPath: string) {
9697
const packageJsonPath = path.join(clientPath, "package.json");
9798
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
9899
const latestVersion = await getLatestVersion();
100+
const catalogVersion = await getCatalogVersions();
99101

100102
packageJson.name = projectName;
101103

102-
for (let dep of Object.keys(packageJson.dependencies)) {
103-
if (
104-
dep.startsWith("@dojoengine") &&
105-
packageJson.dependencies[dep].startsWith("workspace:")
106-
) {
107-
packageJson.dependencies[dep] = latestVersion;
104+
const updatedPackageJson = {
105+
...packageJson,
106+
dependencies: updateDependencies(
107+
packageJson.dependencies,
108+
latestVersion,
109+
catalogVersion
110+
),
111+
devDependencies: updateDependencies(
112+
packageJson.devDependencies,
113+
latestVersion,
114+
catalogVersion
115+
),
116+
peerDependencies: updateDependencies(
117+
packageJson.peerDependencies,
118+
latestVersion,
119+
catalogVersion
120+
),
121+
};
122+
123+
await fs.writeFile(
124+
packageJsonPath,
125+
JSON.stringify(updatedPackageJson, null, 2)
126+
);
127+
}
128+
129+
function updateDependencies(
130+
deps: Record<string, string> | undefined,
131+
latestVersion: string,
132+
catalogVersions: Record<string, string>
133+
) {
134+
if (!deps) return deps;
135+
136+
const updated = { ...deps };
137+
for (const [name, version] of Object.entries(deps)) {
138+
if (version.startsWith("workspace:")) {
139+
// Find the actual version from root package.json
140+
updated[name] = latestVersion;
141+
} else if (version.startsWith("catalog:")) {
142+
// Find the version from catalog
143+
const actualVersion = catalogVersions[name];
144+
if (!actualVersion) {
145+
throw new Error(
146+
`failed to get version for package ${name} from catalog`
147+
);
148+
}
149+
updated[name] = actualVersion;
108150
}
109151
}
110-
111-
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
152+
return updated;
112153
}
113154

114155
async function rewriteDojoConfigFile(clientPath: string) {

packages/create-dojo/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { start } from "./commands/start";
33

44
import { Command } from "commander";
55

6-
import { getPackageInfo } from "./utils/get-package-info";
6+
import { getCatalogVersions, getPackageInfo } from "./utils/get-package-info";
77

88
process.on("SIGINT", () => process.exit(0));
99
process.on("SIGTERM", () => process.exit(0));

packages/create-dojo/src/utils/get-package-info.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import path from "path";
22
import fs from "fs-extra";
33
import { type PackageJson } from "type-fest";
4+
import yaml from "js-yaml";
5+
import fetch from "node-fetch";
6+
7+
interface WorkspaceYaml {
8+
catalog: Record<string, string>;
9+
}
10+
11+
export async function getCatalogVersions(): Promise<Record<string, string>> {
12+
const WORKSPACE_YAML_URL =
13+
"https://raw.githubusercontent.com/dojoengine/dojo.js/refs/heads/main/pnpm-workspace.yaml";
14+
15+
try {
16+
// Fetch workspace yaml to get package paths
17+
const yamlResponse = await fetch(WORKSPACE_YAML_URL);
18+
const yamlContent = await yamlResponse.text();
19+
const workspaceConfig = yaml.load(yamlContent) as WorkspaceYaml;
20+
21+
return workspaceConfig.catalog;
22+
} catch (error) {
23+
console.warn("Failed to fetch catalog versions:", error);
24+
return {};
25+
}
26+
}
427

528
export function getPackageInfo() {
629
const packageJsonPath = path.join(process.cwd(), "package.json");

pnpm-lock.yaml

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)