|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Prepare packages for npm publish by replacing workspace:* dependencies with actual versions. |
| 4 | + * Run this script before publishing to ensure no workspace protocol leaks into npm. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * npm run prepare-publish |
| 8 | + * pnpm prepare-publish |
| 9 | + * |
| 10 | + * This script: |
| 11 | + * 1. Finds all workspace:* dependencies in packages |
| 12 | + * 2. Resolves actual versions from local package.json files |
| 13 | + * 3. Creates temporary package.json files with resolved versions |
| 14 | + * 4. After publish, restore original package.json files |
| 15 | + */ |
| 16 | + |
| 17 | +import { readFileSync, writeFileSync, existsSync } from 'fs'; |
| 18 | +import { join, dirname } from 'path'; |
| 19 | +import { fileURLToPath } from 'url'; |
| 20 | + |
| 21 | +const __filename = fileURLToPath(import.meta.url); |
| 22 | +const __dirname = dirname(__filename); |
| 23 | +const ROOT = join(__dirname, '..'); |
| 24 | + |
| 25 | +interface PackageJson { |
| 26 | + name: string; |
| 27 | + version: string; |
| 28 | + dependencies?: Record<string, string>; |
| 29 | + devDependencies?: Record<string, string>; |
| 30 | + peerDependencies?: Record<string, string>; |
| 31 | +} |
| 32 | + |
| 33 | +function readPackageJson(pkgPath: string): PackageJson { |
| 34 | + return JSON.parse(readFileSync(pkgPath, 'utf-8')); |
| 35 | +} |
| 36 | + |
| 37 | +function writePackageJson(pkgPath: string, pkg: PackageJson): void { |
| 38 | + writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 39 | +} |
| 40 | + |
| 41 | +function resolveWorkspaceVersion(depName: string): string | null { |
| 42 | + // Map package names to their paths in the monorepo |
| 43 | + const pkgMap: Record<string, string> = { |
| 44 | + '@leanspec/core': 'packages/core/package.json', |
| 45 | + '@leanspec/ui': 'packages/ui/package.json', |
| 46 | + 'lean-spec': 'packages/cli/package.json', |
| 47 | + }; |
| 48 | + |
| 49 | + const pkgPath = pkgMap[depName]; |
| 50 | + if (!pkgPath) { |
| 51 | + console.warn(`⚠️ Unknown workspace package: ${depName}`); |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const fullPath = join(ROOT, pkgPath); |
| 56 | + if (!existsSync(fullPath)) { |
| 57 | + console.warn(`⚠️ Package not found: ${fullPath}`); |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + const pkg = readPackageJson(fullPath); |
| 62 | + return pkg.version; |
| 63 | +} |
| 64 | + |
| 65 | +function replaceWorkspaceDeps(deps: Record<string, string> | undefined, depType: string): boolean { |
| 66 | + if (!deps) return false; |
| 67 | + |
| 68 | + let changed = false; |
| 69 | + for (const [name, version] of Object.entries(deps)) { |
| 70 | + if (version.startsWith('workspace:')) { |
| 71 | + const resolvedVersion = resolveWorkspaceVersion(name); |
| 72 | + if (resolvedVersion) { |
| 73 | + deps[name] = `^${resolvedVersion}`; |
| 74 | + console.log(` ✓ ${depType}.${name}: workspace:* → ^${resolvedVersion}`); |
| 75 | + changed = true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return changed; |
| 80 | +} |
| 81 | + |
| 82 | +function processPackage(pkgPath: string): boolean { |
| 83 | + const fullPath = join(ROOT, pkgPath); |
| 84 | + if (!existsSync(fullPath)) { |
| 85 | + console.warn(`⚠️ Package not found: ${fullPath}`); |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + const pkg = readPackageJson(fullPath); |
| 90 | + console.log(`\n📦 Processing ${pkg.name}...`); |
| 91 | + |
| 92 | + let changed = false; |
| 93 | + changed = replaceWorkspaceDeps(pkg.dependencies, 'dependencies') || changed; |
| 94 | + changed = replaceWorkspaceDeps(pkg.devDependencies, 'devDependencies') || changed; |
| 95 | + changed = replaceWorkspaceDeps(pkg.peerDependencies, 'peerDependencies') || changed; |
| 96 | + |
| 97 | + if (changed) { |
| 98 | + // Create backup |
| 99 | + const backupPath = fullPath + '.backup'; |
| 100 | + writeFileSync(backupPath, readFileSync(fullPath, 'utf-8')); |
| 101 | + console.log(` 💾 Backup saved to ${pkgPath}.backup`); |
| 102 | + |
| 103 | + // Write updated package.json |
| 104 | + writePackageJson(fullPath, pkg); |
| 105 | + console.log(` ✅ Updated ${pkgPath}`); |
| 106 | + return true; |
| 107 | + } else { |
| 108 | + console.log(` ⏭️ No workspace:* dependencies found`); |
| 109 | + return false; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function main() { |
| 114 | + console.log('🚀 Preparing packages for npm publish...\n'); |
| 115 | + console.log('This will replace workspace:* with actual versions.\n'); |
| 116 | + |
| 117 | + const packages = [ |
| 118 | + 'packages/core/package.json', |
| 119 | + 'packages/cli/package.json', |
| 120 | + 'packages/ui/package.json', |
| 121 | + ]; |
| 122 | + |
| 123 | + const modified: string[] = []; |
| 124 | + for (const pkg of packages) { |
| 125 | + if (processPackage(pkg)) { |
| 126 | + modified.push(pkg); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (modified.length > 0) { |
| 131 | + console.log('\n✅ Preparation complete!'); |
| 132 | + console.log('\nModified packages:'); |
| 133 | + modified.forEach(pkg => console.log(` - ${pkg}`)); |
| 134 | + console.log('\n⚠️ IMPORTANT: After publishing, restore original files:'); |
| 135 | + console.log(' npm run restore-packages'); |
| 136 | + console.log(' OR manually: mv package.json.backup package.json'); |
| 137 | + } else { |
| 138 | + console.log('\n✅ No workspace:* dependencies found. Ready to publish!'); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +main(); |
0 commit comments