one-and-dones.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. /**
  3. * Contains "command" code for "one-and-dones"--options passed
  4. * to Mocha which cause it to just dump some info and exit.
  5. * See {@link module:lib/cli/one-and-dones.ONE_AND_DONE_ARGS ONE_AND_DONE_ARGS} for more info.
  6. * @module
  7. * @private
  8. */
  9. const align = require('wide-align');
  10. const Mocha = require('../mocha');
  11. /**
  12. * Dumps a sorted list of the enumerable, lower-case keys of some object
  13. * to `STDOUT`.
  14. * @param {Object} obj - Object, ostensibly having some enumerable keys
  15. * @ignore
  16. * @private
  17. */
  18. const showKeys = obj => {
  19. console.log();
  20. const keys = Object.keys(obj);
  21. const maxKeyLength = keys.reduce((max, key) => Math.max(max, key.length), 0);
  22. keys
  23. .filter(
  24. key => /^[a-z]/.test(key) && !obj[key].browserOnly && !obj[key].abstract
  25. )
  26. .sort()
  27. .forEach(key => {
  28. const description = obj[key].description;
  29. console.log(
  30. ` ${align.left(key, maxKeyLength + 1)}${
  31. description ? `- ${description}` : ''
  32. }`
  33. );
  34. });
  35. console.log();
  36. };
  37. /**
  38. * Handlers for one-and-done options
  39. * @namespace
  40. * @private
  41. */
  42. exports.ONE_AND_DONES = {
  43. /**
  44. * Dump list of built-in interfaces
  45. * @private
  46. */
  47. interfaces: () => {
  48. showKeys(Mocha.interfaces);
  49. },
  50. /**
  51. * Dump list of built-in reporters
  52. * @private
  53. */
  54. reporters: () => {
  55. showKeys(Mocha.reporters);
  56. }
  57. };
  58. /**
  59. * A Set of all one-and-done options
  60. * @type Set<string>
  61. * @private
  62. */
  63. exports.ONE_AND_DONE_ARGS = new Set(
  64. ['help', 'h', 'version', 'V'].concat(Object.keys(exports.ONE_AND_DONES))
  65. );