2021-07-03 10:29:16 -07:00
|
|
|
#!/usr/bin/env node
|
2021-10-17 17:52:32 -07:00
|
|
|
const fs = require("fs");
|
2021-07-03 10:29:16 -07:00
|
|
|
const { spawn, spawnSync } = require("child_process");
|
|
|
|
const path = require("path");
|
|
|
|
const { arch, platform } = require("os");
|
|
|
|
const { version } = require("./package.json");
|
|
|
|
|
|
|
|
const PACKAGE_VERSION = `anchor-cli ${version}`;
|
|
|
|
const PACKAGE_ANCHOR_PATH = path.join(__dirname, "anchor");
|
|
|
|
|
|
|
|
function getBinaryVersion(location) {
|
|
|
|
const result = spawnSync(location, ["--version"]);
|
|
|
|
const error =
|
|
|
|
(result.error && result.error.toString()) ||
|
|
|
|
(result.stderr.length > 0 && result.stderr.toString().trim()) ||
|
|
|
|
null;
|
|
|
|
return [error, result.stdout && result.stdout.toString().trim()];
|
|
|
|
}
|
|
|
|
|
|
|
|
function runAnchor(location) {
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
const anchor = spawn(location, args, { stdio: "inherit" });
|
|
|
|
anchor.on("exit", (code, signal) => {
|
|
|
|
process.on("exit", () => {
|
|
|
|
if (signal) {
|
|
|
|
process.kill(process.pid, signal);
|
|
|
|
} else {
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("SIGINT", function () {
|
|
|
|
anchor.kill("SIGINT");
|
|
|
|
anchor.kill("SIGTERM");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryPackageAnchor() {
|
|
|
|
if (arch() !== "x64" || platform() !== "linux") {
|
|
|
|
console.error(`Only x86_64 / Linux distributed in NPM package right now.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [error, binaryVersion] = getBinaryVersion(PACKAGE_ANCHOR_PATH);
|
|
|
|
if (error !== null) {
|
|
|
|
console.error(`Failed to get version of local binary: ${error}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (binaryVersion !== PACKAGE_VERSION) {
|
|
|
|
console.error(
|
|
|
|
`Package binary version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
runAnchor(PACKAGE_ANCHOR_PATH);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function trySystemAnchor() {
|
|
|
|
console.error("Trying globally installed anchor.");
|
|
|
|
|
2021-10-17 17:52:32 -07:00
|
|
|
const absolutePath = process.env.PATH.split(":")
|
|
|
|
.filter((dir) => dir !== path.dirname(process.argv[1]))
|
|
|
|
.find((dir) => {
|
|
|
|
try {
|
|
|
|
fs.accessSync(`${dir}/anchor`, fs.constants.X_OK);
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const absoluteBinaryPath = `${absolutePath}/anchor`;
|
2021-07-03 10:29:16 -07:00
|
|
|
|
2021-10-17 17:52:32 -07:00
|
|
|
const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath);
|
2021-07-03 10:29:16 -07:00
|
|
|
if (error !== null) {
|
|
|
|
console.error(`Failed to get version of global binary: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (binaryVersion !== PACKAGE_VERSION) {
|
|
|
|
console.error(
|
|
|
|
`Globally installed anchor version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:52:32 -07:00
|
|
|
runAnchor(absoluteBinaryPath);
|
2021-07-03 10:29:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tryPackageAnchor() || trySystemAnchor();
|