Skip to content

Commit

Permalink
fix: create-dojo update catalog versions
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Dec 30, 2024
1 parent 8353009 commit 33821e5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/create-dojo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"cross-spawn": "^7.0.3",
"fs-extra": "^11.2.0",
"prompts": "^2.4.2",
"type-fest": "^4.26.1"
"type-fest": "^4.26.1",
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.9"
},
"devDependencies": {
"@types/cross-spawn": "^6.0.6",
Expand Down
57 changes: 49 additions & 8 deletions packages/create-dojo/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { promises as fs } from "fs";
import spawn from "cross-spawn";
import https from "https";
import { input, select } from "@inquirer/prompts";
import { getCatalogVersions } from "../utils/get-package-info";

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

packageJson.name = projectName;

for (let dep of Object.keys(packageJson.dependencies)) {
if (
dep.startsWith("@dojoengine") &&
packageJson.dependencies[dep].startsWith("workspace:")
) {
packageJson.dependencies[dep] = latestVersion;
const updatedPackageJson = {
...packageJson,
dependencies: updateDependencies(
packageJson.dependencies,
latestVersion,
catalogVersion
),
devDependencies: updateDependencies(
packageJson.devDependencies,
latestVersion,
catalogVersion
),
peerDependencies: updateDependencies(
packageJson.peerDependencies,
latestVersion,
catalogVersion
),
};

await fs.writeFile(
packageJsonPath,
JSON.stringify(updatedPackageJson, null, 2)
);
}

function updateDependencies(
deps: Record<string, string> | undefined,
latestVersion: string,
catalogVersions: Record<string, string>
) {
if (!deps) return deps;

const updated = { ...deps };
for (const [name, version] of Object.entries(deps)) {
if (version.startsWith("workspace:")) {
// Find the actual version from root package.json
updated[name] = latestVersion;
} else if (version.startsWith("catalog:")) {
// Find the version from catalog
const actualVersion = catalogVersions[name];
if (!actualVersion) {
throw new Error(
`failed to get version for package ${name} from catalog`
);
}
updated[name] = actualVersion;
}
}

await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
return updated;
}

async function rewriteDojoConfigFile(clientPath: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-dojo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { start } from "./commands/start";

import { Command } from "commander";

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

process.on("SIGINT", () => process.exit(0));
process.on("SIGTERM", () => process.exit(0));
Expand Down
23 changes: 23 additions & 0 deletions packages/create-dojo/src/utils/get-package-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import path from "path";
import fs from "fs-extra";
import { type PackageJson } from "type-fest";
import yaml from "js-yaml";
import fetch from "node-fetch";

interface WorkspaceYaml {
catalog: Record<string, string>;
}

export async function getCatalogVersions(): Promise<Record<string, string>> {
const WORKSPACE_YAML_URL =
"https://raw.githubusercontent.com/dojoengine/dojo.js/refs/heads/main/pnpm-workspace.yaml";

try {
// Fetch workspace yaml to get package paths
const yamlResponse = await fetch(WORKSPACE_YAML_URL);
const yamlContent = await yamlResponse.text();
const workspaceConfig = yaml.load(yamlContent) as WorkspaceYaml;

return workspaceConfig.catalog;
} catch (error) {
console.warn("Failed to fetch catalog versions:", error);
return {};
}
}

export function getPackageInfo() {
const packageJsonPath = path.join(process.cwd(), "package.json");
Expand Down
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33821e5

Please sign in to comment.