| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import { spawnSync } from "child_process"; |
| 4 | |
| 5 | /** |
| 6 | * Return the bundled executable path from node_modules. |
| 7 | * Package names follow the cli-OS-ARCH pattern. |
| 8 | * Windows builds use the .exe suffix. |
| 9 | * @see https://nodejs.org/api/os.html#osarch |
| 10 | * @see https://nodejs.org/api/os.html#osplatform |
| 11 | * @example "x/xx/node_modules/cli-darwin-arm64" |
| 12 | */ |
| 13 | function getExePath() { |
| 14 | const arch = process.arch; |
| 15 | let os = process.platform as string; |
| 16 | let extension = ""; |
| 17 | if (["win32", "cygwin"].includes(process.platform)) { |
| 18 | os = "windows"; |
| 19 | extension = ".exe"; |
| 20 | } |
| 21 | |
| 22 | try { |
| 23 | // Since the binary will be located inside node_modules, we can simply call require.resolve |
| 24 | return require.resolve(`@ondeinference/cli-${os}-${arch}/bin/onde${extension}`); |
| 25 | } catch (e) { |
| 26 | throw new Error( |
| 27 | `Couldn't find application binary inside node_modules for ${os}-${arch}` |
| 28 | ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Run the bundled executable with the current CLI args. |
| 34 | */ |
| 35 | function run() { |
| 36 | const args = process.argv.slice(2); |
| 37 | const processResult = spawnSync(getExePath(), args, { stdio: "inherit" }); |
| 38 | process.exit(processResult.status ?? 0); |
| 39 | } |
| 40 | |
| 41 | run(); |