"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/script/download-node.js" (8 Mar 2017, 3174 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var fs = require('fs');
    2 var mv = require('mv');
    3 var zlib = require('zlib');
    4 var path = require('path');
    5 
    6 var tar = require('tar');
    7 var temp = require('temp');
    8 
    9 var request = require('request');
   10 var getInstallNodeVersion = require('./bundled-node-version')
   11 
   12 temp.track();
   13 
   14 var identifyArch = function() {
   15   switch (process.arch) {
   16     case "ia32":  return "x86";
   17     case "arm":   return "armv" + process.config.variables.arm_version + "l";
   18     default:      return process.arch;
   19   }
   20 };
   21 
   22 var downloadFileToLocation = function(url, filename, callback) {
   23   var stream = fs.createWriteStream(filename);
   24   stream.on('end', callback);
   25   stream.on('error', callback);
   26   var requestStream = request.get(url)
   27   requestStream.on('response', function(response) {
   28     if (response.statusCode == 404) {
   29       console.error('download not found:', url);
   30       process.exit(1);
   31     }
   32     requestStream.pipe(stream);
   33   });
   34 };
   35 
   36 var downloadTarballAndExtract = function(url, location, callback) {
   37   var tempPath = temp.mkdirSync('apm-node-');
   38   var stream = tar.Extract({
   39     path: tempPath
   40   });
   41   stream.on('end', function() {
   42     callback.call(this, tempPath);
   43   });
   44   stream.on('error', callback);
   45   var requestStream = request.get(url)
   46   requestStream.on('response', function(response) {
   47     if (response.statusCode == 404) {
   48       console.error('download not found:', url);
   49       process.exit(1);
   50     }
   51     requestStream.pipe(zlib.createGunzip()).pipe(stream);
   52   });
   53 };
   54 
   55 var copyNodeBinToLocation = function(callback, version, targetFilename, fromDirectory) {
   56   var arch = identifyArch();
   57   var subDir = "node-" + version + "-" + process.platform + "-" + arch;
   58   var downloadedNodePath = path.join(fromDirectory, subDir, 'bin', 'node');
   59   return mv(downloadedNodePath, targetFilename, {mkdirp: true}, function(err) {
   60     if (err) {
   61       callback(err);
   62       return;
   63     }
   64 
   65     fs.chmodSync(targetFilename, "755");
   66     callback()
   67   });
   68 };
   69 
   70 var downloadNode = function(version, done) {
   71   var arch = identifyArch();
   72   var filename = path.join(__dirname, '..', 'bin', process.platform === 'win32' ? 'node.exe' : 'node');
   73 
   74   var downloadFile = function() {
   75     if (process.platform === 'win32') {
   76       downloadFileToLocation("https://nodejs.org/dist/" + version + "/win-" + arch + "/node.exe", filename, done);
   77     } else {
   78       var next = copyNodeBinToLocation.bind(this, done, version, filename);
   79       downloadTarballAndExtract("https://nodejs.org/dist/" + version + "/node-" + version + "-" + process.platform + "-" + arch + ".tar.gz", filename, next);
   80     }
   81   }
   82 
   83   if (fs.existsSync(filename)) {
   84     getInstallNodeVersion(filename, function(error, installedVersion) {
   85       if (error != null) {
   86         done(error);
   87       } else if (installedVersion !== version) {
   88         downloadFile();
   89       } else {
   90         done();
   91       }
   92     });
   93   } else {
   94     downloadFile();
   95   }
   96 };
   97 
   98 var versionToInstall = fs.readFileSync(path.resolve(__dirname, '..', 'BUNDLED_NODE_VERSION'), 'utf8').trim()
   99 downloadNode(versionToInstall, function(error) {
  100   if (error != null) {
  101     console.error('Failed to download node', error);
  102     return process.exit(1);
  103   } else {
  104     return process.exit(0);
  105   }
  106 });