one-and-dones.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 Mocha = require('../mocha');
  10. /**
  11. * Dumps a sorted list of the enumerable, lower-case keys of some object
  12. * to `STDOUT`.
  13. * @param {Object} obj - Object, ostensibly having some enumerable keys
  14. * @ignore
  15. * @private
  16. */
  17. const showKeys = obj => {
  18. console.log();
  19. const keys = Object.keys(obj);
  20. const maxKeyLength = keys.reduce((max, key) => Math.max(max, key.length), 0);
  21. keys
  22. .filter(
  23. key => /^[a-z]/.test(key) && !obj[key].browserOnly && !obj[key].abstract
  24. )
  25. .sort()
  26. .forEach(key => {
  27. const description = obj[key].description;
  28. console.log(
  29. ` ${key.padEnd(maxKeyLength + 1)}${
  30. description ? `- ${description}` : ''
  31. }`
  32. );
  33. });
  34. console.log();
  35. };
  36. /**
  37. * Handlers for one-and-done options
  38. * @namespace
  39. * @private
  40. */
  41. exports.ONE_AND_DONES = {
  42. /**
  43. * Dump list of built-in interfaces
  44. * @private
  45. */
  46. 'list-interfaces': () => {
  47. showKeys(Mocha.interfaces);
  48. },
  49. /**
  50. * Dump list of built-in reporters
  51. * @private
  52. */
  53. 'list-reporters': () => {
  54. showKeys(Mocha.reporters);
  55. }
  56. };
  57. /**
  58. * A Set of all one-and-done options
  59. * @type Set<string>
  60. * @private
  61. */
  62. exports.ONE_AND_DONE_ARGS = new Set(
  63. ['help', 'h', 'version', 'V'].concat(Object.keys(exports.ONE_AND_DONES))
  64. );