|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const chalk = require('chalk'); |
| 7 | + |
| 8 | +// Package build order (dependencies first) |
| 9 | +const packages = [ |
| 10 | + { |
| 11 | + name: '@fecommunity/reactpress-config', |
| 12 | + path: 'config', |
| 13 | + description: 'Configuration management package' |
| 14 | + }, |
| 15 | + { |
| 16 | + name: '@fecommunity/reactpress-server', |
| 17 | + path: 'server', |
| 18 | + description: 'Backend API server package' |
| 19 | + }, |
| 20 | + { |
| 21 | + name: '@fecommunity/reactpress-client', |
| 22 | + path: 'client', |
| 23 | + description: 'Frontend application package' |
| 24 | + } |
| 25 | +]; |
| 26 | + |
| 27 | +// Get current versions |
| 28 | +function getCurrentVersion(packagePath) { |
| 29 | + try { |
| 30 | + const pkgPath = path.join(process.cwd(), packagePath, 'package.json'); |
| 31 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 32 | + return pkg.version; |
| 33 | + } catch (error) { |
| 34 | + return 'unknown'; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Fix workspace dependencies for build |
| 39 | +function fixWorkspaceDependenciesForBuild(packagePath) { |
| 40 | + console.log(chalk.blue(`🔧 Fixing workspace dependencies for build: ${packagePath}...`)); |
| 41 | + |
| 42 | + const pkgPath = path.join(process.cwd(), packagePath, 'package.json'); |
| 43 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 44 | + |
| 45 | + // Fix dependencies |
| 46 | + const depTypes = ['dependencies', 'devDependencies', 'peerDependencies']; |
| 47 | + |
| 48 | + depTypes.forEach(depType => { |
| 49 | + if (pkg[depType]) { |
| 50 | + Object.keys(pkg[depType]).forEach(depName => { |
| 51 | + // Check if it's a workspace dependency |
| 52 | + if (pkg[depType][depName] === 'workspace:*' || pkg[depType][depName].startsWith('workspace:')) { |
| 53 | + // For build purposes, we'll use the file: protocol to reference local packages |
| 54 | + const depPackage = packages.find(p => p.name === depName); |
| 55 | + if (depPackage) { |
| 56 | + console.log(chalk.gray(` Replacing ${depName} workspace dependency with file reference`)); |
| 57 | + pkg[depType][depName] = `file:../${depPackage.path}`; |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + // Write the updated package.json |
| 65 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 66 | + console.log(chalk.green(`✅ Workspace dependencies fixed for build: ${packagePath}`)); |
| 67 | +} |
| 68 | + |
| 69 | +// Restore workspace dependencies after build |
| 70 | +function restoreWorkspaceDependenciesAfterBuild(packagePath) { |
| 71 | + console.log(chalk.blue(`🔄 Restoring workspace dependencies after build: ${packagePath}...`)); |
| 72 | + |
| 73 | + const pkgPath = path.join(process.cwd(), packagePath, 'package.json'); |
| 74 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 75 | + |
| 76 | + // Restore dependencies |
| 77 | + const depTypes = ['dependencies', 'devDependencies', 'peerDependencies']; |
| 78 | + |
| 79 | + depTypes.forEach(depType => { |
| 80 | + if (pkg[depType]) { |
| 81 | + Object.keys(pkg[depType]).forEach(depName => { |
| 82 | + // Check if this is one of our internal packages referenced with file: |
| 83 | + const depPackage = packages.find(p => p.name === depName); |
| 84 | + if (depPackage && pkg[depType][depName].startsWith('file:')) { |
| 85 | + console.log(chalk.gray(` Restoring ${depName} to workspace dependency`)); |
| 86 | + pkg[depType][depName] = 'workspace:*'; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + // Write the updated package.json |
| 93 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 94 | + console.log(chalk.green(`✅ Workspace dependencies restored after build: ${packagePath}`)); |
| 95 | +} |
| 96 | + |
| 97 | +// Build package |
| 98 | +function buildPackage(pkg) { |
| 99 | + console.log(chalk.blue(`\n🔨 Building ${pkg.name} (${pkg.description})...`)); |
| 100 | + |
| 101 | + try { |
| 102 | + // Fix workspace dependencies for build |
| 103 | + fixWorkspaceDependenciesForBuild(pkg.path); |
| 104 | + |
| 105 | + try { |
| 106 | + if (pkg.path === 'config') { |
| 107 | + execSync('pnpm run build', { cwd: path.join(process.cwd(), pkg.path), stdio: 'inherit' }); |
| 108 | + } else if (pkg.path === 'server') { |
| 109 | + execSync('pnpm run prebuild && pnpm run build', { cwd: path.join(process.cwd(), pkg.path), stdio: 'inherit' }); |
| 110 | + } else if (pkg.path === 'client') { |
| 111 | + execSync('pnpm run prebuild && pnpm run build', { cwd: path.join(process.cwd(), pkg.path), stdio: 'inherit' }); |
| 112 | + } |
| 113 | + console.log(chalk.green(`✅ ${pkg.name} built successfully`)); |
| 114 | + } finally { |
| 115 | + // Always restore workspace dependencies |
| 116 | + restoreWorkspaceDependenciesAfterBuild(pkg.path); |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + console.log(chalk.red(`❌ Failed to build ${pkg.name}`)); |
| 120 | + throw error; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Main function |
| 125 | +async function main() { |
| 126 | + console.log(chalk.blue('🏗️ ReactPress Package Builder\n')); |
| 127 | + |
| 128 | + // Show current versions |
| 129 | + console.log(chalk.cyan('📋 Current package versions:')); |
| 130 | + packages.forEach(pkg => { |
| 131 | + const version = getCurrentVersion(pkg.path); |
| 132 | + console.log(chalk.gray(` ${pkg.name}: ${version}`)); |
| 133 | + }); |
| 134 | + console.log(); |
| 135 | + |
| 136 | + try { |
| 137 | + // Build packages in order |
| 138 | + for (const pkg of packages) { |
| 139 | + if (fs.existsSync(path.join(process.cwd(), pkg.path))) { |
| 140 | + await buildPackage(pkg); |
| 141 | + } else { |
| 142 | + console.log(chalk.yellow(`⚠️ Package ${pkg.name} directory not found, skipping...`)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + console.log(chalk.green('\n🎉 All packages built successfully!')); |
| 147 | + } catch (error) { |
| 148 | + console.error(chalk.red('❌ Build failed:'), error); |
| 149 | + process.exit(1); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Check if called with --publish flag |
| 154 | +if (process.argv.includes('--publish')) { |
| 155 | + // When called with --publish, we only show the header and versions but don't build |
| 156 | + // The publish script will handle building as needed |
| 157 | + console.log(chalk.blue('🏗️ ReactPress Package Builder\n')); |
| 158 | + |
| 159 | + // Show current versions |
| 160 | + console.log(chalk.cyan('📋 Current package versions:')); |
| 161 | + packages.forEach(pkg => { |
| 162 | + const version = getCurrentVersion(pkg.path); |
| 163 | + console.log(chalk.gray(` ${pkg.name}: ${version}`)); |
| 164 | + }); |
| 165 | + console.log(); |
| 166 | + |
| 167 | + // Don't build packages here, just start the publish process |
| 168 | + console.log(chalk.blue('\n🚀 Starting publish process...')); |
| 169 | + // Execute publish script |
| 170 | + const { spawn } = require('child_process'); |
| 171 | + const publishProcess = spawn('node', ['scripts/publish-packages.js', '--release'], { stdio: 'inherit' }); |
| 172 | + |
| 173 | + publishProcess.on('close', (code) => { |
| 174 | + if (code === 0) { |
| 175 | + console.log(chalk.green('\n✅ Build and publish completed successfully!')); |
| 176 | + } else { |
| 177 | + console.log(chalk.red('\n❌ Publish process failed!')); |
| 178 | + process.exit(code); |
| 179 | + } |
| 180 | + }); |
| 181 | +} else { |
| 182 | + main(); |
| 183 | +} |
0 commit comments