|
| 1 | +#!/usr/bin/env node |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import { execSync } from 'node:child_process'; |
| 4 | +import { readFileSync } from 'node:fs'; |
| 5 | +import path from 'node:path'; |
| 6 | + |
| 7 | +const require = createRequire(import.meta.url); |
| 8 | + |
| 9 | +function log(msg) { |
| 10 | + console.log(`[platform-binaries] ${msg}`); |
| 11 | +} |
| 12 | + |
| 13 | +function getVersionSafe(getter, label) { |
| 14 | + try { |
| 15 | + const v = getter(); |
| 16 | + if (typeof v === 'string' && v.trim()) return v; |
| 17 | + } catch (e) { |
| 18 | + log(`WARN: Unable to resolve version for ${label}: ${e.message}`); |
| 19 | + } |
| 20 | + return ''; |
| 21 | +} |
| 22 | + |
| 23 | +// Detect platform triplet we support |
| 24 | +const isLinux = process.platform === 'linux'; |
| 25 | +const isX64 = process.arch === 'x64'; |
| 26 | +const suffix = isLinux && isX64 ? 'linux-x64-gnu' : ''; |
| 27 | + |
| 28 | +if (!suffix) { |
| 29 | + log('INFO: Non-linux/x64 platform detected; no native platform binaries to install.'); |
| 30 | + process.exit(0); |
| 31 | +} |
| 32 | + |
| 33 | +// Resolve versions from installed/declared packages |
| 34 | +const rollupVer = getVersionSafe(() => require('rollup/package.json').version, 'rollup'); |
| 35 | + |
| 36 | +function resolveLightningCssVersion() { |
| 37 | + // Try npm ls first (works well with workspaces) |
| 38 | + try { |
| 39 | + const out = execSync('npm ls lightningcss --json', { stdio: 'pipe' }).toString(); |
| 40 | + const json = JSON.parse(out); |
| 41 | + if (json.dependencies && json.dependencies.lightningcss && json.dependencies.lightningcss.version) { |
| 42 | + return json.dependencies.lightningcss.version; |
| 43 | + } |
| 44 | + } catch {} |
| 45 | + // Fallback: resolve file path then read nearest package.json |
| 46 | + try { |
| 47 | + const entry = require.resolve('lightningcss/node/index.js'); |
| 48 | + let dir = path.dirname(entry); |
| 49 | + while (dir && dir !== path.parse(dir).root) { |
| 50 | + const pj = path.join(dir, 'package.json'); |
| 51 | + try { |
| 52 | + const data = JSON.parse(readFileSync(pj, 'utf8')); |
| 53 | + if (data.name === 'lightningcss' && data.version) return data.version; |
| 54 | + } catch {} |
| 55 | + dir = path.dirname(dir); |
| 56 | + } |
| 57 | + } catch {} |
| 58 | + return ''; |
| 59 | +} |
| 60 | + |
| 61 | +const lightningCssVer = resolveLightningCssVersion(); |
| 62 | +const biomeVer = getVersionSafe(() => require('../webapp/package.json').devDependencies['@biomejs/biome'], '@biomejs/biome'); |
| 63 | + |
| 64 | +const pkgs = []; |
| 65 | +if (rollupVer) pkgs.push(`@rollup/rollup-${suffix}@${rollupVer}`); |
| 66 | +if (lightningCssVer) pkgs.push(`lightningcss-${suffix}@${lightningCssVer}`); |
| 67 | +// Biome CLI package naming does not include -gnu suffix |
| 68 | +if (biomeVer) pkgs.push(`@biomejs/cli-linux-x64@${biomeVer}`); |
| 69 | + |
| 70 | +if (pkgs.length === 0) { |
| 71 | + log('INFO: No platform-specific packages to install (versions unresolved).'); |
| 72 | + process.exit(0); |
| 73 | +} |
| 74 | + |
| 75 | +log(`Installing platform binaries: ${pkgs.join(', ')}`); |
| 76 | +try { |
| 77 | + // Install at repo root to match hoisted module locations; avoid lockfile churn |
| 78 | + execSync(`npm i --no-save --no-audit --progress=false ${pkgs.join(' ')}`, { |
| 79 | + stdio: 'inherit', |
| 80 | + env: { ...process.env, npm_config_package_lock: 'false' }, |
| 81 | + }); |
| 82 | +} catch (e) { |
| 83 | + log(`ERROR: Failed to install platform binaries: ${e.status || ''}`); |
| 84 | + process.exit(1); |
| 85 | +} |
| 86 | + |
| 87 | +// Verify presence |
| 88 | +const verifyPkgs = [ |
| 89 | + '@rollup/rollup-linux-x64-gnu', |
| 90 | + 'lightningcss-linux-x64-gnu', |
| 91 | + '@biomejs/cli-linux-x64', |
| 92 | +]; |
| 93 | + |
| 94 | +for (const p of verifyPkgs) { |
| 95 | + try { |
| 96 | + execSync(`npm ls ${p}`, { stdio: 'inherit' }); |
| 97 | + } catch { |
| 98 | + log(`WARN: ${p} not found after install (may be unused or version unresolved).`); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +log('Done installing platform-specific binaries.'); |
0 commit comments