github.com/getgauge/gauge@v1.6.9/build/npm/src/index.js (about)

     1  #!/usr/bin/env node
     2  
     3  "use strict"
     4  
     5  const install = require("./install"),
     6      path = require("path"),
     7      AdmZip = require('adm-zip'),
     8      https = require('https'),
     9      packageJsonPath = path.join(__dirname, "..", "package.json"),
    10      binPath = "./bin";
    11  
    12  var extractZipArchive = function(buffer) {
    13      return new Promise(function(resolve, reject) {
    14          try {
    15              const zip = new AdmZip(buffer);
    16              zip.extractAllTo(path.normalize(binPath), true, true);
    17              resolve();
    18          } catch (err) {
    19              reject(new Error(`Failed to extract archive from buffer: ${err.message}`));
    20          }
    21      })
    22  }
    23  
    24  var downloadFollowingRedirect = function(url, resolve, reject) {
    25      https.get(url, { headers: { 'accept-encoding': 'gzip,deflate' } }, res => {
    26          if (res.statusCode >= 300 && res.statusCode < 400) {
    27              downloadFollowingRedirect(res.headers.location, resolve, reject);
    28              res.resume()
    29          } else if (res.statusCode >= 400) {
    30              reject(new Error(`Unable to download '${url}' : ${res.statusCode}-'${res.statusMessage}'`));
    31              res.resume()
    32          } else {
    33              const chunks = [];
    34              res
    35                  .on('data', chunk => chunks.push(chunk))
    36                  .on('end', () => resolve(Buffer.concat(chunks)))
    37                  .on('error', reject);
    38          }
    39      });
    40  };
    41  
    42  var downloadAndExtract = function(version) {
    43      console.log(`Fetching download url for Gauge version ${version}`);
    44      let url = install.getBinaryUrl(version);
    45      console.log(`Downloading ${url} to ${binPath}`);
    46      return new Promise((resolve, reject) => {
    47          try {
    48              downloadFollowingRedirect(url, resolve, reject);
    49          } catch (error) {
    50              reject(error);
    51          }
    52      })
    53      .then(extractZipArchive)
    54  };
    55  
    56  install.getVersion(packageJsonPath)
    57      .then((v) => downloadAndExtract(v.split('-')[0]))
    58      .catch((e) => console.error(e));