Skip to content

Commit

Permalink
cli: Fix for npm globals unknown command (coral-xyz#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlinton authored Oct 18, 2021
1 parent 0aeaac7 commit 1d14878
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions cli/npm-package/anchor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
const fs = require("fs");
const { spawn, spawnSync } = require("child_process");
const path = require("path");
const { arch, platform } = require("os");
Expand Down Expand Up @@ -60,13 +61,20 @@ function tryPackageAnchor() {
function trySystemAnchor() {
console.error("Trying globally installed anchor.");

const added = path.dirname(process.argv[1]);
const directories = process.env.PATH.split(":").filter(
(dir) => dir !== added
);
process.env.PATH = directories.join(":");
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`;

const [error, binaryVersion] = getBinaryVersion("anchor");
const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath);
if (error !== null) {
console.error(`Failed to get version of global binary: ${error}`);
return;
Expand All @@ -78,7 +86,7 @@ function trySystemAnchor() {
return;
}

runAnchor("anchor");
runAnchor(absoluteBinaryPath);
}

tryPackageAnchor() || trySystemAnchor();

0 comments on commit 1d14878

Please sign in to comment.