diff --git a/npm/index.js b/npm/index.js index e6306fe9..8c80ca0e 100644 --- a/npm/index.js +++ b/npm/index.js @@ -16,6 +16,7 @@ // This package inspired by // https://github.com/angular/clang-format/blob/master/index.js +const fs = require('fs'); const os = require('os'); const path = require('path'); const spawn = require('child_process').spawn; @@ -38,7 +39,23 @@ function main(args) { return Promise.resolve(1); } - const binary = path.join(__dirname, 'bin', `${platform}_${arch}`, 'ibazel'); + // By default, use the ibazel binary underneath this script + var basePath = __dirname; + + const dirs = process.cwd().split(path.sep); + + // Walk up the cwd, looking for a local ibazel installation + for (var i = dirs.length; i > 0; i--) { + var attemptedBasePath = [...dirs.slice(0, i), 'node_modules', '@bazel', 'ibazel'].join(path.sep); + + // If we find a local installation, use that one instead + if (fs.existsSync(path.join(attemptedBasePath, 'bin', `${platform}_${arch}`, 'ibazel'))) { + basePath = attemptedBasePath; + break; + } + } + + const binary = path.join(basePath, 'bin', `${platform}_${arch}`, 'ibazel'); const ibazel = spawn(binary, args, {stdio: 'inherit'}); ibazel.on('close', e => process.exitCode = e); }