anchor.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env node
  2. const fs = require("fs");
  3. const { spawn, spawnSync } = require("child_process");
  4. const path = require("path");
  5. const { arch, platform } = require("os");
  6. const { version } = require("./package.json");
  7. const PACKAGE_VERSION = `anchor-cli ${version}`;
  8. const PACKAGE_ANCHOR_PATH = path.join(__dirname, "anchor");
  9. function getBinaryVersion(location) {
  10. const result = spawnSync(location, ["--version"]);
  11. const error =
  12. (result.error && result.error.toString()) ||
  13. (result.stderr.length > 0 && result.stderr.toString().trim()) ||
  14. null;
  15. return [error, result.stdout && result.stdout.toString().trim()];
  16. }
  17. function runAnchor(location) {
  18. const args = process.argv.slice(2);
  19. const anchor = spawn(location, args, { stdio: "inherit" });
  20. anchor.on("exit", (code, signal) => {
  21. process.on("exit", () => {
  22. if (signal) {
  23. process.kill(process.pid, signal);
  24. } else {
  25. process.exit(code);
  26. }
  27. });
  28. });
  29. process.on("SIGINT", function () {
  30. anchor.kill("SIGINT");
  31. anchor.kill("SIGTERM");
  32. });
  33. }
  34. function tryPackageAnchor() {
  35. if (arch() !== "x64" || platform() !== "linux") {
  36. console.error(`Only x86_64 / Linux distributed in NPM package right now.`);
  37. return false;
  38. }
  39. const [error, binaryVersion] = getBinaryVersion(PACKAGE_ANCHOR_PATH);
  40. if (error !== null) {
  41. console.error(`Failed to get version of local binary: ${error}`);
  42. return false;
  43. }
  44. if (binaryVersion !== PACKAGE_VERSION) {
  45. console.error(
  46. `Package binary version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`
  47. );
  48. return false;
  49. }
  50. runAnchor(PACKAGE_ANCHOR_PATH);
  51. return true;
  52. }
  53. function trySystemAnchor() {
  54. console.error("Trying globally installed anchor.");
  55. const absolutePath = process.env.PATH.split(":")
  56. .filter((dir) => dir !== path.dirname(process.argv[1]))
  57. .find((dir) => {
  58. try {
  59. fs.accessSync(`${dir}/anchor`, fs.constants.X_OK);
  60. } catch {
  61. return false;
  62. }
  63. return true;
  64. });
  65. if (!absolutePath) {
  66. console.error(`Could not find globally installed anchor, install with cargo.`);
  67. process.exit();
  68. }
  69. const absoluteBinaryPath = `${absolutePath}/anchor`;
  70. const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath);
  71. if (error !== null) {
  72. console.error(`Failed to get version of global binary: ${error}`);
  73. return;
  74. }
  75. if (binaryVersion !== PACKAGE_VERSION) {
  76. console.error(
  77. `Globally installed anchor version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`
  78. );
  79. return;
  80. }
  81. runAnchor(absoluteBinaryPath);
  82. }
  83. tryPackageAnchor() || trySystemAnchor();