index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict';
  2. const path = require('path');
  3. const buildParserOptions = require('minimist-options');
  4. const parseArguments = require('yargs-parser');
  5. const camelCaseKeys = require('camelcase-keys');
  6. const decamelizeKeys = require('decamelize-keys');
  7. const trimNewlines = require('trim-newlines');
  8. const redent = require('redent');
  9. const readPkgUp = require('read-pkg-up');
  10. const hardRejection = require('hard-rejection');
  11. const normalizePackageData = require('normalize-package-data');
  12. // Prevent caching of this module so module.parent is always accurate
  13. delete require.cache[__filename];
  14. const parentDir = path.dirname(module.parent && module.parent.filename ? module.parent.filename : '.');
  15. const isFlagMissing = (flagName, definedFlags, receivedFlags, input) => {
  16. const flag = definedFlags[flagName];
  17. let isFlagRequired = true;
  18. if (typeof flag.isRequired === 'function') {
  19. isFlagRequired = flag.isRequired(receivedFlags, input);
  20. if (typeof isFlagRequired !== 'boolean') {
  21. throw new TypeError(`Return value for isRequired callback should be of type boolean, but ${typeof isFlagRequired} was returned.`);
  22. }
  23. }
  24. if (typeof receivedFlags[flagName] === 'undefined') {
  25. return isFlagRequired;
  26. }
  27. return flag.isMultiple && receivedFlags[flagName].length === 0;
  28. };
  29. const getMissingRequiredFlags = (flags, receivedFlags, input) => {
  30. const missingRequiredFlags = [];
  31. if (typeof flags === 'undefined') {
  32. return [];
  33. }
  34. for (const flagName of Object.keys(flags)) {
  35. if (flags[flagName].isRequired && isFlagMissing(flagName, flags, receivedFlags, input)) {
  36. missingRequiredFlags.push({key: flagName, ...flags[flagName]});
  37. }
  38. }
  39. return missingRequiredFlags;
  40. };
  41. const reportMissingRequiredFlags = missingRequiredFlags => {
  42. console.error(`Missing required flag${missingRequiredFlags.length > 1 ? 's' : ''}`);
  43. for (const flag of missingRequiredFlags) {
  44. console.error(`\t--${flag.key}${flag.alias ? `, -${flag.alias}` : ''}`);
  45. }
  46. };
  47. const buildParserFlags = ({flags, booleanDefault}) =>
  48. Object.entries(flags).reduce((parserFlags, [flagKey, flagValue]) => {
  49. const flag = {...flagValue};
  50. if (
  51. typeof booleanDefault !== 'undefined' &&
  52. flag.type === 'boolean' &&
  53. !Object.prototype.hasOwnProperty.call(flag, 'default')
  54. ) {
  55. flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault;
  56. }
  57. if (flag.isMultiple) {
  58. flag.type = flag.type ? `${flag.type}-array` : 'array';
  59. delete flag.isMultiple;
  60. }
  61. parserFlags[flagKey] = flag;
  62. return parserFlags;
  63. }, {});
  64. const validateFlags = (flags, options) => {
  65. for (const [flagKey, flagValue] of Object.entries(options.flags)) {
  66. if (flagKey !== '--' && !flagValue.isMultiple && Array.isArray(flags[flagKey])) {
  67. throw new Error(`The flag --${flagKey} can only be set once.`);
  68. }
  69. }
  70. };
  71. const meow = (helpText, options) => {
  72. if (typeof helpText !== 'string') {
  73. options = helpText;
  74. helpText = '';
  75. }
  76. options = {
  77. pkg: readPkgUp.sync({
  78. cwd: parentDir,
  79. normalize: false
  80. }).packageJson || {},
  81. argv: process.argv.slice(2),
  82. flags: {},
  83. inferType: false,
  84. input: 'string',
  85. help: helpText,
  86. autoHelp: true,
  87. autoVersion: true,
  88. booleanDefault: false,
  89. hardRejection: true,
  90. ...options
  91. };
  92. if (options.hardRejection) {
  93. hardRejection();
  94. }
  95. let parserOptions = {
  96. arguments: options.input,
  97. ...buildParserFlags(options)
  98. };
  99. parserOptions = decamelizeKeys(parserOptions, '-', {exclude: ['stopEarly', '--']});
  100. if (options.inferType) {
  101. delete parserOptions.arguments;
  102. }
  103. parserOptions = buildParserOptions(parserOptions);
  104. if (parserOptions['--']) {
  105. parserOptions.configuration = {
  106. ...parserOptions.configuration,
  107. 'populate--': true
  108. };
  109. }
  110. const {pkg} = options;
  111. const argv = parseArguments(options.argv, parserOptions);
  112. let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2);
  113. normalizePackageData(pkg);
  114. process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
  115. let {description} = options;
  116. if (!description && description !== false) {
  117. ({description} = pkg);
  118. }
  119. help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
  120. const showHelp = code => {
  121. console.log(help);
  122. process.exit(typeof code === 'number' ? code : 2);
  123. };
  124. const showVersion = () => {
  125. console.log(typeof options.version === 'string' ? options.version : pkg.version);
  126. process.exit(0);
  127. };
  128. if (argv._.length === 0 && options.argv.length === 1) {
  129. if (argv.version === true && options.autoVersion) {
  130. showVersion();
  131. }
  132. if (argv.help === true && options.autoHelp) {
  133. showHelp(0);
  134. }
  135. }
  136. const input = argv._;
  137. delete argv._;
  138. const flags = camelCaseKeys(argv, {exclude: ['--', /^\w$/]});
  139. const unnormalizedFlags = {...flags};
  140. validateFlags(flags, options);
  141. for (const flagValue of Object.values(options.flags)) {
  142. delete flags[flagValue.alias];
  143. }
  144. const missingRequiredFlags = getMissingRequiredFlags(options.flags, flags, input);
  145. if (missingRequiredFlags.length > 0) {
  146. reportMissingRequiredFlags(missingRequiredFlags);
  147. process.exit(2);
  148. }
  149. return {
  150. input,
  151. flags,
  152. unnormalizedFlags,
  153. pkg,
  154. help,
  155. showHelp,
  156. showVersion
  157. };
  158. };
  159. module.exports = meow;