|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Ensure the pinned flatc version is available at .bin/flatc (or .bin/flatc.exe on Windows). |
| 4 | + * Downloads it if missing or wrong version. Prints the path on stdout. |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node scripts/ensure-flatc.mjs |
| 8 | + * # returns the path to the flatc binary |
| 9 | + */ |
| 10 | +import { execSync } from "node:child_process"; |
| 11 | +import { existsSync, mkdirSync, chmodSync, rmSync, mkdtempSync } from "node:fs"; |
| 12 | +import { join, dirname } from "node:path"; |
| 13 | +import { tmpdir, platform } from "node:os"; |
| 14 | +import { fileURLToPath } from "node:url"; |
| 15 | + |
| 16 | +const FLATC_VERSION = "25.12.19"; |
| 17 | + |
| 18 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 19 | +const PROJECT_DIR = dirname(__dirname); |
| 20 | +const BIN_DIR = join(PROJECT_DIR, ".bin"); |
| 21 | +const isWindows = platform() === "win32"; |
| 22 | +const FLATC = join(BIN_DIR, isWindows ? "flatc.exe" : "flatc"); |
| 23 | + |
| 24 | +// Check if we already have the right version |
| 25 | +if (existsSync(FLATC)) { |
| 26 | + try { |
| 27 | + const out = execSync(`"${FLATC}" --version`, { encoding: "utf8" }); |
| 28 | + const match = out.match(/(\d+\.\d+\.\d+)/); |
| 29 | + if (match && match[1] === FLATC_VERSION) { |
| 30 | + process.stdout.write(FLATC); |
| 31 | + process.exit(0); |
| 32 | + } |
| 33 | + console.error(`flatc in .bin/ is v${match?.[1]}, need v${FLATC_VERSION}`); |
| 34 | + } catch { |
| 35 | + // binary exists but can't run — re-download |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Determine asset name |
| 40 | +let asset; |
| 41 | +switch (platform()) { |
| 42 | + case "linux": |
| 43 | + asset = "Linux.flatc.binary.clang++-18.zip"; |
| 44 | + break; |
| 45 | + case "darwin": |
| 46 | + asset = "Mac.flatc.binary.zip"; |
| 47 | + break; |
| 48 | + case "win32": |
| 49 | + asset = "Windows.flatc.binary.zip"; |
| 50 | + break; |
| 51 | + default: |
| 52 | + console.error(`error: cannot download flatc for ${platform()}`); |
| 53 | + console.error(`Install flatc v${FLATC_VERSION} to .bin/flatc manually.`); |
| 54 | + process.exit(1); |
| 55 | +} |
| 56 | + |
| 57 | +mkdirSync(BIN_DIR, { recursive: true }); |
| 58 | + |
| 59 | +const url = `https://github.com/google/flatbuffers/releases/download/v${FLATC_VERSION}/${asset}`; |
| 60 | +const tmp = mkdtempSync(join(tmpdir(), "flatc-")); |
| 61 | +const zipPath = join(tmp, "flatc.zip"); |
| 62 | + |
| 63 | +try { |
| 64 | + console.error(`Downloading flatc v${FLATC_VERSION}...`); |
| 65 | + |
| 66 | + // Use curl on unix, PowerShell on Windows |
| 67 | + if (isWindows) { |
| 68 | + execSync( |
| 69 | + `powershell -Command "Invoke-WebRequest -Uri '${url}' -OutFile '${zipPath}'"`, |
| 70 | + { stdio: ["pipe", "pipe", "inherit"] }, |
| 71 | + ); |
| 72 | + execSync( |
| 73 | + `powershell -Command "Expand-Archive -Path '${zipPath}' -DestinationPath '${BIN_DIR}' -Force"`, |
| 74 | + { stdio: ["pipe", "pipe", "inherit"] }, |
| 75 | + ); |
| 76 | + } else { |
| 77 | + execSync(`curl -fsSL "${url}" -o "${zipPath}"`, { |
| 78 | + stdio: ["pipe", "pipe", "inherit"], |
| 79 | + }); |
| 80 | + execSync(`unzip -o -q "${zipPath}" -d "${BIN_DIR}/"`, { |
| 81 | + stdio: ["pipe", "pipe", "inherit"], |
| 82 | + }); |
| 83 | + chmodSync(FLATC, 0o755); |
| 84 | + } |
| 85 | + |
| 86 | + process.stdout.write(FLATC); |
| 87 | +} finally { |
| 88 | + rmSync(tmp, { recursive: true, force: true }); |
| 89 | +} |
0 commit comments