Skip to content

Commit 78a49c2

Browse files
author
Alvaro Madrid
committed
feat(downloadJar): all console.log only if logLevel != undefined
1 parent 119bf79 commit 78a49c2

File tree

1 file changed

+74
-59
lines changed

1 file changed

+74
-59
lines changed

downloadJar.js

+74-59
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,84 @@
88

99
(function () {
1010
"use strict";
11-
12-
function downloadJar(version, artifactoryHost, artifactoryPath) {
13-
var Q = require('q');
14-
var deferred = Q.defer();
15-
var https = require('follow-redirects').https;
16-
var fs = require('fs');
17-
var glob = require('glob');
18-
var dest = 'mockserver-netty-' + version + '-jar-with-dependencies.jar';
19-
var snapshot = version.indexOf("SNAPSHOT") !== -1;
20-
var options = {
21-
host: artifactoryHost,
22-
path: artifactoryPath && !snapshot ?
23-
artifactoryPath + version + "/mockserver-netty-" + version + "-jar-with-dependencies.jar" :
24-
"/service/local/artifact/maven/redirect?r=" + (snapshot ? "snapshots" : "releases") + "&g=org.mock-server&a=mockserver-netty&c=jar-with-dependencies&v=" + version,
25-
port: 443
26-
};
27-
28-
var currentMockServerJars = glob.sync('**/mockserver-netty-*-jar-with-dependencies.jar');
29-
currentMockServerJars.forEach(function (item) {
30-
if (item.indexOf(dest) === -1 || snapshot) {
31-
fs.unlinkSync(item);
32-
console.log('Deleted old version ' + item);
33-
}
11+
12+
function downloadJar(version, artifactoryHost, artifactoryPath, logLevel) {
13+
var Q = require('q');
14+
var deferred = Q.defer();
15+
var https = require('follow-redirects').https;
16+
var fs = require('fs');
17+
var glob = require('glob');
18+
var dest = 'mockserver-netty-' + version + '-jar-with-dependencies.jar';
19+
var snapshot = version.indexOf("SNAPSHOT") !== -1;
20+
var options = {
21+
host: artifactoryHost,
22+
path: artifactoryPath && !snapshot ?
23+
artifactoryPath + version + "/mockserver-netty-" + version + "-jar-with-dependencies.jar" :
24+
"/service/local/artifact/maven/redirect?r=" + (snapshot ? "snapshots" : "releases") + "&g=org.mock-server&a=mockserver-netty&c=jar-with-dependencies&v=" + version,
25+
port: 443
26+
};
27+
28+
var currentMockServerJars = glob.sync('**/mockserver-netty-*-jar-with-dependencies.jar');
29+
currentMockServerJars.forEach(function (item) {
30+
if (item.indexOf(dest) === -1 || snapshot) {
31+
fs.unlinkSync(item);
32+
if (logLevel) {
33+
console.log('Deleted old version ' + item);
34+
}
35+
}
36+
});
37+
38+
if (glob.sync('**/' + dest).length === 0) {
39+
if (logLevel) {
40+
console.log('Fetching ' + JSON.stringify(options, null, 2));
41+
}
42+
var req = https.request(options);
43+
44+
req.once('error', function (error) {
45+
if (logLevel) {
46+
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error);
47+
}
48+
deferred.reject(new Error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error));
3449
});
35-
36-
if (glob.sync('**/' + dest).length === 0) {
37-
console.log('Fetching ' + JSON.stringify(options, null, 2));
38-
var req = https.request(options);
39-
40-
req.once('error', function (error) {
41-
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error);
42-
deferred.reject(new Error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error));
50+
51+
req.once('response', function (res) {
52+
if (res.statusCode < 200 || res.statusCode >= 300) {
53+
if (logLevel) {
54+
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with HTTP status code ' + res.statusCode);
55+
}
56+
deferred.reject(new Error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with HTTP status code ' + res.statusCode));
57+
} else {
58+
var writeStream = fs.createWriteStream(dest);
59+
res.pipe(writeStream);
60+
61+
writeStream.on('error', function (error) {
62+
if (logLevel) {
63+
console.error('Saving ' + dest + ' failed with error ' + error);
64+
}
65+
deferred.reject(new Error('Saving ' + dest + ' failed with error ' + error));
4366
});
44-
45-
req.once('response', function (res) {
46-
if (res.statusCode < 200 || res.statusCode >= 300) {
47-
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with HTTP status code ' + res.statusCode);
48-
deferred.reject(new Error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with HTTP status code ' + res.statusCode));
49-
} else {
50-
var writeStream = fs.createWriteStream(dest);
51-
res.pipe(writeStream);
52-
53-
writeStream.on('error', function (error) {
54-
console.error('Saving ' + dest + ' failed with error ' + error);
55-
deferred.reject(new Error('Saving ' + dest + ' failed with error ' + error));
56-
});
57-
writeStream.on('close', function () {
58-
console.log('Saved ' + dest + ' from ' + JSON.stringify(options, null, 2));
59-
deferred.resolve();
60-
});
61-
}
67+
writeStream.on('close', function () {
68+
if (logLevel) {
69+
console.log('Saved ' + dest + ' from ' + JSON.stringify(options, null, 2));
70+
}
71+
deferred.resolve();
6272
});
63-
64-
req.end();
65-
} else {
66-
console.log('Skipping ' + JSON.stringify(options, null, 2) + ' as file already downloaded');
67-
deferred.resolve();
73+
}
74+
});
75+
76+
req.end();
77+
} else {
78+
if (logLevel) {
79+
console.log('Skipping ' + JSON.stringify(options, null, 2) + ' as file already downloaded');
6880
}
69-
70-
return deferred.promise;
81+
deferred.resolve();
82+
}
83+
84+
return deferred.promise;
7185
}
72-
86+
7387
module.exports = {
74-
downloadJar: downloadJar
88+
downloadJar: downloadJar
7589
};
76-
})();
90+
})();
91+

0 commit comments

Comments
 (0)