github.com/SAP/cloud-mta-build-tool@v1.2.27/install.js (about)

     1  var fs = require("fs");
     2  var axios = require("axios");
     3  var tar = require("tar");
     4  var zlib = require("zlib");
     5  var unzip = require("unzip-stream");
     6  var path = require("path");
     7  
     8  
     9  var packageInfo = require(path.join(process.cwd(), "package.json"));
    10  var version = packageInfo.version;
    11  
    12  var binName = process.argv[2];
    13  var os = process.argv[3] || process.platform;
    14  var arch = process.argv[4] || process.arch;
    15  var root = `https://github.com/SAP/${binName}/releases/download/v${version}/${binName}_${version}_`;
    16  
    17  
    18  var requested = os + "-" + arch;
    19  var current = process.platform + "-" + process.arch;
    20  if (requested !== current ) {
    21    console.error("WARNING: Installing binaries for the requested platform (" + requested + ") instead of for the actual platform (" + current + ").")
    22  }
    23  
    24  var unpackedBinPath = path.join(process.cwd(), "unpacked_bin");
    25  var config = {
    26    dirname: __dirname,
    27    binaries: [
    28        'mbt'
    29    ],
    30    urls: {
    31        'darwin-arm64': root + 'Darwin_arm64.tar.gz',
    32        'darwin-x64': root + 'Darwin_amd64.tar.gz',
    33        'linux-x64': root + 'Linux_amd64.tar.gz',
    34        'win32-x64': root + 'Windows_amd64.tar.gz'
    35    }
    36  };
    37  if (!fs.existsSync("bin")) {
    38    fs.mkdirSync("bin");
    39  }
    40  
    41  var binExt = "";
    42  if (os == "win32") {
    43    binExt = ".exe";
    44  }
    45  
    46  var buildId = os + "-" + arch;
    47  var url = config.urls[buildId];
    48  if (!url) {
    49    throw new Error("No binaries are available for your platform: " + buildId);
    50  }
    51  function binstall(url, path, options) {
    52    if (url.endsWith(".zip")) {
    53      return unzipUrl(url, path, options);
    54    } else {
    55      return untgz(url, path, options);
    56    }
    57  }
    58  
    59  function untgz(url, path, options) {
    60    options = options || {};
    61  
    62    var verbose = options.verbose;
    63    var verify = options.verify;
    64  
    65    return new Promise(function (resolve, reject) {
    66      var untar = tar
    67        .x({ cwd: path })
    68        .on("error", function (error) {
    69          reject("Error extracting " + url + " - " + error);
    70        })
    71        .on("end", function () {
    72          var successMessage = "Successfully downloaded and processed " + url;
    73  
    74          if (verify) {
    75            verifyContents(verify)
    76              .then(function () {
    77                resolve(successMessage);
    78              })
    79              .catch(reject);
    80          } else {
    81            resolve(successMessage);
    82          }
    83        });
    84  
    85      var gunzip = zlib.createGunzip().on("error", function (error) {
    86        reject("Error decompressing " + url + " " + error);
    87      });
    88  
    89      try {
    90        fs.mkdirSync(path);
    91      } catch (error) {
    92        if (error.code !== "EEXIST") throw error;
    93      }
    94  
    95      if (verbose) {
    96        console.log("Downloading binaries from " + url);
    97      }
    98  
    99      axios
   100        .get(url, { responseType: "stream" })
   101        .then((response) => {
   102          response.data.pipe(gunzip).pipe(untar);
   103        })
   104        .catch((error) => {
   105          if (verbose) {
   106            console.error(error);
   107          } else {
   108            console.error(error.message);
   109          }
   110        });
   111    });
   112  }
   113  
   114  function unzipUrl(url, path, options) {
   115    options = options || {};
   116  
   117    var verbose = options.verbose;
   118    var verify = options.verify;
   119  
   120    return new Promise(function (resolve, reject) {
   121      var writeStream = unzip
   122        .Extract({ path: path })
   123        .on("error", function (error) {
   124          reject("Error extracting " + url + " - " + error);
   125        })
   126        .on("entry", function (entry) {
   127          console.log("Entry: " + entry.path);
   128        })
   129        .on("close", function () {
   130          var successMessage = "Successfully downloaded and processed " + url;
   131  
   132          if (verify) {
   133            verifyContents(verify)
   134              .then(function () {
   135                resolve(successMessage);
   136              })
   137              .catch(reject);
   138          } else {
   139            resolve(successMessage);
   140          }
   141        });
   142  
   143      if (verbose) {
   144        console.log("Downloading binaries from " + url);
   145      }
   146  
   147      axios
   148        .get(url, { responseType: "stream" })
   149        .then((response) => {
   150          response.data.pipe(writeStream);
   151        })
   152        .catch((error) => {
   153          if (verbose) {
   154            console.error(error);
   155          } else {
   156            console.error(error.message);
   157          }
   158        });
   159    });
   160  }
   161  
   162  function verifyContents(files) {
   163    return Promise.all(
   164      files.map(function (filePath) {
   165        return new Promise(function (resolve, reject) {
   166          fs.stat(filePath, function (err, stats) {
   167            if (err) {
   168              reject(filePath + " was not found.");
   169            } else if (!stats.isFile()) {
   170              reject(filePath + " was not a file.");
   171            } else {
   172              resolve();
   173            }
   174          });
   175        });
   176      })
   177    );
   178  }
   179  
   180  binstall(url, unpackedBinPath).then(function() {
   181    config.binaries.forEach(function(bin) {
   182      fs.chmodSync(path.join(unpackedBinPath, bin + binExt), "755");
   183    });
   184  }).then(function(result) {
   185    process.exit(0);
   186  }, function(result) {
   187    console.error("ERR", result);
   188    process.exit(1);
   189  });