Skip to content

Commit ebc2603

Browse files
committed
chore: convert launcher to async/await and remove bluebird
1 parent 1b1de36 commit ebc2603

File tree

9 files changed

+200
-172
lines changed

9 files changed

+200
-172
lines changed

packages/launcher/lib/browsers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Debug from 'debug'
22
import type * as cp from 'child_process'
3-
import { utils } from './utils'
3+
import { spawnWithArch } from './utils'
44
import type { FoundBrowser } from '@packages/types'
55
import type { Readable } from 'stream'
66

@@ -36,7 +36,7 @@ export function launch (
3636

3737
debug('spawning browser with opts %o', { browser, url, spawnOpts })
3838

39-
const proc = utils.spawnWithArch(browser.path, args, spawnOpts)
39+
const proc = spawnWithArch(browser.path, args, spawnOpts)
4040

4141
proc.stdout.on('data', (buf) => {
4242
debug('%s stdout: %s', browser.name, String(buf).trim())

packages/launcher/lib/darwin/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const getVersionNumber = linuxHelper.getVersionNumber
103103

104104
export const getPathData = linuxHelper.getPathData
105105

106-
export function detect (browser: Browser): Promise<DetectedBrowser> {
106+
export async function detect (browser: Browser): Promise<DetectedBrowser> {
107107
let findAppParams = get(browsers, [browser.name, browser.channel])
108108

109109
if (!findAppParams) {
@@ -113,11 +113,13 @@ export function detect (browser: Browser): Promise<DetectedBrowser> {
113113
return linuxHelper.detect(browser)
114114
}
115115

116-
return findApp(findAppParams)
117-
.then((val) => ({ name: browser.name, ...val }))
118-
.catch((err) => {
116+
try {
117+
const val = await findApp(findAppParams)
118+
119+
return { name: browser.name, ...val }
120+
} catch (err) {
119121
debugVerbose('could not detect %s using findApp %o, falling back to linux detection method', browser.name, err)
120122

121123
return linuxHelper.detect(browser)
122-
})
124+
}
123125
}

packages/launcher/lib/darwin/util.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Debug from 'debug'
22
import { notInstalledErr } from '../errors'
3-
import { utils } from '../utils'
3+
import execa from 'execa'
44
import fs from 'fs-extra'
55
import path from 'path'
66
import plist from 'plist'
77

88
const debugVerbose = Debug('cypress-verbose:launcher:darwin:util')
99

1010
/** parses Info.plist file from given application and returns a property */
11-
export function parsePlist (p: string, property: string): Promise<string> {
11+
export async function parsePlist (p: string, property: string): Promise<string> {
1212
const pl = path.join(p, 'Contents', 'Info.plist')
1313

1414
debugVerbose('reading property file "%s"', pl)
@@ -21,16 +21,18 @@ export function parsePlist (p: string, property: string): Promise<string> {
2121
throw notInstalledErr('', msg)
2222
}
2323

24-
return fs
25-
.readFile(pl, 'utf8')
26-
.then(plist.parse)
27-
.then((val) => val[property])
28-
.then(String) // explicitly convert value to String type
29-
.catch(failed) // to make TS compiler happy
24+
try {
25+
const file = await fs.readFile(pl, 'utf8')
26+
const val = plist.parse(file)
27+
28+
return String(val[property]) // explicitly convert value to String type
29+
} catch (err) {
30+
return failed(err) // to make TS compiler happy
31+
}
3032
}
3133

3234
/** uses mdfind to find app using Ma app id like 'com.google.Chrome.canary' */
33-
export function mdfind (id: string): Promise<string> {
35+
export async function mdfind (id: string): Promise<string> {
3436
const cmd = `mdfind 'kMDItemCFBundleIdentifier=="${id}"' | head -1`
3537

3638
debugVerbose('looking for bundle id %s using command: %s', id, cmd)
@@ -46,16 +48,15 @@ export function mdfind (id: string): Promise<string> {
4648
throw notInstalledErr(id)
4749
}
4850

49-
return utils.execa(cmd)
50-
.then((val) => {
51-
return val.stdout
52-
})
53-
.then((val) => {
54-
logFound(val)
51+
try {
52+
const val = await execa(cmd)
5553

56-
return val
57-
})
58-
.catch(failedToFind)
54+
logFound(val.stdout)
55+
56+
return val.stdout
57+
} catch (err) {
58+
return failedToFind()
59+
}
5960
}
6061

6162
export type AppInfo = {
@@ -79,22 +80,24 @@ function formApplicationPath (appName: string) {
7980
}
8081

8182
/** finds an application and its version */
82-
export function findApp ({ appName, executable, bundleId, versionProperty }: FindAppParams): Promise<AppInfo> {
83+
export async function findApp ({ appName, executable, bundleId, versionProperty }: FindAppParams): Promise<AppInfo> {
8384
debugVerbose('looking for app %s bundle id %s', executable, bundleId)
8485

85-
const findVersion = (foundPath: string) => {
86-
return parsePlist(foundPath, versionProperty).then((version) => {
87-
debugVerbose('got plist: %o', { foundPath, version })
86+
const findVersion = async (foundPath: string) => {
87+
const version = await parsePlist(foundPath, versionProperty)
88+
89+
debugVerbose('got plist: %o', { foundPath, version })
8890

89-
return {
90-
path: path.join(foundPath, executable),
91-
version,
92-
}
93-
})
91+
return {
92+
path: path.join(foundPath, executable),
93+
version,
94+
}
9495
}
9596

96-
const tryMdFind = () => {
97-
return mdfind(bundleId).then(findVersion)
97+
const tryMdFind = async () => {
98+
const foundPath = await mdfind(bundleId)
99+
100+
return findVersion(foundPath)
98101
}
99102

100103
const tryFullApplicationFind = () => {
@@ -105,5 +108,11 @@ export function findApp ({ appName, executable, bundleId, versionProperty }: Fin
105108
return findVersion(applicationPath)
106109
}
107110

108-
return tryMdFind().catch(tryFullApplicationFind)
111+
try {
112+
const val = await tryMdFind()
113+
114+
return val
115+
} catch (err) {
116+
return tryFullApplicationFind()
117+
}
109118
}

packages/launcher/lib/detect.ts

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Bluebird from 'bluebird'
21
import _, { compact, extend, find } from 'lodash'
32
import os from 'os'
43
import { 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

Comments
 (0)