This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
119 lines (102 loc) · 4.01 KB
/
utils.js
File metadata and controls
119 lines (102 loc) · 4.01 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var request = require('request');
var util = require('util');
// create logger if it's not in scope
if (!global.log){
var Logger = require('./log')
global.log = new Logger('TRACE')
}
function versCmp(x, y){
// like strcmp:
// returns -1 if x < y
// returns 1 if x > y
// returns 0 if versions are equal
// split on dot
x = x.split('.');
y = y.split('.');
// only check terms they share
// e.g. when comparing 1.2.3 vs 1.2, don't check the third term on 1.2.3
terms = Math.min(x.length, y.length);
for (var i=0; i < terms; i++){
if ( parseInt(x[i]) < parseInt(y[i]) ) { return -1 };
if ( parseInt(x[i]) > parseInt(y[i]) ) { return 1 };
}
// if all terms match, we call the version with more terms the 'greater' one
// e.g. compareVersions('1.2.3', '1.2') === 1
if ( x.length < y.length ) { return -1 };
if ( x.length > y.length ) { return 1 };
// otherwise, versions match
return 0;
}
function getConManVersion(env, cb){
// request.get('https://crossbrowsertesting.com/api/v3/localconman/version', (err, resp, body) => {
var urls = {
'local': 'http://localhost:3000',
'test': 'http://test.crossbrowsertesting.com',
'qa' : 'http://qaapp.crossbrowsertesting.com',
'prod': 'http://livetestdirect.crossbrowsertesting.com'
}
request.get(urls[env] + '/api/v3/localconman/version', (err, resp, body) => {
if (err){ return cb(err) };
if (resp.statusCode !== 200){ return cb(new Error(body))};
try {
version = JSON.parse(body);
} catch (ex) {
cb(ex);
}
cb(null, version);
})
}
function getProxyFromEnv(){
let proxyEnvVars = [
"http_proxy",
"HTTP_PROXY",
"https_proxy",
"HTTPS_PROXY" ];
proxyEnvVars = proxyEnvVars.filter( (varName, index, arr) => {
let proxyUrl = process.env[varName];
if (proxyUrl && proxyUrl.indexOf('http') !== 0){
// request module wants env var to have
process.env[varName] = 'http://' + proxyUrl;
}
return proxyUrl;
})
let proxies = proxyEnvVars.map( varName => {
return process.env[varName];
})
if (proxies.length > 0){
log.trace(`Proxies specified: ${proxies.join(', ')}`);
log.trace(`Using ${proxies[0]}`);
return proxies[0];
} else {
// no proxy specified
return false
}
}
module.exports.getProxyFromEnv = getProxyFromEnv
function checkVersion(env, cb){
var pjson = require('./package.json');
var installedVersion = pjson.version;
log.debug('Checking version...');
version = getConManVersion(env, (err, versionsFromNode) => {
// if the current version is lower than or equal to the blocked version
if (err){
log.error("Error checking for updates. Enterprise Connection Manager may not function properly.");
log.error(err.stack || err)
} else if ( versCmp(installedVersion, versionsFromNode.blockedVersion) <= 0 ) {
log.error("CBT Enterprise Connection Manager is woefully out of date and must be updated!\n");
log.error(`Newest version (${versionsFromNode.currentVersion}) vs installed version (${installedVersion})`);
log.error("Please download the new binary or run 'npm update -g cbt-enterprise-connection-manager'");
process.exit(1);
} else if ( versCmp(installedVersion, versionsFromNode.deprecatedVersion) <= 0 ){
log.warn("CBT Enterprise Connection Manager version is deprecated!");
log.warn(`Newest version (${versionsFromNode.currentVersion}) vs installed version (${installedVersion})`);
log.warn("It will continue to work for now, but we recommend updating soon.");
} else if ( versCmp(installedVersion, versionsFromNode.currentVersion) < 0 ){
log.info("Update available! Please download the new binary or run 'npm update -g cbt-enterprise-connection-manager'");
} else if ( versCmp(installedVersion, versionsFromNode.currentVersion) >= 0 ){
log.info(`Current version (${installedVersion}) is up to date.`);
}
cb()
});
}
module.exports.checkVersion = checkVersion