|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { existsSync, mkdirSync } from 'node:fs'; |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const isWindows = process.platform === 'win32'; |
| 8 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..'); |
| 9 | +const cliDistDir = path.join(repoRoot, 'packages', 'cli', 'dist'); |
| 10 | +const cliBinPath = path.join(cliDistDir, 'bin.js'); |
| 11 | +const testCliPath = path.join(repoRoot, 'packages', 'test', 'dist', 'cli.js'); |
| 12 | +const localVpPath = path.join(repoRoot, 'target', 'debug', isWindows ? 'vp.exe' : 'vp'); |
| 13 | +const localVpBinDir = path.dirname(localVpPath); |
| 14 | +const viteRepoDir = path.join(repoRoot, 'vite'); |
| 15 | +const legacyViteRepoDir = path.join(repoRoot, 'rolldown-vite'); |
| 16 | +const toolBinPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'bin.js'); |
| 17 | +const buildHint = 'pnpm build:cli'; |
| 18 | +const pnpmExecPath = process.env.npm_execpath; |
| 19 | +const pnpmBin = isWindows ? 'pnpm.cmd' : 'pnpm'; |
| 20 | +const cargoBin = isWindows ? 'cargo.exe' : 'cargo'; |
| 21 | +const requireFromRolldown = createRequire( |
| 22 | + path.join(repoRoot, 'rolldown', 'packages', 'rolldown', 'package.json'), |
| 23 | +); |
| 24 | + |
| 25 | +type CommandOptions = { |
| 26 | + cwd?: string; |
| 27 | + env?: NodeJS.ProcessEnv; |
| 28 | + hint?: string; |
| 29 | +}; |
| 30 | + |
| 31 | +function failMissing(pathname: string, description: string): never { |
| 32 | + console.error(`Missing ${description}: ${pathname}`); |
| 33 | + console.error(`Run "${buildHint}" first.`); |
| 34 | + process.exit(1); |
| 35 | +} |
| 36 | + |
| 37 | +function ensureLocalCliReady(options?: { needsTestCli?: boolean }) { |
| 38 | + if (!existsSync(cliBinPath)) { |
| 39 | + failMissing(cliBinPath, 'local CLI bundle'); |
| 40 | + } |
| 41 | + if (!existsSync(localVpPath)) { |
| 42 | + failMissing(localVpPath, 'local debug vp binary'); |
| 43 | + } |
| 44 | + if (options?.needsTestCli && !existsSync(testCliPath)) { |
| 45 | + failMissing(testCliPath, 'local test CLI bundle'); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function localCliEnv(): NodeJS.ProcessEnv { |
| 50 | + return { |
| 51 | + ...process.env, |
| 52 | + VITE_GLOBAL_CLI_JS_SCRIPTS_DIR: cliDistDir, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function rolldownBindingCandidates() { |
| 57 | + switch (process.platform) { |
| 58 | + case 'android': |
| 59 | + if (process.arch === 'arm64') return ['@rolldown/binding-android-arm64/package.json']; |
| 60 | + if (process.arch === 'arm') return ['@rolldown/binding-android-arm-eabi/package.json']; |
| 61 | + return []; |
| 62 | + case 'darwin': |
| 63 | + if (process.arch === 'arm64') { |
| 64 | + return [ |
| 65 | + '@rolldown/binding-darwin-universal/package.json', |
| 66 | + '@rolldown/binding-darwin-arm64/package.json', |
| 67 | + ]; |
| 68 | + } |
| 69 | + if (process.arch === 'x64') { |
| 70 | + return [ |
| 71 | + '@rolldown/binding-darwin-universal/package.json', |
| 72 | + '@rolldown/binding-darwin-x64/package.json', |
| 73 | + ]; |
| 74 | + } |
| 75 | + return []; |
| 76 | + case 'freebsd': |
| 77 | + if (process.arch === 'arm64') return ['@rolldown/binding-freebsd-arm64/package.json']; |
| 78 | + if (process.arch === 'x64') return ['@rolldown/binding-freebsd-x64/package.json']; |
| 79 | + return []; |
| 80 | + case 'linux': |
| 81 | + if (process.arch === 'arm') { |
| 82 | + return [ |
| 83 | + '@rolldown/binding-linux-arm-gnueabihf/package.json', |
| 84 | + '@rolldown/binding-linux-arm-musleabihf/package.json', |
| 85 | + ]; |
| 86 | + } |
| 87 | + if (process.arch === 'arm64') { |
| 88 | + return [ |
| 89 | + '@rolldown/binding-linux-arm64-gnu/package.json', |
| 90 | + '@rolldown/binding-linux-arm64-musl/package.json', |
| 91 | + ]; |
| 92 | + } |
| 93 | + if (process.arch === 'loong64') { |
| 94 | + return [ |
| 95 | + '@rolldown/binding-linux-loong64-gnu/package.json', |
| 96 | + '@rolldown/binding-linux-loong64-musl/package.json', |
| 97 | + ]; |
| 98 | + } |
| 99 | + if (process.arch === 'ppc64') return ['@rolldown/binding-linux-ppc64-gnu/package.json']; |
| 100 | + if (process.arch === 'riscv64') { |
| 101 | + return [ |
| 102 | + '@rolldown/binding-linux-riscv64-gnu/package.json', |
| 103 | + '@rolldown/binding-linux-riscv64-musl/package.json', |
| 104 | + ]; |
| 105 | + } |
| 106 | + if (process.arch === 's390x') return ['@rolldown/binding-linux-s390x-gnu/package.json']; |
| 107 | + if (process.arch === 'x64') { |
| 108 | + return [ |
| 109 | + '@rolldown/binding-linux-x64-gnu/package.json', |
| 110 | + '@rolldown/binding-linux-x64-musl/package.json', |
| 111 | + ]; |
| 112 | + } |
| 113 | + return []; |
| 114 | + case 'win32': |
| 115 | + if (process.arch === 'arm64') return ['@rolldown/binding-win32-arm64-msvc/package.json']; |
| 116 | + if (process.arch === 'ia32') return ['@rolldown/binding-win32-ia32-msvc/package.json']; |
| 117 | + if (process.arch === 'x64') { |
| 118 | + return [ |
| 119 | + '@rolldown/binding-win32-x64-msvc/package.json', |
| 120 | + '@rolldown/binding-win32-x64-gnu/package.json', |
| 121 | + ]; |
| 122 | + } |
| 123 | + return []; |
| 124 | + default: |
| 125 | + return []; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function ensureBuildWorkspaceReady() { |
| 130 | + if (!existsSync(viteRepoDir)) { |
| 131 | + console.error(`Missing local vite checkout: ${viteRepoDir}`); |
| 132 | + if (existsSync(legacyViteRepoDir)) { |
| 133 | + console.error( |
| 134 | + `Found legacy checkout at ${legacyViteRepoDir}. This repo now expects the upstream Vite checkout at ./vite.`, |
| 135 | + ); |
| 136 | + console.error( |
| 137 | + 'Run "node packages/tools/src/index.ts sync-remote" to recreate the canonical layout.', |
| 138 | + ); |
| 139 | + } else { |
| 140 | + console.error( |
| 141 | + 'Run "node packages/tools/src/index.ts sync-remote" to fetch the local upstream checkouts required for development.', |
| 142 | + ); |
| 143 | + } |
| 144 | + process.exit(1); |
| 145 | + } |
| 146 | + |
| 147 | + const candidates = rolldownBindingCandidates(); |
| 148 | + if (candidates.length === 0) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + for (const candidate of candidates) { |
| 153 | + try { |
| 154 | + requireFromRolldown.resolve(candidate); |
| 155 | + return; |
| 156 | + } catch { |
| 157 | + continue; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + console.error('Missing local rolldown native binding dependency.'); |
| 162 | + console.error('Run "pnpm install" from the repo root to install workspace optional dependencies.'); |
| 163 | + console.error('If your environment cannot download the prebuilt binding, install "cmake" to build rolldown from source.'); |
| 164 | + process.exit(1); |
| 165 | +} |
| 166 | + |
| 167 | +function runCommand(step: string, command: string, args: string[], options: CommandOptions = {}) { |
| 168 | + const result = spawnSync(command, args, { |
| 169 | + cwd: options.cwd ?? repoRoot, |
| 170 | + env: options.env ?? process.env, |
| 171 | + stdio: 'inherit', |
| 172 | + }); |
| 173 | + |
| 174 | + if (!result.error && result.status === 0) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + console.error(`\n${step} failed.`); |
| 179 | + if (result.error) { |
| 180 | + console.error(result.error.message); |
| 181 | + } |
| 182 | + if (options.hint) { |
| 183 | + console.error(options.hint); |
| 184 | + } |
| 185 | + process.exit(result.status ?? 1); |
| 186 | +} |
| 187 | + |
| 188 | +function runPnpmCommand(step: string, args: string[], options: CommandOptions = {}) { |
| 189 | + const baseArgs = pnpmExecPath ? [pnpmExecPath] : []; |
| 190 | + const command = pnpmExecPath ? process.execPath : pnpmBin; |
| 191 | + |
| 192 | + runCommand(step, command, [...baseArgs, ...args], options); |
| 193 | +} |
| 194 | + |
| 195 | +function exitWith(result: ReturnType<typeof spawnSync>): never { |
| 196 | + if (result.error) { |
| 197 | + console.error(result.error.message); |
| 198 | + process.exit(1); |
| 199 | + } |
| 200 | + process.exit(result.status ?? 1); |
| 201 | +} |
| 202 | + |
| 203 | +export function runLocalCli(args: string[]) { |
| 204 | + ensureLocalCliReady({ needsTestCli: args[0] === 'test' }); |
| 205 | + |
| 206 | + const result = spawnSync(localVpPath, args, { |
| 207 | + cwd: process.cwd(), |
| 208 | + env: localCliEnv(), |
| 209 | + stdio: 'inherit', |
| 210 | + }); |
| 211 | + exitWith(result); |
| 212 | +} |
| 213 | + |
| 214 | +export function runLocalGlobalSnapTest(args: string[]) { |
| 215 | + ensureLocalCliReady(); |
| 216 | + |
| 217 | + const result = spawnSync( |
| 218 | + process.execPath, |
| 219 | + [toolBinPath, 'snap-test', '--dir', 'snap-tests-global', '--bin-dir', localVpBinDir, ...args], |
| 220 | + { |
| 221 | + cwd: process.cwd(), |
| 222 | + env: localCliEnv(), |
| 223 | + stdio: 'inherit', |
| 224 | + }, |
| 225 | + ); |
| 226 | + exitWith(result); |
| 227 | +} |
| 228 | + |
| 229 | +export function runBuildLocalCli(args: string[]) { |
| 230 | + const releaseRust = args.includes('--release-rust'); |
| 231 | + const localBuildEnv = { |
| 232 | + ...process.env, |
| 233 | + VITE_PLUS_CLI_DEBUG: '1', |
| 234 | + }; |
| 235 | + |
| 236 | + mkdirSync(path.join(repoRoot, 'tmp'), { recursive: true }); |
| 237 | + ensureBuildWorkspaceReady(); |
| 238 | + |
| 239 | + runPnpmCommand('Build @rolldown/pluginutils', ['--filter', '@rolldown/pluginutils', 'build']); |
| 240 | + runPnpmCommand('Build rolldown JS glue', ['--filter', 'rolldown', 'build-node'], { |
| 241 | + hint: 'If this fails with a missing rolldown native binding, rerun "pnpm install". If the error mentions "cmake", install cmake to build rolldown from source.', |
| 242 | + }); |
| 243 | + runPnpmCommand('Build vite rolled-up types', ['-C', 'vite', '--filter', 'vite', 'build-types-roll'], { |
| 244 | + hint: 'If this fails because vite dependencies are missing, rerun "pnpm install" from the repo root.', |
| 245 | + }); |
| 246 | + runPnpmCommand('Type-check vite declarations', ['-C', 'vite', '--filter', 'vite', 'build-types-check'], { |
| 247 | + hint: 'If this fails because vite dependencies are missing, rerun "pnpm install" from the repo root.', |
| 248 | + }); |
| 249 | + runPnpmCommand('Build vite-plus core', ['--filter', '@voidzero-dev/vite-plus-core', 'build']); |
| 250 | + runPnpmCommand('Build vite-plus test', ['--filter', '@voidzero-dev/vite-plus-test', 'build']); |
| 251 | + runPnpmCommand('Build vite-plus prompts', ['--filter', '@voidzero-dev/vite-plus-prompts', 'build']); |
| 252 | + runPnpmCommand('Build vite-plus CLI', ['--filter', 'vite-plus', 'build'], { |
| 253 | + env: releaseRust ? process.env : localBuildEnv, |
| 254 | + }); |
| 255 | + runCommand( |
| 256 | + 'Build Rust CLI binaries', |
| 257 | + cargoBin, |
| 258 | + ['build', '-p', 'vite_global_cli', '-p', 'vite_trampoline', ...(releaseRust ? ['--release'] : [])], |
| 259 | + ); |
| 260 | +} |
0 commit comments