Files
cypress/cli/lib/tasks/state.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

210 lines
6.2 KiB
TypeScript

import _ from 'lodash'
import os from 'os'
import path from 'path'
import untildify from 'untildify'
import Debug from 'debug'
import fs from '../fs'
import util from '../util'
const debug = Debug('cypress:cli')
const getPlatformExecutable = (): string => {
const platform = os.platform()
switch (platform) {
case 'darwin': return 'Contents/MacOS/Cypress'
case 'linux': return 'Cypress'
case 'win32': return 'Cypress.exe'
// TODO handle this error using our standard
default: throw new Error(`Platform: "${platform}" is not supported.`)
}
}
const getPlatFormBinaryFolder = (): string => {
const platform = os.platform()
switch (platform) {
case 'darwin': return 'Cypress.app'
case 'linux': return 'Cypress'
case 'win32': return 'Cypress'
// TODO handle this error using our standard
default: throw new Error(`Platform: "${platform}" is not supported.`)
}
}
const getBinaryPkgPath = (binaryDir: string): string => {
const platform = os.platform()
switch (platform) {
case 'darwin': return path.join(binaryDir, 'Contents', 'Resources', 'app', 'package.json')
case 'linux': return path.join(binaryDir, 'resources', 'app', 'package.json')
case 'win32': return path.join(binaryDir, 'resources', 'app', 'package.json')
// TODO handle this error using our standard
default: throw new Error(`Platform: "${platform}" is not supported.`)
}
}
/**
* Get path to binary directory
*/
const getBinaryDir = (version: string = util.pkgVersion()): string => {
return path.join(getVersionDir(version), getPlatFormBinaryFolder())
}
const getVersionDir = (version: string = util.pkgVersion(), buildInfo: any = util.pkgBuildInfo()): string => {
if (buildInfo && !buildInfo.stable) {
version = ['beta', version, buildInfo.commitBranch, buildInfo.commitSha.slice(0, 8)].join('-')
}
return path.join(getCacheDir(), version)
}
/**
* When executing "npm postinstall" hook, the working directory is set to
* "<current folder>/node_modules/cypress", which can be surprising when using relative paths.
*/
const isInstallingFromPostinstallHook = (): boolean => {
// individual folders
const cwdFolders = process.cwd().split(path.sep)
const length = cwdFolders.length
return cwdFolders[length - 2] === 'node_modules' && cwdFolders[length - 1] === 'cypress'
}
const getCacheDir = (): string => {
let cache_directory = util.getCacheDir()
if (util.getEnv('CYPRESS_CACHE_FOLDER')) {
const envVarCacheDir = untildify(util.getEnv('CYPRESS_CACHE_FOLDER') as string)
debug('using environment variable CYPRESS_CACHE_FOLDER %s', envVarCacheDir)
if (!path.isAbsolute(envVarCacheDir) && isInstallingFromPostinstallHook()) {
const packageRootFolder = path.join('..', '..', envVarCacheDir)
cache_directory = path.resolve(packageRootFolder)
debug('installing from postinstall hook, original root folder is %s', packageRootFolder)
debug('and resolved cache directory is %s', cache_directory)
} else {
cache_directory = path.resolve(envVarCacheDir)
}
}
return cache_directory
}
const parseRealPlatformBinaryFolderAsync = (binaryPath: string): any => {
return fs.realpathAsync(binaryPath)
.then((realPath: any) => {
debug('CYPRESS_RUN_BINARY has realpath:', realPath)
if (!realPath.toString().endsWith(getPlatformExecutable())) {
return false
}
if (os.platform() === 'darwin') {
return path.resolve(realPath, '..', '..', '..')
}
return path.resolve(realPath, '..')
})
}
const getDistDir = (): string => {
return path.join(__dirname, '..', '..', 'dist')
}
/**
* Returns full filename to the file that keeps the Test Runner verification state as JSON text.
* Note: the binary state file will be stored one level up from the given binary folder.
* @param {string} binaryDir - full path to the folder holding the binary.
*/
const getBinaryStatePath = (binaryDir: string): string => {
return path.join(binaryDir, '..', 'binary_state.json')
}
const getBinaryStateContentsAsync = (binaryDir: string): any => {
const fullPath = getBinaryStatePath(binaryDir)
return fs.readJsonAsync(fullPath)
.catch({ code: 'ENOENT' }, SyntaxError, () => {
debug('could not read binary_state.json file at "%s"', fullPath)
return {}
})
}
const getBinaryVerifiedAsync = (binaryDir: string): any => {
return getBinaryStateContentsAsync(binaryDir)
.tap(debug)
.get('verified')
}
const clearBinaryStateAsync = (binaryDir: string): any => {
return fs.removeAsync(getBinaryStatePath(binaryDir))
}
/**
* Writes the new binary status.
* @param {boolean} verified The new test runner state after smoke test
* @param {string} binaryDir Folder holding the binary
* @returns {Promise<void>} returns a promise
*/
const writeBinaryVerifiedAsync = (verified: boolean, binaryDir: string): any => {
return getBinaryStateContentsAsync(binaryDir)
.then((contents: any) => {
return fs.outputJsonAsync(
getBinaryStatePath(binaryDir),
_.extend(contents, { verified }),
{ spaces: 2 },
)
})
}
const getPathToExecutable = (binaryDir: string): string => {
return path.join(binaryDir, getPlatformExecutable())
}
/**
* Resolves with an object read from the binary app package.json file.
* If the file does not exist resolves with null
*/
const getBinaryPkgAsync = (binaryDir: string): any => {
const pathToPackageJson = getBinaryPkgPath(binaryDir)
debug('Reading binary package.json from:', pathToPackageJson)
return fs.pathExistsAsync(pathToPackageJson)
.then((exists: boolean) => {
if (!exists) {
return null
}
return fs.readJsonAsync(pathToPackageJson)
})
}
const getBinaryPkgVersion = (o: any): any => _.get(o, 'version', null)
const getBinaryElectronVersion = (o: any): any => _.get(o, 'electronVersion', null)
const getBinaryElectronNodeVersion = (o: any): any => _.get(o, 'electronNodeVersion', null)
const stateModule = {
getPathToExecutable,
getPlatformExecutable,
// those names start to sound like Java
getBinaryElectronNodeVersion,
getBinaryElectronVersion,
getBinaryPkgVersion,
getBinaryVerifiedAsync,
getBinaryPkgAsync,
getBinaryPkgPath,
getBinaryDir,
getCacheDir,
clearBinaryStateAsync,
writeBinaryVerifiedAsync,
parseRealPlatformBinaryFolderAsync,
getDistDir,
getVersionDir,
}
export default stateModule