1 #!/usr/bin/env node
2
3 import { spawnSync } from "child_process";
4
5 /**
6 * Returns the executable path which is located inside `node_modules`
7 * The naming convention is xcrs-${os}-${arch}
8 * If the platform is `win32` or `cygwin`, executable will include a `.exe` extension.
9 * @see https://nodejs.org/api/os.html#osarch
10 * @see https://nodejs.org/api/os.html#osplatform
11 * @example "x/xx/node_modules/xcrs-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 // The binary lives in `node_modules`, so `require.resolve` gives us the installed path.
24 return require.resolve(
25 `@smbcloud/xcrs-${os}-${arch}/bin/xcrs${extension}`,
26 );
27 } catch (e) {
28 throw new Error(
29 `Couldn't find application binary inside node_modules for ${os}-${arch}`,
30 );
31 }
32 }
33
34 /**
35 * Runs the native CLI with the current process arguments.
36 */
37 function run() {
38 const args = process.argv.slice(2);
39 const processResult = spawnSync(getExePath(), args, { stdio: "inherit" });
40 process.exit(processResult.status ?? 0);
41 }
42
43 run();