|
@@ -1,85 +1,93 @@
|
|
#!/usr/bin/env node
|
|
#!/usr/bin/env node
|
|
-const fs = require("fs");
|
|
|
|
-const { spawn, spawnSync } = require("child_process");
|
|
|
|
-const path = require("path");
|
|
|
|
-const { arch, platform } = require("os");
|
|
|
|
-const { version } = require("./package.json");
|
|
|
|
|
|
+import fs from "fs";
|
|
|
|
+import { spawn, spawnSync } from "child_process";
|
|
|
|
+import path from "path";
|
|
|
|
+import { arch, platform } from "os";
|
|
|
|
+import { version } from "./package.json";
|
|
|
|
|
|
const PACKAGE_VERSION = `bolt-cli ${version}`;
|
|
const PACKAGE_VERSION = `bolt-cli ${version}`;
|
|
-const PACKAGE_BOLT_PATH = path.join(__dirname, "bolt");
|
|
|
|
|
|
|
|
-function getBinaryVersion(location) {
|
|
|
|
|
|
+function getBinaryVersion(location: string): [string | null, string | null] {
|
|
const result = spawnSync(location, ["--version"]);
|
|
const result = spawnSync(location, ["--version"]);
|
|
- const error =
|
|
|
|
|
|
+ const error: string | null =
|
|
(result.error && result.error.toString()) ||
|
|
(result.error && result.error.toString()) ||
|
|
(result.stderr.length > 0 && result.stderr.toString().trim()) ||
|
|
(result.stderr.length > 0 && result.stderr.toString().trim()) ||
|
|
null;
|
|
null;
|
|
return [error, result.stdout && result.stdout.toString().trim()];
|
|
return [error, result.stdout && result.stdout.toString().trim()];
|
|
}
|
|
}
|
|
|
|
|
|
-function runBolt(location) {
|
|
|
|
|
|
+function getExePath(): string {
|
|
|
|
+ let os: string = platform();
|
|
|
|
+ let extension = "";
|
|
|
|
+ if (["win32", "cygwin"].includes(os)) {
|
|
|
|
+ os = "windows";
|
|
|
|
+ extension = ".exe";
|
|
|
|
+ }
|
|
|
|
+ const binaryName = `@magicblock-labs/bolt-cli-${os}-${arch()}/bin/bolt${extension}`;
|
|
|
|
+ try {
|
|
|
|
+ return require.resolve(binaryName);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ throw new Error(
|
|
|
|
+ `Couldn't find application binary inside node_modules for ${os}-${arch()}`
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function runBolt(location: string): void {
|
|
const args = process.argv.slice(2);
|
|
const args = process.argv.slice(2);
|
|
const bolt = spawn(location, args, { stdio: "inherit" });
|
|
const bolt = spawn(location, args, { stdio: "inherit" });
|
|
- bolt.on("exit", (code, signal) => {
|
|
|
|
|
|
+ bolt.on("exit", (code: number | null, signal: NodeJS.Signals | null) => {
|
|
process.on("exit", () => {
|
|
process.on("exit", () => {
|
|
if (signal) {
|
|
if (signal) {
|
|
process.kill(process.pid, signal);
|
|
process.kill(process.pid, signal);
|
|
- } else {
|
|
|
|
|
|
+ } else if (code !== null) {
|
|
process.exit(code);
|
|
process.exit(code);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
- process.on("SIGINT", function () {
|
|
|
|
|
|
+ process.on("SIGINT", () => {
|
|
bolt.kill("SIGINT");
|
|
bolt.kill("SIGINT");
|
|
bolt.kill("SIGTERM");
|
|
bolt.kill("SIGTERM");
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
-function tryPackageBolt() {
|
|
|
|
- if (arch() !== "x64" || platform() !== "linux") {
|
|
|
|
- console.error(`Only x86_64 / Linux distributed in NPM package right now.`);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- const [error, binaryVersion] = getBinaryVersion(PACKAGE_BOLT_PATH);
|
|
|
|
- if (error !== null) {
|
|
|
|
- console.error(`Failed to get version of local binary: ${error}`);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- if (binaryVersion !== PACKAGE_VERSION) {
|
|
|
|
|
|
+function tryPackageBolt(): boolean {
|
|
|
|
+ try {
|
|
|
|
+ const path = getExePath();
|
|
|
|
+ runBolt(path);
|
|
|
|
+ return true;
|
|
|
|
+ } catch (e) {
|
|
console.error(
|
|
console.error(
|
|
- `Package binary version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`
|
|
|
|
|
|
+ "Failed to run bolt from package:",
|
|
|
|
+ e instanceof Error ? e.message : e
|
|
);
|
|
);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
-
|
|
|
|
- runBolt(PACKAGE_BOLT_PATH);
|
|
|
|
- return true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-function trySystemBolt() {
|
|
|
|
- console.error("Trying globally installed bolt.");
|
|
|
|
-
|
|
|
|
- const absolutePath = process.env.PATH.split(":")
|
|
|
|
|
|
+function trySystemBolt(): void {
|
|
|
|
+ const absolutePath = process.env.PATH?.split(path.delimiter)
|
|
.filter((dir) => dir !== path.dirname(process.argv[1]))
|
|
.filter((dir) => dir !== path.dirname(process.argv[1]))
|
|
.find((dir) => {
|
|
.find((dir) => {
|
|
try {
|
|
try {
|
|
fs.accessSync(`${dir}/bolt`, fs.constants.X_OK);
|
|
fs.accessSync(`${dir}/bolt`, fs.constants.X_OK);
|
|
|
|
+ return true;
|
|
} catch {
|
|
} catch {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
});
|
|
});
|
|
|
|
|
|
if (!absolutePath) {
|
|
if (!absolutePath) {
|
|
- console.error(`Could not find globally installed bolt, install with cargo.`);
|
|
|
|
- process.exit();
|
|
|
|
|
|
+ console.error(
|
|
|
|
+ `Could not find globally installed bolt, please install with cargo.`
|
|
|
|
+ );
|
|
|
|
+ process.exit(1);
|
|
}
|
|
}
|
|
|
|
|
|
const absoluteBinaryPath = `${absolutePath}/bolt`;
|
|
const absoluteBinaryPath = `${absolutePath}/bolt`;
|
|
-
|
|
|
|
const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath);
|
|
const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath);
|
|
|
|
+
|
|
if (error !== null) {
|
|
if (error !== null) {
|
|
console.error(`Failed to get version of global binary: ${error}`);
|
|
console.error(`Failed to get version of global binary: ${error}`);
|
|
return;
|
|
return;
|
|
@@ -93,5 +101,4 @@ function trySystemBolt() {
|
|
|
|
|
|
runBolt(absoluteBinaryPath);
|
|
runBolt(absoluteBinaryPath);
|
|
}
|
|
}
|
|
-
|
|
|
|
tryPackageBolt() || trySystemBolt();
|
|
tryPackageBolt() || trySystemBolt();
|