1
- import Bluebird from 'bluebird'
2
1
import _ , { compact , extend , find } from 'lodash'
3
2
import os from 'os'
4
3
import { removeDuplicateBrowsers } from '@packages/data-context/src/sources/BrowserDataSource'
@@ -94,17 +93,19 @@ function lookup (
94
93
* one for each binary. If Windows is detected, only one `checkOneBrowser` will be called, because
95
94
* we don't use the `binary` field on Windows.
96
95
*/
97
- function checkBrowser ( browser : Browser ) : Bluebird < ( boolean | HasVersion ) [ ] > {
96
+ async function checkBrowser ( browser : Browser ) : Promise < ( boolean | HasVersion ) [ ] > {
98
97
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
102
101
}
103
102
104
- return Bluebird . map ( [ browser ] , checkOneBrowser )
103
+ const checkedBrowsers = await checkOneBrowser ( browser )
104
+
105
+ return [ checkedBrowsers ]
105
106
}
106
107
107
- function checkOneBrowser ( browser : Browser ) : Promise < boolean | HasVersion > {
108
+ async function checkOneBrowser ( browser : Browser ) : Promise < boolean | HasVersion > {
108
109
const platform = os . platform ( )
109
110
const pickBrowserProps = [
110
111
'name' ,
@@ -131,21 +132,25 @@ function checkOneBrowser (browser: Browser): Promise<boolean | HasVersion> {
131
132
throw err
132
133
}
133
134
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
+
138
142
foundBrowser . majorVersion = getMajorVersion ( foundBrowser . version )
139
143
140
144
validateCypressSupport ( browser . validator , foundBrowser , platform )
141
145
142
146
return foundBrowser
143
- } )
144
- . catch ( failed )
147
+ } catch ( error ) {
148
+ return failed ( error as NotInstalledError )
149
+ }
145
150
}
146
151
147
152
/** returns list of detected browsers */
148
- export const detect = ( goalBrowsers ?: Browser [ ] ) : Bluebird < FoundBrowser [ ] > => {
153
+ export const detect = async ( goalBrowsers ?: Browser [ ] ) : Promise < FoundBrowser [ ] > => {
149
154
// we can detect same browser under different aliases
150
155
// tell them apart by the name and the version property
151
156
if ( ! goalBrowsers ) {
@@ -158,13 +163,27 @@ export const detect = (goalBrowsers?: Browser[]): Bluebird<FoundBrowser[]> => {
158
163
159
164
debug ( 'detecting if the following browsers are present %o' , goalBrowsers )
160
165
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
165
184
}
166
185
167
- export const detectByPath = (
186
+ export const detectByPath = async (
168
187
path : string ,
169
188
goalBrowsers ?: Browser [ ] ,
170
189
) : Promise < FoundBrowser > => {
@@ -210,8 +229,9 @@ export const detectByPath = (
210
229
211
230
const pathData = helper . getPathData ( path )
212
231
213
- return helper . getVersionString ( pathData . path )
214
- . then ( ( version ) => {
232
+ try {
233
+ const version = await helper . getVersionString ( pathData . path )
234
+
215
235
let browser
216
236
217
237
if ( pathData . browserKey ) {
@@ -227,12 +247,11 @@ export const detectByPath = (
227
247
}
228
248
229
249
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
234
253
}
235
254
236
- throw notDetectedAtPathErr ( err . message )
237
- } )
255
+ throw notDetectedAtPathErr ( error . message )
256
+ }
238
257
}
0 commit comments