|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Platform binary build orchestration script. |
| 5 | + * Builds all or specific platform binaries in parallel or sequence. |
| 6 | + */ |
| 7 | + |
| 8 | +import { spawn } from 'node:child_process' |
| 9 | +import process from 'node:process' |
| 10 | + |
| 11 | +const PLATFORMS = [ |
| 12 | + 'alpine-arm64', |
| 13 | + 'alpine-x64', |
| 14 | + 'darwin-arm64', |
| 15 | + 'darwin-x64', |
| 16 | + 'linux-arm64', |
| 17 | + 'linux-x64', |
| 18 | + 'win32-arm64', |
| 19 | + 'win32-x64' |
| 20 | +] |
| 21 | + |
| 22 | +const args = process.argv.slice(2) |
| 23 | +let parallel = false |
| 24 | +let platforms = [] |
| 25 | +const buildFlags = [] |
| 26 | + |
| 27 | +for (let i = 0; i < args.length; i++) { |
| 28 | + const arg = args[i] |
| 29 | + if (arg === '--parallel') { |
| 30 | + parallel = true |
| 31 | + } else if (arg === '--platforms') { |
| 32 | + const platformsArg = args[++i] |
| 33 | + platforms = platformsArg.split(',').map(p => p.trim()) |
| 34 | + } else { |
| 35 | + buildFlags.push(arg) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +if (!platforms.length) { |
| 40 | + platforms = PLATFORMS |
| 41 | +} |
| 42 | + |
| 43 | +// Validate platforms. |
| 44 | +for (const platform of platforms) { |
| 45 | + if (!PLATFORMS.includes(platform)) { |
| 46 | + console.error(`Unknown platform: ${platform}`) |
| 47 | + console.error(`Available platforms: ${PLATFORMS.join(', ')}`) |
| 48 | + process.exit(1) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +console.log(`Building ${platforms.length} platform(s): ${platforms.join(', ')}`) |
| 53 | +console.log(`Mode: ${parallel ? 'parallel' : 'sequential'}`) |
| 54 | +if (buildFlags.length) { |
| 55 | + console.log(`Build flags: ${buildFlags.join(' ')}`) |
| 56 | +} |
| 57 | +console.log('') |
| 58 | + |
| 59 | +/** |
| 60 | + * Build a single platform. |
| 61 | + */ |
| 62 | +function buildPlatform(platform) { |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + const startTime = Date.now() |
| 65 | + console.log(`[${platform}] Starting build...`) |
| 66 | + |
| 67 | + const pnpmArgs = ['run', 'build', '--', '--target', platform, ...buildFlags] |
| 68 | + const child = spawn('pnpm', pnpmArgs, { |
| 69 | + encoding: 'utf8', |
| 70 | + shell: false, |
| 71 | + stdio: 'pipe' |
| 72 | + }) |
| 73 | + |
| 74 | + let stdout = '' |
| 75 | + let stderr = '' |
| 76 | + |
| 77 | + child.stdout?.on('data', data => { |
| 78 | + stdout += data |
| 79 | + }) |
| 80 | + |
| 81 | + child.stderr?.on('data', data => { |
| 82 | + stderr += data |
| 83 | + }) |
| 84 | + |
| 85 | + child.on('close', code => { |
| 86 | + const duration = ((Date.now() - startTime) / 1000).toFixed(2) |
| 87 | + |
| 88 | + if (code === 0) { |
| 89 | + console.log(`[${platform}] ✓ Build succeeded (${duration}s)`) |
| 90 | + resolve({ platform, code, duration, stdout, stderr }) |
| 91 | + } else { |
| 92 | + console.error(`[${platform}] ✗ Build failed (${duration}s)`) |
| 93 | + if (stderr) { |
| 94 | + console.error(`[${platform}] Error output:`) |
| 95 | + console.error(stderr) |
| 96 | + } |
| 97 | + reject(new Error(`Platform ${platform} build failed with code ${code}`)) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + child.on('error', error => { |
| 102 | + console.error(`[${platform}] ✗ Build error:`, error) |
| 103 | + reject(error) |
| 104 | + }) |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Build platforms sequentially. |
| 110 | + */ |
| 111 | +async function buildSequential() { |
| 112 | + const results = [] |
| 113 | + for (const platform of platforms) { |
| 114 | + try { |
| 115 | + const result = await buildPlatform(platform) |
| 116 | + results.push(result) |
| 117 | + } catch (error) { |
| 118 | + console.error(`\nBuild failed at platform: ${platform}`) |
| 119 | + process.exit(1) |
| 120 | + } |
| 121 | + } |
| 122 | + return results |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Build platforms in parallel. |
| 127 | + */ |
| 128 | +async function buildParallel() { |
| 129 | + const promises = platforms.map(platform => buildPlatform(platform)) |
| 130 | + try { |
| 131 | + return await Promise.all(promises) |
| 132 | + } catch (error) { |
| 133 | + console.error('\nOne or more platform builds failed') |
| 134 | + process.exit(1) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// Execute builds. |
| 139 | +const buildFn = parallel ? buildParallel : buildSequential |
| 140 | +const startTime = Date.now() |
| 141 | + |
| 142 | +buildFn() |
| 143 | + .then(results => { |
| 144 | + const totalDuration = ((Date.now() - startTime) / 1000).toFixed(2) |
| 145 | + console.log(`\n✓ All ${results.length} platform(s) built successfully in ${totalDuration}s`) |
| 146 | + process.exit(0) |
| 147 | + }) |
| 148 | + .catch(error => { |
| 149 | + console.error('\nBuild orchestration failed:', error.message) |
| 150 | + process.exit(1) |
| 151 | + }) |
0 commit comments