Skip to content

Commit

Permalink
Search for a local ibazel installation in node_modules (bazelbuild#74)
Browse files Browse the repository at this point in the history
Make index.js look for a local installation
  • Loading branch information
dougkoch authored and achew22 committed Dec 3, 2017
1 parent d0a5319 commit f49cb8c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion npm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit f49cb8c

Please sign in to comment.