Files
cypress/cli/lib/exec/run.ts
Bill Glesias 3481d1acaf chore: refactor cypress/cli to TypeScript (#32063)
* migrate cli scripts to TypeScript

* convert all javascript source files in the CLI to TypeScript

rebase into first

* chore: refactor all tests to TypeScript

rebase into second

* add npmignore for cli for typescript files

* update build process

* fix publically available exports

* Fix cy-in-cy tests

* add ts-expect-error to failing files

* fix projectConfigIpc failures as there are now multiple installs of tsx

* fix after-pack hook

* fix binary script

* chore: update publish binary to account for CLI being an ESModule compiled down to CommonJS

* does this work?

* fix the verify spec by making the listr2 renderer silent as it behaves differently since the refactor and is printing non deterministic outputs into our tests that do not have a large impact on the area we are testing and mostly served to actually test the renders of the listr2 framework itself

* empty commit

* additional refactor to code to remove strange any typing and exporting

* bump cache and build binaries

* fix CLI exports to keep backwards compatibility

* fix unit-tests

* turn on mac jobs

* fix group name rename in CLI

* remove babel deps from cli and explicitly install typescript

* address feedback from code review

* dont just falsy check results and instead explicitly check for null or undefined

* add ts-expect-error

* additional pass on cleaning up dynamic require / import from global lib references

* annotate ts-expect-errors with reason for why error is expected

* add rest of ts-expect-error comments

* removing hardcoded branch to publish binary chore/migrate_cli_to_typescript
2025-09-02 17:52:45 -04:00

204 lines
4.5 KiB
TypeScript

import _ from 'lodash'
import Debug from 'debug'
import util from '../util'
import spawn from './spawn'
import verifyModule from '../tasks/verify'
import { exitWithError, errors } from '../errors'
import { processTestingType, throwInvalidOptionError, checkConfigFile } from './shared'
const debug = Debug('cypress:cli:run')
/**
* Typically a user passes a string path to the project.
* But "cypress open" allows using `false` to open in global mode,
* and the user can accidentally execute `cypress run --project false`
* which should be invalid.
*/
const isValidProject = (v: any): boolean => {
if (typeof v === 'boolean') {
return false
}
if (v === '' || v === 'false' || v === 'true') {
return false
}
return true
}
/**
* Maps options collected by the CLI
* and forms list of CLI arguments to the server.
*
* Note: there is lightweight validation, with errors
* thrown synchronously.
*
* @returns {string[]} list of CLI arguments
*/
const processRunOptions = (options: any = {}): string[] => {
debug('processing run options %o', options)
if (!isValidProject(options.project)) {
debug('invalid project option %o', { project: options.project })
return throwInvalidOptionError(errors.invalidRunProjectPath)
}
const args: string[] = ['--run-project', options.project]
if (options.autoCancelAfterFailures || options.autoCancelAfterFailures === 0 || options.autoCancelAfterFailures === false) {
args.push('--auto-cancel-after-failures', options.autoCancelAfterFailures)
}
if (options.browser) {
args.push('--browser', options.browser)
}
if (options.ciBuildId) {
args.push('--ci-build-id', options.ciBuildId)
}
if (options.config) {
args.push('--config', options.config)
}
if (options.configFile !== undefined) {
checkConfigFile(options)
args.push('--config-file', options.configFile)
}
if (options.env) {
args.push('--env', options.env)
}
if (options.exit === false) {
args.push('--no-exit')
}
if (options.group) {
args.push('--group', options.group)
}
if (options.headed) {
args.push('--headed', options.headed)
}
if (options.headless) {
if (options.headed) {
return throwInvalidOptionError(errors.incompatibleHeadlessFlags)
}
args.push('--headed', String(!options.headless))
}
// if key is set use that - else attempt to find it by environment variable
if (options.key == null) {
debug('--key is not set, looking up environment variable CYPRESS_RECORD_KEY')
options.key = util.getEnv('CYPRESS_RECORD_KEY')
}
// if we have a key assume we're in record mode
if (options.key) {
args.push('--key', options.key)
}
if (options.outputPath) {
args.push('--output-path', options.outputPath)
}
if (options.parallel) {
args.push('--parallel')
}
if (options.port) {
args.push('--port', options.port)
}
if (options.quiet) {
args.push('--quiet')
}
// if record is defined and we're not
// already in ci mode, then send it up
if (options.record != null) {
args.push('--record', options.record)
}
// if we have a specific reporter push that into the args
if (options.reporter) {
args.push('--reporter', options.reporter)
}
// if we have a specific reporter push that into the args
if (options.reporterOptions) {
args.push('--reporter-options', options.reporterOptions)
}
if (options.runnerUi != null) {
args.push('--runner-ui', options.runnerUi)
}
// if we have specific spec(s) push that into the args
if (options.spec) {
args.push('--spec', options.spec)
}
if (options.tag) {
args.push('--tag', options.tag)
}
if (options.inspect) {
args.push('--inspect')
}
if (options.inspectBrk) {
args.push('--inspectBrk')
}
args.push(...processTestingType(options))
return args
}
const runModule = {
processRunOptions,
isValidProject,
// resolves with the number of failed tests
start (options: any = {}): any {
_.defaults(options, {
key: null,
spec: null,
reporter: null,
reporterOptions: null,
project: process.cwd(),
})
function run (): any {
try {
const args = processRunOptions(options)
debug('run to spawn.start args %j', args)
return spawn.start(args, {
dev: options.dev,
})
} catch (err: any) {
if (err.details) {
return exitWithError(err.details)()
}
throw err
}
}
if (options.dev) {
return run()
}
return verifyModule.start()
.then(run)
},
}
export default runModule