|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * Gets the path of the binary for the current platform |
3 | 5 | * |
4 | 6 | * @returns Filesystem path to the binary, or null if no prebuilt distribution exists for the current platform |
5 | 7 | */ |
6 | 8 | export function getCommand(): string | null { |
7 | | - const { platform, arch } = process; |
8 | | - |
9 | | - type PlatformPaths = { |
10 | | - [P in NodeJS.Platform]?: { |
11 | | - [A in NodeJS.Architecture]?: string; |
12 | | - }; |
13 | | - }; |
14 | | - |
15 | | - const PLATFORMS: PlatformPaths = { |
16 | | - win32: { |
17 | | - x64: "@postgrestools/cli-win32-x64/postgrestools.exe", |
18 | | - arm64: "@postgrestools/cli-win32-arm64/postgrestools.exe", |
19 | | - }, |
20 | | - darwin: { |
21 | | - x64: "@postgrestools/cli-darwin-x64/postgrestools", |
22 | | - arm64: "@postgrestools/cli-darwin-arm64/postgrestools", |
23 | | - }, |
24 | | - linux: { |
25 | | - x64: "@postgrestools/cli-linux-x64/postgrestools", |
26 | | - arm64: "@postgrestools/cli-linux-arm64/postgrestools", |
27 | | - }, |
28 | | - }; |
29 | | - |
30 | | - const binPath = PLATFORMS?.[platform]?.[arch]; |
31 | | - if (!binPath) { |
32 | | - return null; |
33 | | - } |
34 | | - |
35 | | - return require.resolve(binPath); |
| 9 | + const { platform, arch } = process; |
| 10 | + |
| 11 | + const PLATFORMS: Partial< |
| 12 | + Record< |
| 13 | + NodeJS.Platform | "linux-musl", |
| 14 | + Partial<Record<NodeJS.Architecture, string>> |
| 15 | + > |
| 16 | + > = { |
| 17 | + win32: { |
| 18 | + x64: "@postgrestools/cli-x86_64-windows-msvc/postgrestools.exe", |
| 19 | + arm64: "@postgrestools/cli-aarch64-windows-msvc/postgrestools.exe", |
| 20 | + }, |
| 21 | + darwin: { |
| 22 | + x64: "@postgrestools/cli-x86_64-apple-darwin/postgrestools", |
| 23 | + arm64: "@postgrestools/cli-aarch64-apple-darwin/postgrestools", |
| 24 | + }, |
| 25 | + linux: { |
| 26 | + x64: "@postgrestools/cli-x86_64-linux-gnu/postgrestools", |
| 27 | + arm64: "@postgrestools/cli-aarch64-linux-gnu/postgrestools", |
| 28 | + }, |
| 29 | + "linux-musl": { |
| 30 | + x64: "@postgrestools/cli-x86_64-linux-musl/postgrestools", |
| 31 | + // no arm64 build for musl |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + function isMusl() { |
| 36 | + let stderr = ""; |
| 37 | + try { |
| 38 | + stderr = execSync("ldd --version", { |
| 39 | + stdio: [ |
| 40 | + "ignore", // stdin |
| 41 | + "pipe", // stdout – glibc systems print here |
| 42 | + "pipe", // stderr – musl systems print here |
| 43 | + ], |
| 44 | + }).toString(); |
| 45 | + } catch (err: unknown) { |
| 46 | + if (hasStdErr(err)) { |
| 47 | + stderr = err.stderr; |
| 48 | + } |
| 49 | + } |
| 50 | + if (stderr.indexOf("musl") > -1) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + function getPlatform(): NodeJS.Platform | "linux-musl" { |
| 57 | + if (platform === "linux") { |
| 58 | + return isMusl() ? "linux-musl" : "linux"; |
| 59 | + } |
| 60 | + |
| 61 | + return platform; |
| 62 | + } |
| 63 | + |
| 64 | + const binPath = PLATFORMS?.[getPlatform()]?.[arch]; |
| 65 | + if (!binPath) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + return require.resolve(binPath); |
| 70 | +} |
| 71 | + |
| 72 | +function hasStdErr(err: unknown): err is { stderr: string } { |
| 73 | + return !!(err as any)?.stderr; |
36 | 74 | } |
0 commit comments