|
| 1 | +import { createRequire } from "node:module"; |
| 2 | +import { dirname, join } from "node:path"; |
| 3 | + |
| 4 | +const PLATFORMS: Record<string, string> = { |
| 5 | + "darwin-arm64": "@aaif/goose-binary-darwin-arm64", |
| 6 | + "darwin-x64": "@aaif/goose-binary-darwin-x64", |
| 7 | + "linux-arm64": "@aaif/goose-binary-linux-arm64", |
| 8 | + "linux-x64": "@aaif/goose-binary-linux-x64", |
| 9 | + "win32-x64": "@aaif/goose-binary-win32-x64", |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * Resolves the path to the goose binary. |
| 14 | + * |
| 15 | + * Resolution order: |
| 16 | + * 1. `GOOSE_BINARY` environment variable (explicit override) |
| 17 | + * 2. Platform-specific `@aaif/goose-binary-*` optional dependency |
| 18 | + * |
| 19 | + * @throws if no binary can be found |
| 20 | + */ |
| 21 | +export function resolveGooseBinary(): string { |
| 22 | + const envBinary = process.env.GOOSE_BINARY; |
| 23 | + if (envBinary) return envBinary; |
| 24 | + |
| 25 | + const key = `${process.platform}-${process.arch}`; |
| 26 | + const pkg = PLATFORMS[key]; |
| 27 | + if (!pkg) { |
| 28 | + throw new Error( |
| 29 | + `No goose binary available for ${key}. Set GOOSE_BINARY to the path of a goose binary.`, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + const require = createRequire(import.meta.url); |
| 35 | + const pkgDir = dirname(require.resolve(`${pkg}/package.json`)); |
| 36 | + const binName = process.platform === "win32" ? "goose.exe" : "goose"; |
| 37 | + return join(pkgDir, "bin", binName); |
| 38 | + } catch { |
| 39 | + throw new Error( |
| 40 | + `goose binary package ${pkg} is not installed. Set GOOSE_BINARY or install the native package.`, |
| 41 | + ); |
| 42 | + } |
| 43 | +} |
0 commit comments