help.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const _ = require('lodash');
  5. const table = require('text-table');
  6. /**
  7. * @mixin
  8. * @alias actions/help
  9. */
  10. const help = module.exports;
  11. /**
  12. * Tries to get the description from a USAGE file one folder above the
  13. * source root otherwise uses a default description
  14. */
  15. help.help = function () {
  16. const filepath = path.resolve(this.sourceRoot(), '../USAGE');
  17. const exists = fs.existsSync(filepath);
  18. let out = [
  19. 'Usage:',
  20. ' ' + this.usage(),
  21. ''
  22. ];
  23. // Build options
  24. if (Object.keys(this._options).length > 0) {
  25. out = out.concat([
  26. 'Options:',
  27. this.optionsHelp(),
  28. ''
  29. ]);
  30. }
  31. // Build arguments
  32. if (this._arguments.length > 0) {
  33. out = out.concat([
  34. 'Arguments:',
  35. this.argumentsHelp(),
  36. ''
  37. ]);
  38. }
  39. // Append USAGE file is any
  40. if (exists) {
  41. out.push(fs.readFileSync(filepath, 'utf8'));
  42. }
  43. return out.join('\n');
  44. };
  45. function formatArg(config) {
  46. let arg = `<${config.name}>`;
  47. if (!config.required) {
  48. arg = `[${arg}]`;
  49. }
  50. return arg;
  51. }
  52. /**
  53. * Output usage information for this given generator, depending on its arguments
  54. * or options
  55. */
  56. help.usage = function () {
  57. const options = Object.keys(this._options).length ? '[options]' : '';
  58. let name = this.options.namespace;
  59. let args = '';
  60. if (this._arguments.length > 0) {
  61. args = this._arguments.map(formatArg).join(' ') + ' ';
  62. }
  63. name = name.replace(/^yeoman:/, '');
  64. let out = `yo ${name} ${args}${options}`;
  65. if (this.description) {
  66. out += '\n\n' + this.description;
  67. }
  68. return out;
  69. };
  70. /**
  71. * Simple setter for custom `description` to append on help output.
  72. *
  73. * @param {String} description
  74. */
  75. help.desc = function (description) {
  76. this.description = description || '';
  77. return this;
  78. };
  79. /**
  80. * Get help text for arguments
  81. * @returns {String} Text of options in formatted table
  82. */
  83. help.argumentsHelp = function () {
  84. const rows = this._arguments.map(config => {
  85. return [
  86. '',
  87. config.name ? config.name : '',
  88. config.description ? `# ${config.description}` : '',
  89. config.type ? `Type: ${config.type.name}` : '',
  90. `Required: ${config.required}`
  91. ];
  92. });
  93. return table(rows);
  94. };
  95. /**
  96. * Get help text for options
  97. * @returns {String} Text of options in formatted table
  98. */
  99. help.optionsHelp = function () {
  100. const options = _.reject(this._options, x => x.hide);
  101. const rows = options.map(opt => {
  102. return [
  103. '',
  104. opt.alias ? `-${opt.alias}, ` : '',
  105. `--${opt.name}`,
  106. opt.description ? `# ${opt.description}` : '',
  107. (opt.default !== undefined && opt.default !== '') ? 'Default: ' + opt.default : ''
  108. ];
  109. });
  110. return table(rows);
  111. };