github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/launcher.js (about)

     1  #!/usr/bin/env node
     2  // Copyright 2020 The Bazel Authors. All rights reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  'use strict';
    16  
    17  // This package inspired by
    18  // https://github.com/angular/clang-format/blob/master/index.js
    19  const os = require('os');
    20  const path = require('path');
    21  const spawn = require('child_process').spawn;
    22  
    23  function getNativeBinary() {
    24    const arch = {
    25      'x64' : 'amd64',
    26    }[os.arch()];
    27    // Filter the platform based on the platforms that are build/included.
    28    const platform = {
    29      'darwin' : 'darwin',
    30      'linux' : 'linux',
    31      'win32' : 'windows',
    32    }[os.platform()];
    33    const extension = {
    34      'darwin' : '',
    35      'linux' : '',
    36      'win32' : '.exe',
    37    }[os.platform()];
    38  
    39    if (arch == undefined || platform == undefined) {
    40      console.error(`FATAL: Your platform/architecture combination ${
    41          os.platform()} - ${os.arch()} is not yet supported.
    42      See instructions at https://github.com/bazelbuild/buildtools/blob/master/_TOOL_/README.md.`);
    43      return Promise.resolve(1);
    44    }
    45  
    46    const binary =
    47        path.join(__dirname, `_TOOL_-${platform}_${arch}${extension}`);
    48    return binary;
    49  }
    50  
    51  function main(args) {
    52    const binary = getNativeBinary();
    53    const ps = spawn(binary, args, {stdio : 'inherit'});
    54  
    55    function shutdown() {
    56      ps.kill("SIGTERM")
    57      process.exit();
    58    }
    59  
    60    process.on("SIGINT", shutdown);
    61    process.on("SIGTERM", shutdown);
    62  
    63    ps.on('close', e => process.exitCode = e);
    64  }
    65  
    66  if (require.main === module) {
    67    main(process.argv.slice(2));
    68  }
    69  
    70  module.exports = {
    71    getNativeBinary,
    72  };