Skip to content

Commit 66d0448

Browse files
committed
chore: fix publish package version
1 parent 5969be2 commit 66d0448

File tree

7 files changed

+265
-4
lines changed

7 files changed

+265
-4
lines changed

.changeset/tall-taxes-change.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@dojoengine/core": patch
3+
"@dojoengine/create-burner": patch
4+
"@dojoengine/create-dojo": patch
5+
"@dojoengine/grpc": patch
6+
"@dojoengine/internal": patch
7+
"@dojoengine/predeployed-connector": patch
8+
"@dojoengine/react": patch
9+
"@dojoengine/sdk": patch
10+
"@dojoengine/state": patch
11+
"@dojoengine/torii-client": patch
12+
"@dojoengine/torii-wasm": patch
13+
"@dojoengine/utils": patch
14+
"@dojoengine/utils-wasm": patch
15+
---
16+
17+
chore: fix publish workspace package version

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ lerna-debug.log
1212
**/**/vite.config.ts.timestamp-*
1313
**/**/tsup.config.bundled*
1414

15+
.package-backups

.husky/commit-msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
npx --no -- commitlint --edit ${1}
1+
bunx --no -- commitlint --edit ${1}

commitlint.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "dojo.js",
3+
"type": "module",
34
"packageManager": "bun@1.2.19",
45
"workspaces": {
56
"packages": [
@@ -36,8 +37,10 @@
3637
"lint:check": "turbo lint:check",
3738
"changeset": "changeset",
3839
"version": "changeset version",
39-
"release": "bun run build:deps && changeset publish",
40-
"release:dry-run": "bun run build:deps && changeset publish --dry-run",
40+
"prepare:publish": "bun scripts/prepare-publish.ts",
41+
"restore:packages": "bun scripts/restore-packages.ts",
42+
"release": "bun run build:deps && bun run prepare:publish && changeset publish && bun run restore:packages --cleanup",
43+
"release:dry-run": "bun run build:deps && bun run prepare:publish && changeset publish --dry-run && bun run restore:packages --cleanup",
4144
"dev": "turbo watch dev --concurrency 20",
4245
"dev:deps": "turbo watch build:deps --concurrency 20",
4346
"docs": "turbo run docs --ui stream && typedoc --entryPointStrategy merge 'docs-json/*.json'",
@@ -61,5 +64,10 @@
6164
"dependencies": {
6265
"@changesets/cli": "^2.29.5",
6366
"@dojoengine/recs": "2.0.13"
67+
},
68+
"commitlint": {
69+
"extends": [
70+
"@commitlint/config-conventional"
71+
]
6472
}
6573
}

scripts/prepare-publish.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env bun
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
6+
const rootDir = path.resolve(import.meta.dir, '..');
7+
const BACKUP_DIR = path.join(rootDir, '.package-backups');
8+
9+
interface PackageJson {
10+
name: string;
11+
version: string;
12+
dependencies?: Record<string, string>;
13+
devDependencies?: Record<string, string>;
14+
peerDependencies?: Record<string, string>;
15+
optionalDependencies?: Record<string, string>;
16+
}
17+
18+
interface WorkspacePackageInfo {
19+
version: string;
20+
path: string;
21+
}
22+
23+
interface RootPackageJson extends PackageJson {
24+
workspaces?: {
25+
packages?: string[];
26+
catalog?: Record<string, string>;
27+
};
28+
}
29+
30+
function readJSON<T = any>(filePath: string): T {
31+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
32+
}
33+
34+
function writeJSON(filePath: string, data: any): void {
35+
fs.writeFileSync(filePath, JSON.stringify(data, null, 4) + '\n');
36+
}
37+
38+
function getWorkspacePackages(): Map<string, WorkspacePackageInfo> {
39+
const packages = new Map<string, WorkspacePackageInfo>();
40+
const packagesDir = path.join(rootDir, 'packages');
41+
42+
const dirs = fs.readdirSync(packagesDir)
43+
.filter(dir => fs.statSync(path.join(packagesDir, dir)).isDirectory());
44+
45+
for (const dir of dirs) {
46+
const pkgPath = path.join(packagesDir, dir, 'package.json');
47+
if (fs.existsSync(pkgPath)) {
48+
const pkg = readJSON<PackageJson>(pkgPath);
49+
packages.set(pkg.name, {
50+
version: pkg.version,
51+
path: pkgPath
52+
});
53+
}
54+
}
55+
56+
return packages;
57+
}
58+
59+
function getCatalogVersions(): Record<string, string> {
60+
const rootPkg = readJSON<RootPackageJson>(path.join(rootDir, 'package.json'));
61+
return rootPkg.workspaces?.catalog || {};
62+
}
63+
64+
function resolveWorkspaceVersion(depName: string, workspacePackages: Map<string, WorkspacePackageInfo>): string | null {
65+
const pkg = workspacePackages.get(depName);
66+
return pkg ? pkg.version : null;
67+
}
68+
69+
function resolveCatalogVersion(depName: string, catalogVersions: Record<string, string>): string | null {
70+
return catalogVersions[depName] || null;
71+
}
72+
73+
function backupPackageJson(filePath: string): void {
74+
if (!fs.existsSync(BACKUP_DIR)) {
75+
fs.mkdirSync(BACKUP_DIR, { recursive: true });
76+
}
77+
78+
const relativePath = path.relative(rootDir, filePath);
79+
const backupPath = path.join(BACKUP_DIR, relativePath);
80+
const backupDir = path.dirname(backupPath);
81+
82+
if (!fs.existsSync(backupDir)) {
83+
fs.mkdirSync(backupDir, { recursive: true });
84+
}
85+
86+
fs.copyFileSync(filePath, backupPath);
87+
}
88+
89+
function processPackageJson(
90+
pkgPath: string,
91+
workspacePackages: Map<string, WorkspacePackageInfo>,
92+
catalogVersions: Record<string, string>
93+
): boolean {
94+
const pkg = readJSON<PackageJson>(pkgPath);
95+
let modified = false;
96+
97+
backupPackageJson(pkgPath);
98+
99+
const processDeps = (deps?: Record<string, string>) => {
100+
if (!deps) return;
101+
102+
for (const [name, version] of Object.entries(deps)) {
103+
if (version === 'workspace:*') {
104+
const resolvedVersion = resolveWorkspaceVersion(name, workspacePackages);
105+
if (resolvedVersion) {
106+
deps[name] = `^${resolvedVersion}`;
107+
modified = true;
108+
console.log(` ✓ Resolved ${name}: workspace:* → ^${resolvedVersion}`);
109+
} else {
110+
console.warn(` ⚠ Could not resolve workspace version for ${name}`);
111+
}
112+
} else if (version === 'catalog:') {
113+
const resolvedVersion = resolveCatalogVersion(name, catalogVersions);
114+
if (resolvedVersion) {
115+
deps[name] = resolvedVersion;
116+
modified = true;
117+
console.log(` ✓ Resolved ${name}: catalog: → ${resolvedVersion}`);
118+
} else {
119+
console.warn(` ⚠ Could not resolve catalog version for ${name}`);
120+
}
121+
}
122+
}
123+
};
124+
125+
processDeps(pkg.dependencies);
126+
processDeps(pkg.devDependencies);
127+
processDeps(pkg.peerDependencies);
128+
processDeps(pkg.optionalDependencies);
129+
130+
if (modified) {
131+
writeJSON(pkgPath, pkg);
132+
console.log(` ✅ Updated ${path.relative(rootDir, pkgPath)}`);
133+
}
134+
135+
return modified;
136+
}
137+
138+
function main(): void {
139+
console.log('🔧 Preparing packages for publishing...\n');
140+
141+
const workspacePackages = getWorkspacePackages();
142+
const catalogVersions = getCatalogVersions();
143+
144+
console.log(`📦 Found ${workspacePackages.size} workspace packages`);
145+
console.log(`📚 Found ${Object.keys(catalogVersions).length} catalog entries\n`);
146+
147+
let packagesModified = 0;
148+
149+
for (const [name, info] of workspacePackages) {
150+
console.log(`Processing ${name}...`);
151+
if (processPackageJson(info.path, workspacePackages, catalogVersions)) {
152+
packagesModified++;
153+
}
154+
console.log('');
155+
}
156+
157+
console.log(`✅ Prepared ${packagesModified} packages for publishing`);
158+
console.log(`📁 Backups saved to ${path.relative(rootDir, BACKUP_DIR)}`);
159+
}
160+
161+
try {
162+
main();
163+
} catch (error) {
164+
console.error('❌ Error preparing packages:', error instanceof Error ? error.message : error);
165+
process.exit(1);
166+
}

scripts/restore-packages.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bun
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
6+
const rootDir = path.resolve(import.meta.dir, '..');
7+
const BACKUP_DIR = path.join(rootDir, '.package-backups');
8+
9+
function restorePackages(): void {
10+
if (!fs.existsSync(BACKUP_DIR)) {
11+
console.log('ℹ️ No backup directory found. Nothing to restore.');
12+
return;
13+
}
14+
15+
console.log('🔄 Restoring original package.json files...\n');
16+
17+
let restoredCount = 0;
18+
19+
function restoreFiles(dir: string, baseBackupPath: string = BACKUP_DIR): void {
20+
const entries = fs.readdirSync(dir, { withFileTypes: true });
21+
22+
for (const entry of entries) {
23+
const backupPath = path.join(dir, entry.name);
24+
25+
if (entry.isDirectory()) {
26+
restoreFiles(backupPath, baseBackupPath);
27+
} else if (entry.name === 'package.json') {
28+
const relativePath = path.relative(baseBackupPath, backupPath);
29+
const originalPath = path.join(rootDir, relativePath);
30+
31+
if (fs.existsSync(originalPath)) {
32+
fs.copyFileSync(backupPath, originalPath);
33+
console.log(` ✓ Restored ${relativePath}`);
34+
restoredCount++;
35+
}
36+
}
37+
}
38+
}
39+
40+
restoreFiles(BACKUP_DIR);
41+
42+
console.log(`\n✅ Restored ${restoredCount} package.json files`);
43+
}
44+
45+
function cleanupBackups(): void {
46+
if (fs.existsSync(BACKUP_DIR)) {
47+
fs.rmSync(BACKUP_DIR, { recursive: true, force: true });
48+
console.log(`🧹 Cleaned up backup directory`);
49+
}
50+
}
51+
52+
function main(): void {
53+
const args = process.argv.slice(2);
54+
const shouldCleanup = args.includes('--cleanup');
55+
56+
try {
57+
restorePackages();
58+
59+
if (shouldCleanup) {
60+
cleanupBackups();
61+
} else {
62+
console.log('\nℹ️ Run with --cleanup flag to remove backup directory');
63+
}
64+
} catch (error) {
65+
console.error('❌ Error restoring packages:', error instanceof Error ? error.message : error);
66+
process.exit(1);
67+
}
68+
}
69+
70+
main();

0 commit comments

Comments
 (0)