github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/npm/init.js (about) 1 validateNpmVersion(); 2 3 var https = require('https'); 4 var http = require('http'); 5 var url = require('url'); 6 var fs = require('fs'); 7 var packageJson = require('./package.json'); 8 var fileName = getFileName(); 9 var filePath = "bin/" + fileName; 10 var version = packageJson.version; 11 var btPackage = "jfrog-cli-" + getArchitecture(); 12 13 downloadCli(); 14 15 function validateNpmVersion() { 16 if (!isValidNpmVersion()) { 17 throw new Error("JFrog CLI can be installed using npm version 5.0.0 or above."); 18 } 19 } 20 21 function downloadWithProxy(myUrl) { 22 var proxyparts = url.parse(process.env.https_proxy); 23 var myUrlParts = url.parse(myUrl); 24 25 http.request({ 26 host: proxyparts.hostname, 27 port: proxyparts.port, 28 method: 'CONNECT', 29 path: myUrlParts.hostname + ':443' 30 }).on('connect', function(res, socket, head) { 31 https.get({ 32 host: myUrlParts.hostname, 33 socket: socket, 34 path: myUrlParts.path, 35 agent: false 36 }, function(res) { 37 if (res.statusCode == 301 || res.statusCode == 302) { 38 downloadWithProxy(res.headers.location); 39 } else if (res.statusCode == 200) { 40 writeToFile(res); 41 } else { 42 console.log('Unexpected status code ' + res.statusCode + ' during JFrog CLI download'); 43 } 44 }).on('error', function (err) {console.error(err);}); 45 }).end(); 46 } 47 48 function download(url) { 49 https.get(url, function(res) { 50 if (res.statusCode == 301 || res.statusCode == 302) { 51 download(res.headers.location); 52 } else if (res.statusCode == 200) { 53 writeToFile(res); 54 } else { 55 console.log('Unexpected status code ' + res.statusCode + ' during JFrog CLI download'); 56 } 57 }).on('error', function (err) {console.error(err);}); 58 } 59 60 function downloadCli() { 61 console.log("Downloading JFrog CLI " + version ); 62 var startUrl = 'https://api.bintray.com/content/jfrog/jfrog-cli-go/' + version + '/' + btPackage + '/' + fileName + '?bt_package=' + btPackage; 63 // We detect outbount proxy by looking at the environment variable 64 if (process.env.https_proxy && process.env.https_proxy.length > 0) { 65 downloadWithProxy(startUrl); 66 } else { 67 download(startUrl); 68 } 69 } 70 71 function isValidNpmVersion() { 72 var child_process = require('child_process'); 73 var npmVersionCmdOut = child_process.execSync("npm version -json"); 74 var npmVersion = JSON.parse(npmVersionCmdOut).npm; 75 // Supported since version 5.0.0 76 return parseInt(npmVersion.charAt(0)) > 4; 77 } 78 79 function writeToFile(response) { 80 var file = fs.createWriteStream(filePath); 81 response.on('data', function (chunk) { 82 file.write(chunk); 83 }).on('end', function () { 84 file.end(); 85 if (!process.platform.startsWith("win")) { 86 fs.chmodSync(filePath, 0555); 87 } 88 }).on('error', function (err) { 89 console.error(err); 90 }); 91 } 92 93 function getArchitecture() { 94 var platform = process.platform; 95 if (platform.startsWith("win")) { 96 return "windows-amd64"; 97 } 98 if (platform.includes("darwin")) { 99 return "mac-386"; 100 } 101 if (process.arch.includes("64")) { 102 return "linux-amd64"; 103 } 104 return "linux-386"; 105 } 106 107 function getFileName() { 108 var executable = "jfrog"; 109 if (process.platform.startsWith("win")) { 110 executable += ".exe"; 111 } 112 return executable; 113 }