-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
173 lines (145 loc) · 4 KB
/
index.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
170
171
172
173
#!/usr/bin/env node
'use strict';
const child_process = require('child_process'),
path = require('path'),
fetch = require('node-fetch'),
unzipper = require('unzipper'),
fs = require('fs-extra'),
argv = require('yargs').argv,
unparse = require('yargs-unparser'),
HttpsProxyAgent = require('https-proxy-agent');
var fetchOptions = {};
var httpProxy = (process.env.http_proxy || process.env.https_proxy || process.env.HTTP_PROXY
|| process.env.HTTPS_PROXY || '').trim();
if (httpProxy) {
fetchOptions = { agent: new HttpsProxyAgent(httpProxy)}
}
var SONAR_VERSION = argv.t || argv.target || null;
delete argv.t;
delete argv.target;
async function getTags(page){
try{
var res = await fetch("https://api.github.com/repos/SonarSource/sonar-scanner-cli/tags"
+"?page="+page, fetchOptions)
var data = await res.json();
} catch (err) {
console.error(err);
throw new Error("Couldn't get tags from Github API.");
}
if (data.length == 0){
throw new Error("Couldn't find target sonar-scanner-cli version: "
+ SONAR_VERSION);
}
if (!SONAR_VERSION){
SONAR_VERSION = data[0].name;
}
var gotcha = data.some(element => {
return checkEqualVersion(element.name);
});
if(gotcha){
console.log("Found ", SONAR_VERSION, ". Start download CLI now...");
if (!isAlreadyInstalled()) {
const buffer = await getBinary();
console.log('download complete. extract binary...');
await unzip(buffer);
cleanDirectory();
console.log('install complete.');
} else {
console.log("already installed cli. skip download...")
}
changePermission();
} else {
getTags(++page);
}
}
var isAlreadyInstalled = function(){
return fs.existsSync(path.join(__dirname, 'lib'
, 'sonar-scanner-cli-' + SONAR_VERSION + '.jar'));
}
async function getBinary(){
try{
var res = await fetch("https://binaries.sonarsource.com/Distribution/"
+ "sonar-scanner-cli/sonar-scanner-cli-"
+ SONAR_VERSION + ".zip", fetchOptions);
return await res.buffer();
}catch(err){
console.error(err);
throw new Error('sonar-scanner-cli download failed.');
}
}
async function unzip(buffer) {
try {
var d = await unzipper.Open.buffer(buffer);
await d.extract({path: path.join(__dirname)});
} catch(err){
console.error(err);
throw new Error("couldn't extract binary.")
}
}
var cleanDirectory = function(){
const prefix = 'sonar-scanner-';
const newBinPath = path.join(__dirname, 'bin');
const newLibPath = path.join(__dirname, 'lib');
fs.removeSync(newBinPath);
fs.removeSync(newLibPath);
try{
fs.moveSync(
path.join(__dirname, prefix + SONAR_VERSION, 'bin')
, newBinPath);
fs.moveSync(
path.join(__dirname, prefix + SONAR_VERSION, 'lib')
, newLibPath);
} catch(e){
console.error(e);
throw new Error("Couldn't move files. please check logs.");
}
fs.removeSync(path.join(__dirname, prefix + SONAR_VERSION));
return;
}
var checkEqualVersion = function(name){
if (name == SONAR_VERSION){
return true;
}
return false;
}
var changePermission = function(){
if (process.platform === 'win32') {
runAfterInstall();
return;
}
var script = path.join(__dirname, 'bin', 'sonar-scanner'),
command = 'chmod',
args = ['+x', script];
var child = child_process.spawn(command, args, {
stdio: 'inherit'
});
child.on('close', function(code) {
runAfterInstall();
});
}
var runAfterInstall = function(){
var args = unparse(argv),
script = path.join(__dirname, 'bin', 'sonar-scanner'),
command;
if (process.platform === 'win32') {
command = 'cmd.exe';
args = ['/c', (script + '.bat')].concat(args);
}
else {
command = script;
}
var child = child_process.spawn(command, args, {
stdio: 'inherit'
});
child.on('close', function(code) {
console.log('sonar-scanner install complete.');
process.exit(code);
});
child.on('error', function(err){
console.error(err);
});
}
getTags(1).catch((err) => {
console.error(err);
process.exit(1);
});