Skip to content

Commit cbdcae7

Browse files
committed
chore: start refactoring launcher code in server
fix type issues
1 parent ebc2603 commit cbdcae7

File tree

6 files changed

+60
-60
lines changed

6 files changed

+60
-60
lines changed

packages/server/lib/browsers/utils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,21 @@ async function ensureAndGetByNameOrPath (nameOrPath: string, returnAll = false,
344344
// did the user give a bad name, or is this actually a path?
345345
if (isValidPathToBrowser(nameOrPath)) {
346346
// looks like a path - try to resolve it to a FoundBrowser
347-
return launcher.detectByPath(nameOrPath)
348-
.then((browser) => {
347+
try {
348+
const browser = await launcher.detectByPath(nameOrPath)
349+
349350
if (returnAll) {
350351
return [browser].concat(browsers)
351352
}
352353

353354
return browser
354-
}).catch((err) => {
355-
errors.throwErr('BROWSER_NOT_FOUND_BY_PATH', nameOrPath, err.message)
356-
})
355+
} catch (err) {
356+
return errors.throwErr('BROWSER_NOT_FOUND_BY_PATH', nameOrPath, err.message)
357+
}
357358
}
358359

359360
// not a path, not found by name
360-
throwBrowserNotFound(nameOrPath, browsers)
361+
return throwBrowserNotFound(nameOrPath, browsers)
361362
}
362363

363364
const formatBrowsersToOptions = (browsers) => {

packages/server/lib/cypress.ts

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import argsUtils from './util/args'
1616
import { telemetry } from '@packages/telemetry'
1717
import { getCtx, hasCtx } from '@packages/data-context'
1818
import { warning as errorsWarning } from './errors'
19+
import pkg from '@packages/root'
20+
import { info } from './modes/info'
21+
import { toNumber } from 'lodash'
1922

2023
const debug = Debug('cypress:server:cypress')
2124

@@ -206,74 +209,85 @@ export = {
206209
})
207210
},
208211

209-
startInMode (mode: Mode, options: any) {
212+
async startInMode (mode: Mode, options: any) {
210213
debug('starting in mode %s with options %o', mode, options)
211214

212215
switch (mode) {
213216
case 'version':
214-
return require('./modes/pkg')(options)
215-
.get('version')
216-
.then((version: any) => {
217-
return console.log(version) // eslint-disable-line no-console
218-
}).then(exit0)
219-
.catch(exitErr)
217+
try {
218+
console.log(pkg.version)// eslint-disable-line no-console
220219

220+
return exit0()
221+
} catch (err) {
222+
return exitErr(err)
223+
}
221224
case 'info':
222-
return require('./modes/info')(options)
223-
.then(exit0)
224-
.catch(exitErr)
225+
try {
226+
await info()
225227

228+
return exit0()
229+
} catch (err) {
230+
return exitErr(err)
231+
}
226232
case 'smokeTest':
227-
return this.runElectron(mode, options)
228-
.then((pong: any) => {
233+
try {
234+
const pong = await this.runElectron(mode, options)
235+
229236
if (!this.isCurrentlyRunningElectron()) {
230-
return pong
237+
return exit(pong)
231238
}
232239

233240
if (pong === options.ping) {
234-
return 0
241+
return exit(0)
235242
}
236243

237-
return 1
238-
}).then(exit)
239-
.catch(exitErr)
244+
return exit(1)
245+
} catch (err) {
246+
return exitErr(err)
247+
}
240248

241249
case 'returnPkg':
242-
return require('./modes/pkg')(options)
243-
.then((pkg: any) => {
244-
return console.log(JSON.stringify(pkg)) // eslint-disable-line no-console
245-
}).then(exit0)
246-
.catch(exitErr)
250+
try {
251+
console.log(JSON.stringify(pkg)) // eslint-disable-line no-console
252+
253+
return exit0()
254+
} catch (err) {
255+
return exitErr(err)
256+
}
247257

248258
case 'exitWithCode':
249-
return require('./modes/exit')(options)
250-
.then(exit)
251-
.catch(exitErr)
259+
try {
260+
const exitCode = toNumber(options.exitWithCode)
252261

262+
return exit(exitCode)
263+
} catch (err) {
264+
return exitErr(err)
265+
}
253266
case 'run':
254267
// run headlessly and exit
255268
// with num of totalFailed
256-
return this.runElectron(mode, options)
257-
.then((results: any) => {
269+
try {
270+
const results = await this.runElectron(mode, options)
271+
258272
if (results.runs) {
259273
const isCanceled = results.runs.filter((run) => run.skippedSpec).length
260274

261275
if (isCanceled) {
262276
// eslint-disable-next-line no-console
263277
console.log(require('chalk').magenta('\n Exiting with non-zero exit code because the run was canceled.'))
264278

265-
return 1
279+
return exit(1)
266280
}
267281
}
268282

269283
if (options.posixExitCodes) {
270-
return results.totalFailed ? 1 : 0
284+
return exit(results.totalFailed ? 1 : 0)
271285
}
272286

273-
return results.totalFailed
274-
})
275-
.then(exit)
276-
.catch(exitErr)
287+
return exit(results.totalFailed)
288+
} catch (err) {
289+
return exitErr(err)
290+
}
277291

278292
case 'interactive':
279293
return this.runElectron(mode, options)

packages/server/lib/modes/exit.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/server/lib/modes/info.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ const print = (browsers: FoundBrowser[] = []) => {
127127
}
128128
}
129129

130-
const info = () => {
131-
return launcherDetect()
132-
.then(addProfilePath)
133-
.then(print)
134-
}
130+
export const info = async () => {
131+
const browsers = await launcherDetect()
132+
const browsersWithProfilePath = await addProfilePath(browsers)
135133

136-
module.exports = info
134+
print(browsersWithProfilePath)
135+
}

packages/server/lib/modes/pkg.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/server/test/unit/modes/info_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require('../../spec_helper')
22

3-
const info = require(`../../../lib/modes/info`)
3+
const { info } = require(`../../../lib/modes/info`)
44
const capture = require(`../../../lib/capture`)
55
const browserUtils = require(`../../../lib/browsers/utils`)
66
const { fs } = require(`../../../lib/util/fs`)

0 commit comments

Comments
 (0)