forked from gitter-badger/isptools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manutencao.js
169 lines (157 loc) · 5.54 KB
/
manutencao.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var sys = require('sys');
var http = require('http');
var colors = require('colors');
var exec = require('child_process').exec;
module.exports = {
atualizar: function () {
http.get('http://www.isptools.com.br/ok/version/?nocache='+(new Date()).getTime(), function (e) {
e.on('data', function (body) {
if(version_compare(body,global.version)>0) {
global.updated = false;
console.log(' '.inverse.yellow);
console.log(' Necessário ATUALIZAR '.inverse.yellow);
console.log(' '.inverse.yellow);
console.log('');
console.log('Versão Atual: '+global.version);
console.log('Nova Versão: '+body);
console.log('');
console.log('Instruções em:');
console.log('http://www.isptools.com.br/atualizar'.cyan);
console.log('------------------------------------------------------------------'.yellow);
executa('sudo at -f upgrade.sh -v now + 1 minutes');
exec('echo "\n\n+-----------------------------------------+\n| ISP Tools DESATUALIZADO. |\n| |\n| Acesse: |\n| http://www.isptools.com.br/atualizar |\n+-----------------------------------------+\n\n " | wall');
}
});
});
return true;
}
};
function executa(comando) {
exec(comando, function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
/**
* Compara versão
* @param {[type]} v1 Versão 1
* @param {[type]} v2 Versão 2
* @param {[type]} operator Operador de comparação
*/
function version_compare(v1, v2, operator) {
// discuss at: http://phpjs.org/functions/version_compare/
// original by: Philippe Jausions (http://pear.php.net/user/jausions)
// original by: Aidan Lister (http://aidanlister.com/)
// reimplemented by: Kankrelune (http://www.webfaktory.info/)
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Scott Baker
// improved by: Theriault
// example 1: version_compare('8.2.5rc', '8.2.5a');
// returns 1: 1
// example 2: version_compare('8.2.50', '8.2.52', '<');
// returns 2: true
// example 3: version_compare('5.3.0-dev', '5.3.0');
// returns 3: -1
// example 4: version_compare('4.1.0.52','4.01.0.51');
// returns 4: 1
this.php_js = this.php_js || {};
this.php_js.ENV = this.php_js.ENV || {};
// END REDUNDANT
// Important: compare must be initialized at 0.
var i = 0,
x = 0,
compare = 0,
// vm maps textual PHP versions to negatives so they're less than 0.
// PHP currently defines these as CASE-SENSITIVE. It is important to
// leave these as negatives so that they can come before numerical versions
// and as if no letters were there to begin with.
// (1alpha is < 1 and < 1.1 but > 1dev1)
// If a non-numerical value can't be mapped to this table, it receives
// -7 as its value.
vm = {
'dev': -6,
'alpha': -5,
'a': -5,
'beta': -4,
'b': -4,
'RC': -3,
'rc': -3,
'#': -2,
'p': 1,
'pl': 1
},
// This function will be called to prepare each version argument.
// It replaces every _, -, and + with a dot.
// It surrounds any nonsequence of numbers/dots with dots.
// It replaces sequences of dots with a single dot.
// version_compare('4..0', '4.0') == 0
// Important: A string of 0 length needs to be converted into a value
// even less than an unexisting value in vm (-7), hence [-8].
// It's also important to not strip spaces because of this.
// version_compare('', ' ') == 1
prepVersion = function(v) {
v = ('' + v)
.replace(/[_\-+]/g, '.');
v = v.replace(/([^.\d]+)/g, '.$1.')
.replace(/\.{2,}/g, '.');
return (!v.length ? [-8] : v.split('.'));
};
// This converts a version component to a number.
// Empty component becomes 0.
// Non-numerical component becomes a negative number.
// Numerical component becomes itself as an integer.
numVersion = function(v) {
return !v ? 0 : (isNaN(v) ? vm[v] || -7 : parseInt(v, 10));
};
v1 = prepVersion(v1);
v2 = prepVersion(v2);
x = Math.max(v1.length, v2.length);
for (i = 0; i < x; i++) {
if (v1[i] == v2[i]) {
continue;
}
v1[i] = numVersion(v1[i]);
v2[i] = numVersion(v2[i]);
if (v1[i] < v2[i]) {
compare = -1;
break;
} else if (v1[i] > v2[i]) {
compare = 1;
break;
}
}
if (!operator) {
return compare;
}
// Important: operator is CASE-SENSITIVE.
// "No operator" seems to be treated as "<."
// Any other values seem to make the function return null.
switch (operator) {
case '>':
case 'gt':
return (compare > 0);
case '>=':
case 'ge':
return (compare >= 0);
case '<=':
case 'le':
return (compare <= 0);
case '==':
case '=':
case 'eq':
return (compare === 0);
case '<>':
case '!=':
case 'ne':
return (compare !== 0);
case '':
case '<':
case 'lt':
return (compare < 0);
default:
return null;
}
}