cli.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. var _templateObject = _taggedTemplateLiteral(['\n ', ' Warning: It looks like you\'re passing --spec a space-separated list of files:\n\n "', '"\n\n This will work, but it\'s not recommended.\n\n The most common cause of this warning is using an unescaped glob pattern. If you are\n trying to pass a glob pattern, escape it using quotes:\n cypress run --spec "**/*.spec.js"\n\n If you are trying to pass multiple spec filenames, separate them by commas instead:\n cypress run --spec spec1,spec2,spec3\n '], ['\n ', ' Warning: It looks like you\'re passing --spec a space-separated list of files:\n\n "', '"\n\n This will work, but it\'s not recommended.\n\n The most common cause of this warning is using an unescaped glob pattern. If you are\n trying to pass a glob pattern, escape it using quotes:\n cypress run --spec "**/*.spec.js"\n\n If you are trying to pass multiple spec filenames, separate them by commas instead:\n cypress run --spec spec1,spec2,spec3\n ']);
  3. function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
  4. var _ = require('lodash');
  5. var commander = require('commander');
  6. var _require = require('common-tags'),
  7. stripIndent = _require.stripIndent;
  8. var logSymbols = require('log-symbols');
  9. var debug = require('debug')('cypress:cli');
  10. var util = require('./util');
  11. var logger = require('./logger');
  12. var errors = require('./errors');
  13. var cache = require('./tasks/cache');
  14. // patch "commander" method called when a user passed an unknown option
  15. // we want to print help for the current command and exit with an error
  16. function unknownOption(flag) {
  17. var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'option';
  18. if (this._allowUnknownOption) return;
  19. logger.error();
  20. logger.error(' error: unknown ' + type + ':', flag);
  21. logger.error();
  22. this.outputHelp();
  23. util.exit(1);
  24. }
  25. commander.Command.prototype.unknownOption = unknownOption;
  26. var coerceFalse = function coerceFalse(arg) {
  27. return arg !== 'false';
  28. };
  29. var spaceDelimitedSpecsMsg = function spaceDelimitedSpecsMsg(files) {
  30. logger.log();
  31. logger.warn(stripIndent(_templateObject, logSymbols.warning, files.join(' ')));
  32. logger.log();
  33. };
  34. var parseVariableOpts = function parseVariableOpts(fnArgs, args) {
  35. var opts = fnArgs.pop();
  36. if (fnArgs.length && opts.spec) {
  37. // this will capture space-delimited specs after --spec spec1 but before the next option
  38. var argIndex = _.indexOf(args, '--spec') + 2;
  39. var nextOptOffset = _.findIndex(_.slice(args, argIndex), function (arg) {
  40. return _.startsWith(arg, '--');
  41. });
  42. var endIndex = nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length;
  43. var maybeSpecs = _.slice(args, argIndex, endIndex);
  44. var extraSpecs = _.intersection(maybeSpecs, fnArgs);
  45. if (extraSpecs.length) {
  46. opts.spec = [opts.spec].concat(extraSpecs);
  47. spaceDelimitedSpecsMsg(opts.spec);
  48. opts.spec = opts.spec.join(',');
  49. }
  50. }
  51. return util.parseOpts(opts);
  52. };
  53. var descriptions = {
  54. record: 'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
  55. key: 'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
  56. spec: 'runs a specific spec file. defaults to "all"',
  57. reporter: 'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
  58. reporterOptions: 'options for the mocha reporter. defaults to "null"',
  59. port: 'runs Cypress on a specific port. overrides any value in the configuration file.',
  60. env: 'sets environment variables. separate multiple values with a comma. overrides any value in the configuration file or cypress.env.json',
  61. config: 'sets configuration values. separate multiple values with a comma. overrides any value in the configuration file.',
  62. 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.',
  63. browserOpenMode: 'path to a custom browser to be added to the list of available browsers in Cypress',
  64. detached: 'runs Cypress application in detached mode',
  65. project: 'path to the project',
  66. global: 'force Cypress into global mode as if its globally installed',
  67. configFile: 'path to JSON file where configuration values are set. defaults to "cypress.json". pass "false" to disable.',
  68. version: 'prints Cypress version',
  69. headed: 'displays the Electron browser instead of running headlessly',
  70. dev: 'runs cypress in development and bypasses binary check',
  71. forceInstall: 'force install the Cypress binary',
  72. exit: 'keep the browser open after tests finish',
  73. cachePath: 'print the path to the binary cache',
  74. cacheList: 'list cached binary versions',
  75. cacheClear: 'delete all cached binaries',
  76. group: 'a named group for recorded runs in the Cypress dashboard',
  77. parallel: 'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
  78. 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'
  79. };
  80. var knownCommands = ['version', 'run', 'open', 'install', 'verify', '-v', '--version', 'help', '-h', '--help', 'cache'];
  81. var text = function text(description) {
  82. if (!descriptions[description]) {
  83. throw new Error('Could not find description for: ' + description);
  84. }
  85. return descriptions[description];
  86. };
  87. function includesVersion(args) {
  88. return _.includes(args, 'version') || _.includes(args, '--version') || _.includes(args, '-v');
  89. }
  90. function showVersions() {
  91. debug('printing Cypress version');
  92. return require('./exec/versions').getVersions().then(function () {
  93. var versions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  94. logger.log('Cypress package version:', versions.package);
  95. logger.log('Cypress binary version:', versions.binary);
  96. process.exit(0);
  97. }).catch(util.logErrorExit1);
  98. }
  99. module.exports = {
  100. init: function init(args) {
  101. if (!args) {
  102. args = process.argv;
  103. }
  104. if (!util.isValidCypressEnvValue(process.env.CYPRESS_ENV)) {
  105. debug('invalid CYPRESS_ENV value', process.env.CYPRESS_ENV);
  106. return errors.exitWithError(errors.errors.invalidCypressEnv)('CYPRESS_ENV=' + process.env.CYPRESS_ENV);
  107. }
  108. var program = new commander.Command();
  109. // bug in commaner not printing name
  110. // in usage help docs
  111. program._name = 'cypress';
  112. program.usage('<command> [options]');
  113. program.command('help').description('Shows CLI help and exits').action(function () {
  114. program.help();
  115. });
  116. program.option('-v, --version', text('version')).command('version').description(text('version')).action(showVersions);
  117. program.command('run').usage('[options]').description('Runs Cypress tests from the CLI without the GUI').option('--record [bool]', text('record'), coerceFalse).option('--headed', text('headed')).option('-k, --key <record-key>', text('key')).option('-s, --spec <spec>', text('spec')).option('-r, --reporter <reporter>', text('reporter')).option('-o, --reporter-options <reporter-options>', text('reporterOptions')).option('-p, --port <port>', text('port')).option('-e, --env <env>', text('env')).option('-c, --config <config>', text('config')).option('-C, --config-file <config-file>', text('configFile')).option('-b, --browser <browser-name-or-path>', text('browserRunMode')).option('-P, --project <project-path>', text('project')).option('--parallel', text('parallel')).option('--group <name>', text('group')).option('--ci-build-id <id>', text('ciBuildId')).option('--no-exit', text('exit')).option('--dev', text('dev'), coerceFalse).action(function () {
  118. for (var _len = arguments.length, fnArgs = Array(_len), _key = 0; _key < _len; _key++) {
  119. fnArgs[_key] = arguments[_key];
  120. }
  121. debug('running Cypress');
  122. require('./exec/run').start(parseVariableOpts(fnArgs, args)).then(util.exit).catch(util.logErrorExit1);
  123. });
  124. program.command('open').usage('[options]').description('Opens Cypress in the interactive GUI.').option('-p, --port <port>', text('port')).option('-e, --env <env>', text('env')).option('-c, --config <config>', text('config')).option('-C, --config-file <config-file>', text('configFile')).option('-d, --detached [bool]', text('detached'), coerceFalse).option('-b, --browser <browser-path>', text('browserOpenMode')).option('-P, --project <project-path>', text('project')).option('--global', text('global')).option('--dev', text('dev'), coerceFalse).action(function (opts) {
  125. debug('opening Cypress');
  126. require('./exec/open').start(util.parseOpts(opts)).catch(util.logErrorExit1);
  127. });
  128. program.command('install').usage('[options]').description('Installs the Cypress executable matching this package\'s version').option('-f, --force', text('forceInstall')).action(function (opts) {
  129. require('./tasks/install').start(util.parseOpts(opts)).catch(util.logErrorExit1);
  130. });
  131. program.command('verify').usage('[options]').description('Verifies that Cypress is installed correctly and executable').option('--dev', text('dev'), coerceFalse).action(function (opts) {
  132. var defaultOpts = { force: true, welcomeMessage: false };
  133. var parsedOpts = util.parseOpts(opts);
  134. var options = _.extend(parsedOpts, defaultOpts);
  135. require('./tasks/verify').start(options).catch(util.logErrorExit1);
  136. });
  137. program.command('cache').usage('[command]').description('Manages the Cypress binary cache').option('list', text('cacheList')).option('path', text('cachePath')).option('clear', text('cacheClear')).action(function (opts) {
  138. if (!_.isString(opts)) {
  139. this.outputHelp();
  140. util.exit(1);
  141. }
  142. if (opts.command || !_.includes(['list', 'path', 'clear'], opts)) {
  143. unknownOption.call(this, 'cache ' + opts, 'command');
  144. }
  145. cache[opts]();
  146. });
  147. debug('cli starts with arguments %j', args);
  148. util.printNodeOptions();
  149. // if there are no arguments
  150. if (args.length <= 2) {
  151. debug('printing help');
  152. program.help();
  153. // exits
  154. }
  155. var firstCommand = args[2];
  156. if (!_.includes(knownCommands, firstCommand)) {
  157. debug('unknown command %s', firstCommand);
  158. logger.error('Unknown command', '"' + firstCommand + '"');
  159. program.outputHelp();
  160. return util.exit(1);
  161. }
  162. if (includesVersion(args)) {
  163. // commander 2.11.0 changes behavior
  164. // and now does not understand top level options
  165. // .option('-v, --version').command('version')
  166. // so we have to manually catch '-v, --version'
  167. return showVersions();
  168. }
  169. debug('program parsing arguments');
  170. return program.parse(args);
  171. }
  172. };
  173. if (!module.parent) {
  174. logger.error('This CLI module should be required from another Node module');
  175. logger.error('and not executed directly');
  176. process.exit(-1);
  177. }