Files
cypress/cli/lib/util.js
Brian Mann d54156e2f2 cli, fixes #921, #1113, #1126, #1127, make DEBUG logs work, show error when xvfb exits with status code 1, force tty in linux, handle colors in windows, enable logging cypress:xvfb stderr
* cli: fixes #838 start cypress in dev by routing through the CLI

- matches how we run in production better to keep parity and consistency

* cli: add coerceFalse for clarity

* cli: add global flag, update to work with windows

* server: bring into parity with root scripts

* cli: just execute start script directly to work with windows

* cli: if colors are supported then force them via env vars

- this fixes windows not displaying colors from electron because by
default isTTY is false (due to electron)

* cli: fixes #921 don't ignore stderr, inherit stdio on everything except when linux + xvfb

- filter out stderr messages coming from Xlib or libudev (from xvfb)

* cli, server: force stderr tty so that normalize tty behavior when piping

* server: drop in supports color so debug outputs more colors!

* server: remove empty line

* root: refer to cypress not monorepo

* cli: make util.supportsColor return boolean

* cl: add tests around spawn behavior with forcing colors, tty, and stdio configuration

* cli: handle xvfb onStderrData callback to output debug information

* cli: handle non zero exit code error from xvfb with special message
2017-12-24 19:03:57 -05:00

111 lines
2.4 KiB
JavaScript

const _ = require('lodash')
const R = require('ramda')
const path = require('path')
const isCi = require('is-ci')
const chalk = require('chalk')
const supportsColor = require('supports-color')
const isInstalledGlobally = require('is-installed-globally')
const pkg = require(path.join(__dirname, '..', 'package.json'))
const logger = require('./logger')
const joinWithEq = (x, y) => `${x}=${y}`
// converts an object (single level) into
// key1=value1,key2=value2,...
const objectToString = (obj) =>
R.zipWith(joinWithEq, R.keys(obj), R.values(obj)).join(',')
const normalizeObject = (env) =>
_.isPlainObject(env) ? objectToString(env) : env
function normalizeModuleOptions (options = {}) {
return R.evolve({
env: normalizeObject,
config: normalizeObject,
reporterOptions: normalizeObject,
})(options)
}
function stdoutLineMatches (expectedLine, stdout) {
const lines = stdout.split('\n').map(R.trim)
const lineMatches = R.equals(expectedLine)
return lines.some(lineMatches)
}
const util = {
normalizeModuleOptions,
isCi () {
return isCi
},
supportsColor () {
// we only care about stderr supporting color
// since thats what our DEBUG logs use
return Boolean(supportsColor.stderr)
},
cwd () {
return process.cwd()
},
pkgVersion () {
return pkg.version
},
exit (code) {
process.exit(code)
},
logErrorExit1 (err) {
logger.error(err.message)
process.exit(1)
},
titleize (...args) {
// prepend first arg with space
// and pad so that all messages line up
args[0] = _.padEnd(` ${args[0]}`, 24)
// get rid of any falsy values
args = _.compact(args)
return chalk.blue(...args)
},
calculateEta (percent, elapsed) {
// returns the number of seconds remaining
// if we're at 100 already just return 0
if (percent === 100) {
return 0
}
// take the percentage and divide by one
// and multiple that against elapsed
// subtracting what's already elapsed
return elapsed * (1 / (percent / 100)) - elapsed
},
secsRemaining (eta) {
// calculate the seconds reminaing with no decimal places
return (_.isFinite(eta) ? (eta / 1000) : 0).toFixed(0)
},
setTaskTitle (task, title, renderer) {
// only update the renderer title when not running in CI
if (renderer === 'default') {
task.title = title
}
},
isInstalledGlobally () {
return isInstalledGlobally
},
stdoutLineMatches,
}
module.exports = util