1- import Bluebird from 'bluebird'
21import _ , { compact , extend , find } from 'lodash'
32import os from 'os'
43import { removeDuplicateBrowsers } from '@packages/data-context/src/sources/BrowserDataSource'
@@ -94,17 +93,19 @@ function lookup (
9493 * one for each binary. If Windows is detected, only one `checkOneBrowser` will be called, because
9594 * we don't use the `binary` field on Windows.
9695 */
97- function checkBrowser ( browser : Browser ) : Bluebird < ( boolean | HasVersion ) [ ] > {
96+ async function checkBrowser ( browser : Browser ) : Promise < ( boolean | HasVersion ) [ ] > {
9897 if ( Array . isArray ( browser . binary ) && os . platform ( ) !== 'win32' ) {
99- return Bluebird . map ( browser . binary , ( binary : string ) => {
100- return checkOneBrowser ( extend ( { } , browser , { binary } ) )
101- } )
98+ const checkedBrowsers = await Promise . all ( browser . binary . map ( ( binary ) => checkOneBrowser ( extend ( { } , browser , { binary } ) ) ) )
99+
100+ return checkedBrowsers
102101 }
103102
104- return Bluebird . map ( [ browser ] , checkOneBrowser )
103+ const checkedBrowsers = await checkOneBrowser ( browser )
104+
105+ return [ checkedBrowsers ]
105106}
106107
107- function checkOneBrowser ( browser : Browser ) : Promise < boolean | HasVersion > {
108+ async function checkOneBrowser ( browser : Browser ) : Promise < boolean | HasVersion > {
108109 const platform = os . platform ( )
109110 const pickBrowserProps = [
110111 'name' ,
@@ -131,21 +132,25 @@ function checkOneBrowser (browser: Browser): Promise<boolean | HasVersion> {
131132 throw err
132133 }
133134
134- return lookup ( platform , browser )
135- . then ( ( val ) => ( { ...browser , ...val } ) )
136- . then ( ( val ) => _ . pick ( val , pickBrowserProps ) as FoundBrowser )
137- . then ( ( foundBrowser ) => {
135+ try {
136+ const detectedBrowser = await lookup ( platform , browser )
137+
138+ const browserWithDetected = { ...browser , ...detectedBrowser }
139+
140+ const foundBrowser = _ . pick ( browserWithDetected , pickBrowserProps ) as FoundBrowser
141+
138142 foundBrowser . majorVersion = getMajorVersion ( foundBrowser . version )
139143
140144 validateCypressSupport ( browser . validator , foundBrowser , platform )
141145
142146 return foundBrowser
143- } )
144- . catch ( failed )
147+ } catch ( error ) {
148+ return failed ( error as NotInstalledError )
149+ }
145150}
146151
147152/** returns list of detected browsers */
148- export const detect = ( goalBrowsers ?: Browser [ ] ) : Bluebird < FoundBrowser [ ] > => {
153+ export const detect = async ( goalBrowsers ?: Browser [ ] ) : Promise < FoundBrowser [ ] > => {
149154 // we can detect same browser under different aliases
150155 // tell them apart by the name and the version property
151156 if ( ! goalBrowsers ) {
@@ -158,13 +163,27 @@ export const detect = (goalBrowsers?: Browser[]): Bluebird<FoundBrowser[]> => {
158163
159164 debug ( 'detecting if the following browsers are present %o' , goalBrowsers )
160165
161- return Bluebird . mapSeries ( goalBrowsers , checkBrowser )
162- . then ( ( val ) => _ . flatten ( val ) )
163- . then ( compactFalse )
164- . then ( removeDuplicateBrowsers )
166+ let foundBrowsers : FoundBrowser [ ] = [ ]
167+
168+ {
169+ const hasVersionOrFalse : ( boolean | HasVersion ) [ ] [ ] = [ ]
170+
171+ for ( const browser of goalBrowsers ) {
172+ const browserOrFalse = await checkBrowser ( browser )
173+
174+ hasVersionOrFalse . push ( browserOrFalse )
175+ }
176+
177+ const flattenedFoundBrowsers = _ . flatten ( hasVersionOrFalse )
178+ const compactedFoundBrowsers = compactFalse ( flattenedFoundBrowsers )
179+
180+ foundBrowsers = removeDuplicateBrowsers ( compactedFoundBrowsers )
181+ }
182+
183+ return foundBrowsers
165184}
166185
167- export const detectByPath = (
186+ export const detectByPath = async (
168187 path : string ,
169188 goalBrowsers ?: Browser [ ] ,
170189) : Promise < FoundBrowser > => {
@@ -210,8 +229,9 @@ export const detectByPath = (
210229
211230 const pathData = helper . getPathData ( path )
212231
213- return helper . getVersionString ( pathData . path )
214- . then ( ( version ) => {
232+ try {
233+ const version = await helper . getVersionString ( pathData . path )
234+
215235 let browser
216236
217237 if ( pathData . browserKey ) {
@@ -227,12 +247,11 @@ export const detectByPath = (
227247 }
228248
229249 return setCustomBrowserData ( browser , pathData . path , version )
230- } )
231- . catch ( ( err : NotDetectedAtPathError ) => {
232- if ( err . notDetectedAtPath ) {
233- throw err
250+ } catch ( error : any ) {
251+ if ( error . notDetectedAtPath ) {
252+ throw error as NotDetectedAtPathError
234253 }
235254
236- throw notDetectedAtPathErr ( err . message )
237- } )
255+ throw notDetectedAtPathErr ( error . message )
256+ }
238257}
0 commit comments