Skip to content

Commit 8740f3a

Browse files
fix(backendjsonrpc): fix command (#520)
1 parent 562b805 commit 8740f3a

File tree

1 file changed

+67
-29
lines changed
  • packages/@postgrestools/backend-jsonrpc/src

1 file changed

+67
-29
lines changed
Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
1+
import { execSync } from "node:child_process";
2+
13
/**
24
* Gets the path of the binary for the current platform
35
*
46
* @returns Filesystem path to the binary, or null if no prebuilt distribution exists for the current platform
57
*/
68
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;
3674
}

0 commit comments

Comments
 (0)