-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcli.js
615 lines (519 loc) · 19.1 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// @ts-check
const _ = require('lodash')
const R = require('ramda')
const commander = require('commander')
const { stripIndent } = require('common-tags')
const logSymbols = require('log-symbols')
const debug = require('debug')('cypress:cli:cli')
const util = require('./util')
const logger = require('./logger')
const errors = require('./errors')
const cache = require('./tasks/cache')
// patch "commander" method called when a user passed an unknown option
// we want to print help for the current command and exit with an error
function unknownOption (flag, type = 'option') {
if (this._allowUnknownOption) return
logger.error()
logger.error(` error: unknown ${type}:`, flag)
logger.error()
this.outputHelp()
util.exit(1)
}
commander.Command.prototype.unknownOption = unknownOption
const coerceFalseOrString = (arg) => {
return arg !== 'false' ? arg : false
}
const coerceFalse = (arg) => {
return arg !== 'false'
}
const coerceAnyStringToInt = (arg) => {
return typeof arg === 'string' ? parseInt(arg) : arg
}
const spaceDelimitedArgsMsg = (flag, args) => {
let msg = `
${logSymbols.warning} Warning: It looks like you're passing --${flag} a space-separated list of arguments:
"${args.join(' ')}"
This will work, but it's not recommended.
If you are trying to pass multiple arguments, separate them with commas instead:
cypress run --${flag} arg1,arg2,arg3
`
if (flag === 'spec') {
msg += `
The most common cause of this warning is using an unescaped glob pattern. If you are
trying to pass a glob pattern, escape it using quotes:
cypress run --spec "**/*.spec.js"
`
}
logger.log()
logger.warn(stripIndent(msg))
logger.log()
}
const parseVariableOpts = (fnArgs, args) => {
const [opts, unknownArgs] = fnArgs
if ((unknownArgs && unknownArgs.length) && (opts.spec || opts.tag)) {
// this will capture space-delimited args after
// flags that could have possible multiple args
// but before the next option
// --spec spec1 spec2 or --tag foo bar
const multiArgFlags = _.compact([
opts.spec ? 'spec' : opts.spec,
opts.tag ? 'tag' : opts.tag,
])
_.forEach(multiArgFlags, (flag) => {
const argIndex = _.indexOf(args, `--${flag}`) + 2
const nextOptOffset = _.findIndex(_.slice(args, argIndex), (arg) => {
return _.startsWith(arg, '--')
})
const endIndex = nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length
const maybeArgs = _.slice(args, argIndex, endIndex)
const extraArgs = _.intersection(maybeArgs, unknownArgs)
if (extraArgs.length) {
opts[flag] = [opts[flag]].concat(extraArgs)
spaceDelimitedArgsMsg(flag, opts[flag])
opts[flag] = opts[flag].join(',')
}
})
}
debug('variable-length opts parsed %o', { args, opts })
return util.parseOpts(opts)
}
const descriptions = {
browserOpenMode: 'path to a custom browser to be added to the list of available browsers in Cypress',
browserRunMode: 'runs Cypress in the browser with the given name. if a filesystem path is supplied, Cypress will attempt to use the browser at that path.',
cacheClear: 'delete all cached binaries',
cachePrune: 'deletes all cached binaries except for the version currently in use',
cacheList: 'list cached binary versions',
cachePath: 'print the path to the binary cache',
cacheSize: 'Used with the list command to show the sizes of the cached folders',
ciBuildId: 'the unique identifier for a run on your CI provider. typically a "BUILD_ID" env var. this value is automatically detected for most CI providers',
component: 'runs component tests',
config: 'sets configuration values. separate multiple values with a comma. overrides any value in cypress.config.{ts|js}.',
configFile: 'path to JSON file where configuration values are set. defaults to "cypress.config.{ts|js}". pass "false" to disable.',
detached: 'runs Cypress application in detached mode',
dev: 'runs cypress in development and bypasses binary check',
e2e: 'runs end to end tests',
env: 'sets environment variables. separate multiple values with a comma. overrides any value in cypress.config.{ts|js} or cypress.env.json',
exit: 'keep the browser open after tests finish',
forceInstall: 'force install the Cypress binary',
global: 'force Cypress into global mode as if its globally installed',
group: 'a named group for recorded runs in the Cypress Dashboard',
headed: 'displays the browser instead of running headlessly',
headless: 'hide the browser instead of running headed (default for cypress run)',
key: 'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
parallel: 'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
port: 'runs Cypress on a specific port. overrides any value in cypress.config.{ts|js}.',
project: 'path to the project',
quiet: 'run quietly, using only the configured reporter',
record: 'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
reporter: 'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
reporterOptions: 'options for the mocha reporter. defaults to "null"',
spec: 'runs specific spec file(s). defaults to "all"',
tag: 'named tag(s) for recorded runs in the Cypress Dashboard',
version: 'prints Cypress version',
}
const knownCommands = [
'cache',
'help',
'-h',
'--help',
'install',
'open',
'run',
'open-ct',
'run-ct',
'verify',
'-v',
'--version',
'version',
'info',
]
const text = (description) => {
if (!descriptions[description]) {
throw new Error(`Could not find description for: ${description}`)
}
return descriptions[description]
}
function includesVersion (args) {
return (
_.includes(args, 'version') ||
_.includes(args, '--version') ||
_.includes(args, '-v')
)
}
function showVersions (args) {
debug('printing Cypress version')
debug('additional arguments %o', args)
const versionParser = commander.option(
'--component <package|binary|electron|node>', 'component to report version for',
)
.allowUnknownOption(true)
const parsed = versionParser.parse(args)
const parsedOptions = {
component: parsed.component,
}
debug('parsed version arguments %o', parsedOptions)
const reportAllVersions = (versions) => {
logger.always('Cypress package version:', versions.package)
logger.always('Cypress binary version:', versions.binary)
logger.always('Electron version:', versions.electronVersion)
logger.always('Bundled Node version:', versions.electronNodeVersion)
}
const reportComponentVersion = (componentName, versions) => {
const names = {
package: 'package',
binary: 'binary',
electron: 'electronVersion',
node: 'electronNodeVersion',
}
if (!names[componentName]) {
throw new Error(`Unknown component name "${componentName}"`)
}
const name = names[componentName]
if (!versions[name]) {
throw new Error(`Cannot find version for component "${componentName}" under property "${name}"`)
}
const version = versions[name]
logger.always(version)
}
const defaultVersions = {
package: undefined,
binary: undefined,
electronVersion: undefined,
electronNodeVersion: undefined,
}
return require('./exec/versions')
.getVersions()
.then((versions = defaultVersions) => {
if (parsedOptions.component) {
reportComponentVersion(parsedOptions.component, versions)
} else {
reportAllVersions(versions)
}
process.exit(0)
})
.catch(util.logErrorExit1)
}
const createProgram = () => {
const program = new commander.Command()
// bug in commander not printing name
// in usage help docs
program._name = 'cypress'
program.usage('<command> [options]')
return program
}
const addCypressRunCommand = (program) => {
return program
.command('run')
.usage('[options]')
.description('Runs Cypress tests from the CLI without the GUI')
.option('-b, --browser <browser-name-or-path>', text('browserRunMode'))
.option('--ci-build-id <id>', text('ciBuildId'))
.option('--component', text('component'))
.option('-c, --config <config>', text('config'))
.option('-C, --config-file <config-file>', text('configFile'))
.option('--e2e', text('e2e'))
.option('-e, --env <env>', text('env'))
.option('--group <name>', text('group'))
.option('-k, --key <record-key>', text('key'))
.option('--headed', text('headed'))
.option('--headless', text('headless'))
.option('--no-exit', text('exit'))
.option('--parallel', text('parallel'))
.option('-p, --port <port>', text('port'))
.option('-P, --project <project-path>', text('project'))
.option('-q, --quiet', text('quiet'))
.option('--record [bool]', text('record'), coerceFalse)
.option('-r, --reporter <reporter>', text('reporter'))
.option('-o, --reporter-options <reporter-options>', text('reporterOptions'))
.option('-s, --spec <spec>', text('spec'))
.option('-t, --tag <tag>', text('tag'))
.option('--dev', text('dev'), coerceFalse)
}
/**
* Casts known command line options for "cypress run" to their intended type.
* For example if the user passes "--port 5005" the ".port" property should be
* a number 5005 and not a string "5005".
*
* Returns a clone of the original object.
*/
const castCypressRunOptions = (opts) => {
// only properties that have type "string | false" in our TS definition
// require special handling, because CLI parsing takes care of purely
// boolean arguments
const result = R.evolve({
port: coerceAnyStringToInt,
configFile: coerceFalseOrString,
})(opts)
return result
}
module.exports = {
/**
* Parses `cypress run` command line option array into an object
* with options that you can feed into a `cypress.run()` module API call.
* @example
* const options = parseRunCommand(['cypress', 'run', '--browser', 'chrome'])
* // options is {browser: 'chrome'}
*/
parseRunCommand (args) {
return new Promise((resolve, reject) => {
if (!Array.isArray(args)) {
return reject(new Error('Expected array of arguments'))
}
// make a copy of the input arguments array
// and add placeholders where "node ..." would usually be
// also remove "cypress" keyword at the start if present
const cliArgs = args[0] === 'cypress' ? [...args.slice(1)] : [...args]
cliArgs.unshift(null, null)
debug('creating program parser')
const program = createProgram()
addCypressRunCommand(program)
.action((...fnArgs) => {
debug('parsed Cypress run %o', fnArgs)
const options = parseVariableOpts(fnArgs, cliArgs)
debug('parsed options %o', options)
const casted = castCypressRunOptions(options)
debug('casted options %o', casted)
resolve(casted)
})
debug('parsing args: %o', cliArgs)
program.parse(cliArgs)
})
},
/**
* Parses the command line and kicks off Cypress process.
*/
init (args) {
if (!args) {
args = process.argv
}
const { CYPRESS_INTERNAL_ENV } = process.env
if (!util.isValidCypressInternalEnvValue(CYPRESS_INTERNAL_ENV)) {
debug('invalid CYPRESS_INTERNAL_ENV value', CYPRESS_INTERNAL_ENV)
return errors.exitWithError(errors.errors.invalidCypressEnv)(
`CYPRESS_INTERNAL_ENV=${CYPRESS_INTERNAL_ENV}`,
)
}
if (util.isNonProductionCypressInternalEnvValue(CYPRESS_INTERNAL_ENV)) {
debug('non-production CYPRESS_INTERNAL_ENV value', CYPRESS_INTERNAL_ENV)
let msg = `
${logSymbols.warning} Warning: It looks like you're passing CYPRESS_INTERNAL_ENV=${CYPRESS_INTERNAL_ENV}
The environment variable "CYPRESS_INTERNAL_ENV" is reserved and should only be used internally.
Unset the "CYPRESS_INTERNAL_ENV" environment variable and run Cypress again.
`
logger.log()
logger.warn(stripIndent(msg))
logger.log()
}
const program = createProgram()
program
.command('help')
.description('Shows CLI help and exits')
.action(() => {
program.help()
})
program
.option('-v, --version', text('version'))
.command('version')
.description(text('version'))
.action(() => {
showVersions(args)
})
program
.command('open')
.usage('[options]')
.description('Opens Cypress in the interactive GUI.')
.option('-b, --browser <browser-path>', text('browserOpenMode'))
.option('--component', text('component'))
.option('-c, --config <config>', text('config'))
.option('-C, --config-file <config-file>', text('configFile'))
.option('-d, --detached [bool]', text('detached'), coerceFalse)
.option('--e2e', text('e2e'))
.option('-e, --env <env>', text('env'))
.option('--global', text('global'))
.option('-p, --port <port>', text('port'))
.option('-P, --project <project-path>', text('project'))
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
debug('opening Cypress')
require('./exec/open')
.start(util.parseOpts(opts))
.then(util.exit)
.catch(util.logErrorExit1)
})
addCypressRunCommand(program)
.action((...fnArgs) => {
debug('running Cypress with args %o', fnArgs)
require('./exec/run')
.start(parseVariableOpts(fnArgs, args))
.then(util.exit)
.catch(util.logErrorExit1)
})
program
.command('open-ct')
.usage('[options]')
.description('Opens Cypress component testing interactive mode. Deprecated: use "open --component"')
.option('-b, --browser <browser-path>', text('browserOpenMode'))
.option('-c, --config <config>', text('config'))
.option('-C, --config-file <config-file>', text('configFile'))
.option('-d, --detached [bool]', text('detached'), coerceFalse)
.option('-e, --env <env>', text('env'))
.option('--global', text('global'))
.option('-p, --port <port>', text('port'))
.option('-P, --project <project-path>', text('project'))
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
debug('opening Cypress')
const msg = `
${logSymbols.warning} Warning: open-ct is deprecated and will be removed in a future release.
Use \`cypress open --component\` instead.
`
logger.warn()
logger.warn(stripIndent(msg))
logger.warn()
require('./exec/open')
.start({ ...util.parseOpts(opts), testingType: 'component' })
.catch(util.logErrorExit1)
})
program
.command('run-ct')
.usage('[options]')
.description('Runs all Cypress component testing suites. Deprecated: use "run --component"')
.option('-b, --browser <browser-name-or-path>', text('browserRunMode'))
.option('--ci-build-id <id>', text('ciBuildId'))
.option('-c, --config <config>', text('config'))
.option('-C, --config-file <config-file>', text('configFile'))
.option('-e, --env <env>', text('env'))
.option('--group <name>', text('group'))
.option('-k, --key <record-key>', text('key'))
.option('--headed', text('headed'))
.option('--headless', text('headless'))
.option('--no-exit', text('exit'))
.option('--parallel', text('parallel'))
.option('-p, --port <port>', text('port'))
.option('-P, --project <project-path>', text('project'))
.option('-q, --quiet', text('quiet'))
.option('--record [bool]', text('record'), coerceFalse)
.option('-r, --reporter <reporter>', text('reporter'))
.option('-o, --reporter-options <reporter-options>', text('reporterOptions'))
.option('-s, --spec <spec>', text('spec'))
.option('-t, --tag <tag>', text('tag'))
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
debug('running Cypress run-ct')
const msg = `
${logSymbols.warning} Warning: run-ct is deprecated and will be removed in a future release.
Use \`cypress run --component\` instead.
`
logger.warn()
logger.warn(stripIndent(msg))
logger.warn()
require('./exec/run')
.start({ ...util.parseOpts(opts), testingType: 'component' })
.then(util.exit)
.catch(util.logErrorExit1)
})
program
.command('install')
.usage('[options]')
.description(
'Installs the Cypress executable matching this package\'s version',
)
.option('-f, --force', text('forceInstall'))
.action((opts) => {
require('./tasks/install')
.start(util.parseOpts(opts))
.catch(util.logErrorExit1)
})
program
.command('verify')
.usage('[options]')
.description(
'Verifies that Cypress is installed correctly and executable',
)
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
const defaultOpts = { force: true, welcomeMessage: false }
const parsedOpts = util.parseOpts(opts)
const options = _.extend(parsedOpts, defaultOpts)
require('./tasks/verify')
.start(options)
.catch(util.logErrorExit1)
})
program
.command('cache')
.usage('[command]')
.description('Manages the Cypress binary cache')
.option('list', text('cacheList'))
.option('path', text('cachePath'))
.option('clear', text('cacheClear'))
.option('prune', text('cachePrune'))
.option('--size', text('cacheSize'))
.action(function (opts, args) {
if (!args || !args.length) {
this.outputHelp()
util.exit(1)
}
const [command] = args
if (!_.includes(['list', 'path', 'clear', 'prune'], command)) {
unknownOption.call(this, `cache ${command}`, 'command')
}
if (command === 'list') {
debug('cache command %o', {
command,
size: opts.size,
})
return cache.list(opts.size)
.catch({ code: 'ENOENT' }, () => {
logger.always('No cached binary versions were found.')
process.exit(0)
})
.catch((e) => {
debug('cache list command failed with "%s"', e.message)
util.logErrorExit1(e)
})
}
cache[command]()
})
program
.command('info')
.usage('[command]')
.description('Prints Cypress and system information')
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
require('./exec/info')
.start(opts)
.then(util.exit)
.catch(util.logErrorExit1)
})
debug('cli starts with arguments %j', args)
util.printNodeOptions()
// if there are no arguments
if (args.length <= 2) {
debug('printing help')
program.help()
// exits
}
const firstCommand = args[2]
if (!_.includes(knownCommands, firstCommand)) {
debug('unknown command %s', firstCommand)
logger.error('Unknown command', `"${firstCommand}"`)
program.outputHelp()
return util.exit(1)
}
if (includesVersion(args)) {
// commander 2.11.0 changes behavior
// and now does not understand top level options
// .option('-v, --version').command('version')
// so we have to manually catch '-v, --version'
return showVersions(args)
}
debug('program parsing arguments')
return program.parse(args)
},
}
// @ts-ignore
if (!module.parent) {
logger.error('This CLI module should be required from another Node module')
logger.error('and not executed directly')
process.exit(-1)
}