function getNativeBinary()

in release/npm/index.js [24:78]


function getNativeBinary() {
  const arch = {
    'x64' : 'amd64',
  }[os.arch()];
  // Filter the platform based on the platforms that are build/included.
  const platform = {
    'darwin' : 'darwin',
    'linux' : 'linux',
    'win32' : 'windows',
  }[os.platform()];
  const extension = {
    'darwin' : '',
    'linux' : '',
    'win32' : '.exe',
  }[os.platform()];

  if (arch == undefined || platform == undefined) {
    console.error(`FATAL: Your platform/architecture combination ${
        os.platform()} - ${os.arch()} is not yet supported.
    Follow install instructions at https://github.com/bazelbuild/bazel-watcher/blob/master/README.md to compile for your system.`);
    return Promise.resolve(1);
  }

  // By default, use the ibazel binary underneath this script
  var basePath = __dirname;
  var foundLocalInstallation = false;

  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' + extension))) {
      basePath = attemptedBasePath;
      foundLocalInstallation = true;
      break;
    }
  }

  if (!foundLocalInstallation) {
    console.error(`WARNING: no ibazel version found in your node_modules.
        We recommend installing a devDependency on ibazel so you use the same
        version as other engineers on this project.
        Using the globally installed version at ${__dirname}`);
  }

  const binary =
      path.join(basePath, 'bin', `${platform}_${arch}`, 'ibazel' + extension);
  return binary;
}