2
2
3
3
var fs = require ( 'fs' ) ;
4
4
var os = require ( 'os' ) ;
5
+ var url = require ( 'url' ) ;
5
6
var request = require ( 'request' ) ;
6
7
var http = require ( 'http' ) ;
7
8
var path = require ( 'path' ) ;
@@ -76,7 +77,6 @@ var binaries = {
76
77
}
77
78
} ;
78
79
79
-
80
80
var cli = optimist .
81
81
usage ( 'Usage: webdriver-manager <command>\n' +
82
82
'Commands:\n' +
@@ -85,7 +85,8 @@ var cli = optimist.
85
85
' status: list the current available drivers' ) .
86
86
describe ( 'out_dir' , 'Location to output/expect ' ) .
87
87
default ( 'out_dir' , SELENIUM_DIR ) .
88
- describe ( 'seleniumPort' , 'Optional port for the selenium standalone server' ) ;
88
+ describe ( 'seleniumPort' , 'Optional port for the selenium standalone server' ) .
89
+ describe ( 'proxy' , 'proxy to use for the install or update command' ) ;
89
90
90
91
for ( bin in binaries ) {
91
92
cli . describe ( bin , 'install or update ' + binaries [ bin ] . name ) .
@@ -105,6 +106,17 @@ if (!fs.existsSync(argv.out_dir) || !fs.statSync(argv.out_dir).isDirectory()) {
105
106
fs . mkdirSync ( argv . out_dir ) ;
106
107
}
107
108
109
+ var resolveProxy = function ( fileUrl ) {
110
+ var protocol = url . parse ( fileUrl ) . protocol ;
111
+ if ( argv . proxy ) {
112
+ return argv . proxy ;
113
+ } else if ( protocol === 'https:' ) {
114
+ return process . env . HTTPS_PROXY || process . env . HTTP_PROXY ;
115
+ } else if ( protocol === 'http:' ) {
116
+ return process . env . HTTP_PROXY ;
117
+ }
118
+ } ;
119
+
108
120
/**
109
121
* Function to download file using HTTP.get.
110
122
* TODO: look into using something instead of request here, to avoid the
@@ -114,23 +126,20 @@ var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
114
126
console . log ( 'downloading ' + fileUrl + '...' ) ;
115
127
var filePath = path . join ( outputDir , fileName ) ;
116
128
var file = fs . createWriteStream ( filePath ) ;
117
- var req = request ( fileUrl ) ;
118
- req . on ( 'response' , function ( res ) {
119
- if ( res . statusCode !== 200 ) {
120
- throw new Error ( 'Got code ' + res . statusCode + ' from ' + fileUrl ) ;
121
- }
122
- } ) ;
123
- req . on ( 'data' , function ( data ) {
124
- file . write ( data ) ;
125
- } ) ;
126
- req . on ( 'end' , function ( ) {
127
- file . end ( function ( ) {
128
- console . log ( fileName + ' downloaded to ' + filePath ) ;
129
- if ( callback ) {
130
- callback ( filePath ) ;
131
- }
132
- } ) ;
133
- } ) ;
129
+
130
+ var options = {
131
+ url : fileUrl ,
132
+ proxy : resolveProxy ( fileUrl )
133
+ }
134
+ request ( options , function ( error , response ) {
135
+ if ( response . statusCode !== 200 ) {
136
+ throw new Error ( 'Got code ' + response . statusCode + ' from ' + fileUrl ) ;
137
+ }
138
+ console . log ( fileName + ' downloaded to ' + filePath ) ;
139
+ if ( callback ) {
140
+ callback ( filePath ) ;
141
+ }
142
+ } ) . pipe ( file ) ;
134
143
} ;
135
144
136
145
/**
0 commit comments