Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: convert server/lib/modes files to ts + add more cli options to Cloud terminal error outputs #31211

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/errors/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const AllCypressErrors = {

https://on.cypress.io/stale-run`
},
CLOUD_ALREADY_COMPLETE: (props: {runUrl: string}) => {
CLOUD_ALREADY_COMPLETE: (props: {runUrl: string, tags: string, group: string, parallel: string, ciBuildId: string}) => {
return errTemplate`\
The run you are attempting to access is already complete and will not accept new groups.

Expand All @@ -257,7 +257,7 @@ export const AllCypressErrors = {

https://on.cypress.io/already-complete`
},
CLOUD_PARALLEL_REQUIRED: (arg1: {runUrl: string}) => {
CLOUD_PARALLEL_REQUIRED: (arg1: {tags: string, group: string, runUrl: string, ciBuildId: string }) => {
return errTemplate`\
You did not pass the ${fmt.flag(`--parallel`)} flag, but this run's group was originally created with the --parallel flag.

Expand All @@ -274,13 +274,14 @@ export const AllCypressErrors = {

https://on.cypress.io/parallel-required`
},
CLOUD_PARALLEL_DISALLOWED: (arg1: {runUrl: string}) => {
CLOUD_PARALLEL_DISALLOWED: (arg1: {tags: string, group: string, runUrl: string, ciBuildId: string}) => {
return errTemplate`\
You passed the ${fmt.flag(`--parallel`)} flag, but this run group was originally created without the --parallel flag.

The existing run is: ${fmt.url(arg1.runUrl)}

${fmt.listFlags(arg1, {
tags: '--tag',
group: '--group',
parallel: '--parallel',
ciBuildId: '--ciBuildId',
Expand All @@ -290,7 +291,7 @@ export const AllCypressErrors = {

https://on.cypress.io/parallel-disallowed`
},
CLOUD_PARALLEL_GROUP_PARAMS_MISMATCH: (arg1: {runUrl: string, parameters: any, payload: any }) => {
CLOUD_PARALLEL_GROUP_PARAMS_MISMATCH: (arg1: {group: string, runUrl: string, ciBuildId: string, parameters: any, payload: any }) => {
let params: any = arg1.parameters

if (arg1.payload?.differentParams) {
Expand Down Expand Up @@ -344,7 +345,7 @@ export const AllCypressErrors = {

https://on.cypress.io/parallel-group-params-mismatch`
},
CLOUD_RUN_GROUP_NAME_NOT_UNIQUE: (arg1: {runUrl: string, ciBuildId?: string | null}) => {
CLOUD_RUN_GROUP_NAME_NOT_UNIQUE: (arg1: {group: string, runUrl: string, ciBuildId?: string | null}) => {
return errTemplate`\
You passed the ${fmt.flag(`--group`)} flag, but this group name has already been used for this run.

Expand All @@ -370,7 +371,7 @@ export const AllCypressErrors = {

${fmt.off(arg1.link)}`
},
CLOUD_AUTO_CANCEL_MISMATCH: (arg1: {runUrl: string}) => {
CLOUD_AUTO_CANCEL_MISMATCH: (arg1: {runUrl: string, tags: string, group: string, parallel: string, ciBuildId: string, autoCancelAfterFailures: string }) => {
return errTemplate`\
You passed the ${fmt.flag(`--auto-cancel-after-failures`)} flag, but this run originally started with a different value for the ${fmt.flag(`--auto-cancel-after-failures`)} flag.

Expand Down Expand Up @@ -1886,7 +1887,7 @@ export const AllCypressErrors = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _typeCheck: Record<keyof AllCypressErrorObj, (...args: any[]) => ErrTemplateResult> = AllCypressErrors

type AllCypressErrorObj = typeof AllCypressErrors
export type AllCypressErrorObj = typeof AllCypressErrors

export type AllCypressErrorNames = keyof typeof AllCypressErrors

Expand Down
6 changes: 5 additions & 1 deletion packages/server/lib/cloud/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ const isRetriableError = (err) => {

export type CreateRunOptions = {
projectRoot: string
ci: string
ci: {
params: string
provider: string
}
ciBuildId: string
projectId: string
recordKey: string
Expand All @@ -253,6 +256,7 @@ export type CreateRunOptions = {
testingType: 'e2e' | 'component'
timeout?: number
project: ProjectBase
autoCancelAfterFailures?: number | undefined
}

type CreateRunResponse = {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/cloud/artifacts/upload_artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const toUploadReportPayload = (acc: {
type UploadArtifactOptions = {
protocolManager?: ProtocolManager
videoUploadUrl?: string
video?: string // filepath to the video artifact
video?: string | null // filepath to the video artifact
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw where we set this null in another file, so this is possible

screenshots?: {
screenshotId: string
path: string
Expand Down
48 changes: 27 additions & 21 deletions packages/server/lib/cypress.js → packages/server/lib/cypress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('./environment')
import './environment'

// we are not requiring everything up front
// to optimize how quickly electron boots while
Expand All @@ -9,14 +9,19 @@ require('./environment')
// essentially do it all again when we boot the correct
// mode.

const Promise = require('bluebird')
const debug = require('debug')('cypress:server:cypress')
const { getPublicConfigKeys } = require('@packages/config')
const argsUtils = require('./util/args')
const { telemetry } = require('@packages/telemetry')
const { getCtx, hasCtx } = require('@packages/data-context')
import Promise from 'bluebird'
import Debug from 'debug'
import { getPublicConfigKeys } from '@packages/config'
import argsUtils from './util/args'
import { telemetry } from '@packages/telemetry'
import { getCtx, hasCtx } from '@packages/data-context'
import type { AllCypressErrorObj } from '@packages/errors/src/errors'

const warning = (code, args) => {
const debug = Debug('cypress:server:cypress')

type Mode = 'exit' | 'info' | 'interactive' | 'pkg' | 'record' | 'results' | 'run' | 'smokeTest' | 'version' | 'returnPkg' | 'exitWithCode'

Copy link
Member Author

@jennifer-shehane jennifer-shehane Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type may be more than is actually supported. It was hard to tell the definitive list by how this is called.

const warning = function <Type extends keyof AllCypressErrorObj> (code: Type, ...args: Parameters<AllCypressErrorObj[Type]>) {
return require('./errors').warning(code, args)
}

Expand All @@ -27,7 +32,7 @@ const exit = async (code = 0) => {
debug('about to exit with code', code)

if (hasCtx()) {
await getCtx().lifecycleManager.mainProcessWillDisconnect().catch((err) => {
await getCtx().lifecycleManager.mainProcessWillDisconnect().catch((err: any) => {
debug('mainProcessWillDisconnect errored with: ', err)
})
}
Expand All @@ -37,14 +42,14 @@ const exit = async (code = 0) => {
span?.setAttribute('exitCode', code)
span?.end()

await telemetry.shutdown().catch((err) => {
await telemetry.shutdown().catch((err: any) => {
debug('telemetry shutdown errored with: ', err)
})

return process.exit(code)
}

const showWarningForInvalidConfig = (options) => {
const showWarningForInvalidConfig = (options: any) => {
const publicConfigKeys = getPublicConfigKeys()
const invalidConfigOptions = require('lodash').keys(options.config).reduce((invalid, option) => {
if (!publicConfigKeys.find((configKey) => configKey === option)) {
Expand All @@ -63,7 +68,7 @@ const exit0 = () => {
return exit(0)
}

const exitErr = (err) => {
const exitErr = (err: any) => {
// log errors to the console
// and potentially raygun
// and exit with 1
Expand All @@ -77,12 +82,12 @@ const exitErr = (err) => {
})
}

module.exports = {
export = {
isCurrentlyRunningElectron () {
return require('./util/electron-app').isRunning()
},

runElectron (mode, options) {
runElectron (mode: Mode, options: any) {
// wrap all of this in a promise to force the
// promise interface - even if it doesn't matter
// in dev mode due to cp.spawn
Expand All @@ -107,7 +112,7 @@ module.exports = {
debug('starting Electron')
const cypressElectron = require('@packages/electron')

const fn = (code) => {
const fn = (code: number) => {
// juggle up the totalFailed since our outer
// promise is expecting this object structure
debug('electron finished with', code)
Expand Down Expand Up @@ -144,7 +149,7 @@ module.exports = {
options = argsUtils.toObject(argv)

showWarningForInvalidConfig(options)
} catch (argumentsError) {
} catch (argumentsError: any) {
debug('could not parse CLI arguments: %o', argv)

// note - this is promise-returned call
Expand All @@ -153,6 +158,7 @@ module.exports = {

debug('from argv %o got options %o', argv, options)

// @ts-expect-error TODO: Fix type that says attachRecordKey is not a function
telemetry.exporter()?.attachRecordKey(options.key)

if (options.headless) {
Expand Down Expand Up @@ -202,14 +208,14 @@ module.exports = {
})
},

startInMode (mode, options) {
startInMode (mode: Mode, options: any) {
debug('starting in mode %s with options %o', mode, options)

switch (mode) {
case 'version':
return require('./modes/pkg')(options)
.get('version')
.then((version) => {
.then((version: any) => {
return console.log(version) // eslint-disable-line no-console
}).then(exit0)
.catch(exitErr)
Expand All @@ -221,7 +227,7 @@ module.exports = {

case 'smokeTest':
return this.runElectron(mode, options)
.then((pong) => {
.then((pong: any) => {
if (!this.isCurrentlyRunningElectron()) {
return pong
}
Expand All @@ -236,7 +242,7 @@ module.exports = {

case 'returnPkg':
return require('./modes/pkg')(options)
.then((pkg) => {
.then((pkg: any) => {
return console.log(JSON.stringify(pkg)) // eslint-disable-line no-console
}).then(exit0)
.catch(exitErr)
Expand All @@ -250,7 +256,7 @@ module.exports = {
// run headlessly and exit
// with num of totalFailed
return this.runElectron(mode, options)
.then((results) => {
.then((results: any) => {
if (results.runs) {
const isCanceled = results.runs.filter((run) => run.skippedSpec).length

Expand Down
8 changes: 0 additions & 8 deletions packages/server/lib/modes/exit.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/server/lib/modes/exit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toNumber } from 'lodash'
import Promise from 'bluebird'

export = (options) => {
return Promise.try(() => {
return toNumber(options.exitWithCode)
})
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable no-console */
const debug = require('debug')('cypress:server:info')
const launcher = require('@packages/launcher')
const pluralize = require('pluralize')
const { stripIndent } = require('common-tags')
const browserUtils = require('../browsers/utils')
const _ = require('lodash')
const chalk = require('chalk')
const { fs } = require('../util/fs')

import Debug from 'debug'
import { detect as launcherDetect } from '@packages/launcher'
import pluralize from 'pluralize'
import { stripIndent } from 'common-tags'
import browserUtils from '../browsers/utils'
import _ from 'lodash'
import chalk from 'chalk'
import { fs } from '../util/fs'
import type { FoundBrowser } from '@packages/types/src/browser'

const debug = Debug('cypress:server:info')
// color for numbers and short values
const n = chalk.green
// color for paths
Expand All @@ -21,7 +23,7 @@ const link = chalk.blue.underline
* If the list has at least 1 item, picks a random item
* and returns it AND the remaining items.
*/
const pickRandomItem = (list) => {
const pickRandomItem = (list: any) => {
if (!list.length) {
return {
item: null,
Expand All @@ -40,7 +42,7 @@ const pickRandomItem = (list) => {
// Usually the full browser name to pass via --browser
// is <name>:<channel>. If the channel is stable, you
// can just do "--browser <name>"
const formBrowserName = (browser) => {
const formBrowserName = (browser: FoundBrowser) => {
if (browser.channel === 'stable') {
return browser.name
}
Expand All @@ -51,9 +53,9 @@ const formBrowserName = (browser) => {
// for each browser computes the profile folder
// and checks if the folder exists. If exists,
// adds it to the browser object as a property
const addProfilePath = async (browsers = []) => {
const addProfilePath = async (browsers: FoundBrowser[] = []) => {
for (const browser of browsers) {
const profilePath = browserUtils.getBrowserPath(browser)
const profilePath: string = browserUtils.getBrowserPath(browser)

debug('checking profile path %s for browser %s:%s', profilePath, browser.name, browser.channel)
try {
Expand All @@ -71,11 +73,11 @@ const addProfilePath = async (browsers = []) => {
return browsers
}

const print = (browsers = []) => {
const print = (browsers: FoundBrowser[] = []) => {
console.log('Displaying Cypress info...')
console.log('')
if (browsers.length) {
console.log('Detected %s %s installed:', n(browsers.length), pluralize('browser', browsers.length))
console.log('Detected %s %s installed:', n(`${browsers.length}`), pluralize('browser', browsers.length))
} else {
console.log('Detected no known browsers installed')
}
Expand Down Expand Up @@ -126,7 +128,7 @@ const print = (browsers = []) => {
}

const info = () => {
return launcher.detect()
return launcherDetect()
.then(addProfilePath)
.then(print)
}
Expand Down
6 changes: 0 additions & 6 deletions packages/server/lib/modes/pkg.js

This file was deleted.

6 changes: 6 additions & 0 deletions packages/server/lib/modes/pkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Promise from 'bluebird'
import pkg from '@packages/root'

export = () => {
return Promise.resolve(pkg)
}
Loading
Loading