|
| 1 | +import { spawnSync } from "node:child_process" |
| 2 | +import { existsSync, mkdirSync } from "node:fs" |
| 3 | +import { dirname, resolve } from "node:path" |
| 4 | +import { fileURLToPath } from "node:url" |
| 5 | + |
| 6 | +export const NODE26_VERSION = "v26.1.0" |
| 7 | + |
| 8 | +const scriptDir = dirname(fileURLToPath(import.meta.url)) |
| 9 | +const packageRoot = resolve(scriptDir, "..") |
| 10 | +const cacheDir = resolve(packageRoot, ".cache/node26") |
| 11 | + |
| 12 | +function getTarget() { |
| 13 | + if (process.arch !== "x64") { |
| 14 | + throw new Error(`This examples runner downloads the x64 Node 26 builds, not ${process.platform}-${process.arch}.`) |
| 15 | + } |
| 16 | + |
| 17 | + switch (process.platform) { |
| 18 | + case "linux": |
| 19 | + return { |
| 20 | + archiveName: `node-${NODE26_VERSION}-linux-x64.tar.xz`, |
| 21 | + directoryName: `node-${NODE26_VERSION}-linux-x64`, |
| 22 | + nodeRelativePath: "bin/node", |
| 23 | + url: `https://nodejs.org/dist/${NODE26_VERSION}/node-${NODE26_VERSION}-linux-x64.tar.xz`, |
| 24 | + } |
| 25 | + case "darwin": |
| 26 | + return { |
| 27 | + archiveName: `node-${NODE26_VERSION}-darwin-x64.tar.gz`, |
| 28 | + directoryName: `node-${NODE26_VERSION}-darwin-x64`, |
| 29 | + nodeRelativePath: "bin/node", |
| 30 | + url: `https://nodejs.org/dist/${NODE26_VERSION}/node-${NODE26_VERSION}-darwin-x64.tar.gz`, |
| 31 | + } |
| 32 | + case "win32": |
| 33 | + return { |
| 34 | + archiveName: `node-${NODE26_VERSION}-win-x64.zip`, |
| 35 | + directoryName: `node-${NODE26_VERSION}-win-x64`, |
| 36 | + nodeRelativePath: "node.exe", |
| 37 | + url: `https://nodejs.org/dist/${NODE26_VERSION}/node-${NODE26_VERSION}-win-x64.zip`, |
| 38 | + } |
| 39 | + default: |
| 40 | + throw new Error(`This examples runner does not have a Node 26 download for ${process.platform}-${process.arch}.`) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function getNode26Path() { |
| 45 | + const target = getTarget() |
| 46 | + return resolve(cacheDir, target.directoryName, target.nodeRelativePath) |
| 47 | +} |
| 48 | + |
| 49 | +export function ensureNode26() { |
| 50 | + const target = getTarget() |
| 51 | + const archivePath = resolve(cacheDir, target.archiveName) |
| 52 | + const nodeDir = resolve(cacheDir, target.directoryName) |
| 53 | + const nodePath = resolve(nodeDir, target.nodeRelativePath) |
| 54 | + |
| 55 | + if (existsSync(nodePath)) { |
| 56 | + return nodePath |
| 57 | + } |
| 58 | + |
| 59 | + mkdirSync(cacheDir, { recursive: true }) |
| 60 | + |
| 61 | + if (!existsSync(archivePath)) { |
| 62 | + run("curl", ["-L", target.url, "-o", archivePath]) |
| 63 | + } |
| 64 | + |
| 65 | + extractArchive(archivePath, cacheDir) |
| 66 | + |
| 67 | + if (!existsSync(nodePath)) { |
| 68 | + throw new Error(`Downloaded Node 26 archive did not produce ${nodePath}`) |
| 69 | + } |
| 70 | + |
| 71 | + return nodePath |
| 72 | +} |
| 73 | + |
| 74 | +function extractArchive(archivePath, outputDir) { |
| 75 | + if (archivePath.endsWith(".zip")) { |
| 76 | + if (process.platform === "win32") { |
| 77 | + run("powershell", [ |
| 78 | + "-NoProfile", |
| 79 | + "-Command", |
| 80 | + `Expand-Archive -Path '${archivePath}' -DestinationPath '${outputDir}' -Force`, |
| 81 | + ]) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + run("unzip", ["-q", archivePath, "-d", outputDir]) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + run("tar", ["-xf", archivePath, "-C", outputDir]) |
| 90 | +} |
| 91 | + |
| 92 | +function run(command, args) { |
| 93 | + const result = spawnSync(command, args, { stdio: "inherit" }) |
| 94 | + |
| 95 | + if (result.error) { |
| 96 | + throw result.error |
| 97 | + } |
| 98 | + |
| 99 | + if (result.status !== 0) { |
| 100 | + process.exit(result.status ?? 1) |
| 101 | + } |
| 102 | +} |
0 commit comments