33
44const fs = require ( 'fs' ) ;
55const helper = require ( './lib/chromedriver' ) ;
6- const axios = require ( 'axios' ) . default ;
6+ const axios = require ( 'axios' ) ;
77const path = require ( 'path' ) ;
88const child_process = require ( 'child_process' ) ;
99const os = require ( 'os' ) ;
@@ -44,9 +44,12 @@ let platform = '';
4444 // Refer http://chromedriver.chromium.org/downloads/version-selection
4545 const chromeVersion = await getChromeVersion ( include_chromium ) ;
4646 console . log ( "Your Chrome version is " + chromeVersion ) ;
47- const chromeVersionWithoutPatch = / ^ ( .* ?) \. \d + $ / . exec ( chromeVersion ) [ 1 ] ;
48- await getChromeDriverVersion ( getRequestOptions ( cdnUrl + '/LATEST_RELEASE_' + chromeVersionWithoutPatch ) ) ;
49- console . log ( "Compatible ChromeDriver version is " + chromedriver_version ) ;
47+ const versionMatch = / ^ ( .* ?) \. \d + $ / . exec ( chromeVersion ) ;
48+ if ( versionMatch ) {
49+ const chromeVersionWithoutPatch = versionMatch [ 1 ] ;
50+ await getChromeDriverVersion ( getRequestOptions ( cdnUrl + '/LATEST_RELEASE_' + chromeVersionWithoutPatch ) ) ;
51+ console . log ( "Compatible ChromeDriver version is " + chromedriver_version ) ;
52+ }
5053 }
5154 if ( chromedriver_version === 'LATEST' ) {
5255 await getChromeDriverVersion ( getRequestOptions ( `${ cdnUrl } /LATEST_RELEASE` ) ) ;
@@ -162,11 +165,10 @@ function findSuitableTempDirectory() {
162165 path . join ( process . cwd ( ) , 'tmp' )
163166 ] ;
164167
165- for ( let i = 0 ; i < candidateTmpDirs . length ; i ++ ) {
166- if ( ! candidateTmpDirs [ i ] ) continue ;
167- // Prevent collision with other versions in the dependency tree
168+ for ( const tempDir of candidateTmpDirs ) {
169+ if ( ! tempDir ) continue ;
168170 const namespace = chromedriver_version ;
169- const candidatePath = path . join ( candidateTmpDirs [ i ] , namespace , 'chromedriver' ) ;
171+ const candidatePath = path . join ( tempDir , namespace , 'chromedriver' ) ;
170172 try {
171173 fs . mkdirSync ( candidatePath , { recursive : true } ) ;
172174 const testFile = path . join ( candidatePath , now + '.tmp' ) ;
@@ -190,11 +192,12 @@ function getRequestOptions(downloadPath) {
190192
191193 if ( proxyUrl ) {
192194 const proxyUrlParts = url . parse ( proxyUrl ) ;
193- options . proxy = {
194- host : proxyUrlParts . hostname ,
195- port : proxyUrlParts . port ? parseInt ( proxyUrlParts . port ) : 80 ,
196- protocol : proxyUrlParts . protocol
197- } ;
195+ if ( proxyUrlParts . hostname && proxyUrlParts . protocol )
196+ options . proxy = {
197+ host : proxyUrlParts . hostname ,
198+ port : proxyUrlParts . port ? parseInt ( proxyUrlParts . port ) : 80 ,
199+ protocol : proxyUrlParts . protocol
200+ } ;
198201 }
199202
200203 if ( isHttps ) {
@@ -244,7 +247,7 @@ function getRequestOptions(downloadPath) {
244247 */
245248async function getChromeDriverVersion ( requestOptions ) {
246249 console . log ( 'Finding Chromedriver version.' ) ;
247- const response = await axios ( requestOptions ) ;
250+ const response = await axios . request ( requestOptions ) ;
248251 chromedriver_version = response . data . trim ( ) ;
249252 console . log ( `Chromedriver version is ${ chromedriver_version } .` ) ;
250253}
@@ -258,7 +261,7 @@ async function requestBinary(requestOptions, filePath) {
258261 const outFile = fs . createWriteStream ( filePath ) ;
259262 let response ;
260263 try {
261- response = await axios ( { responseType : 'stream' , ...requestOptions } ) ;
264+ response = await axios . request ( { responseType : 'stream' , ...requestOptions } ) ;
262265 } catch ( error ) {
263266 if ( error && error . response ) {
264267 if ( error . response . status )
@@ -315,14 +318,14 @@ async function copyIntoPlace(originPath, targetPath) {
315318 . filter ( dirent => dirent . isFile ( ) && dirent . name . startsWith ( 'chromedriver' ) && ! dirent . name . endsWith ( ".debug" ) && ! dirent . name . endsWith ( ".zip" ) )
316319 . map ( dirent => dirent . name ) ;
317320 const promises = files . map ( name => {
318- return new Promise ( ( resolve ) => {
321+ return /** @type { Promise<void> } */ ( new Promise ( ( resolve ) => {
319322 const file = path . join ( originPath , name ) ;
320323 const reader = fs . createReadStream ( file ) ;
321324 const targetFile = path . join ( targetPath , name ) ;
322325 const writer = fs . createWriteStream ( targetFile ) ;
323326 writer . on ( "close" , ( ) => resolve ( ) ) ;
324327 reader . pipe ( writer ) ;
325- } ) ;
328+ } ) ) ;
326329 } ) ;
327330 await Promise . all ( promises ) ;
328331}
0 commit comments