-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
94 lines (85 loc) · 2.88 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
validateNpmVersion();
var https = require('https');
var http = require('http');
var fs = require('fs');
var packageJson = require('./package.json');
var fileName = getFileName();
var filePath = "bin/" + fileName;
var version = packageJson.version;
var btPackage = "jfrog-cli-" + getArchitecture();
downloadCli();
function validateNpmVersion() {
if (!isValidNpmVersion()) {
throw new Error("JFrog CLI can be installed using npm version 5.0.0 or above.")
}
}
function redirectDetectDownload(starturl) {
if(process.env.https_proxy && process.env.https_proxy.length > 0) {
var mainurl = process.env.https_proxy + starturl.replace('https://', '/https/');
console.log(mainurl);
http.get(mainurl, function(res) {
if(res.statusCode == 302) {
redirectDetectDownload(res.headers.location);
} else if (res.statusCode == 200) {
writeToFile(res)
} else {
console.log('Unexpected status code during JFrog CLI download')
}
}).on('error', function (err) {console.error(err);});
} else {
https.get(starturl, function(res) {
if(res.statusCode == 302) {
redirectDetectDownload(res.headers.location);
} else if (res.statusCode == 200) {
writeToFile(res)
} else {
console.log('Unexpected status code during JFrog CLI download')
}
}).on('error', function (err) {console.error(err);});
}
}
function downloadCli() {
console.log("Downloading JFrog CLI " + version );
var startUrl = 'https://api.bintray.com/content/jfrog/jfrog-cli-go/' + version + '/' + btPackage + '/' + fileName + '?bt_package=' + btPackage;
redirectDetectDownload(startUrl);
}
function isValidNpmVersion() {
var child_process = require('child_process');
var npmVersionCmdOut = child_process.execSync("npm version -json");
var npmVersion = JSON.parse(npmVersionCmdOut).npm;
// Supported since version 5.0.0
return parseInt(npmVersion.charAt(0)) > 4
}
function writeToFile(response) {
var file = fs.createWriteStream(filePath);
response.on('data', function (chunk) {
file.write(chunk);
}).on('end', function () {
file.end();
if (!process.platform.startsWith("win")) {
fs.chmodSync(filePath, 7775)
}
}).on('error', function (err) {
console.error(err);
});
}
function getArchitecture() {
var platform = process.platform;
if (platform.startsWith("win")) {
return "windows-amd64"
}
if (platform.includes("darwin")) {
return "mac-386"
}
if (process.arch.includes("64")) {
return "linux-amd64"
}
return "linux-386"
}
function getFileName() {
var excecutable = "jfrog";
if (process.platform.startsWith("win")) {
excecutable += ".exe"
}
return excecutable
}