index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 reportUnknownFlags = unknownFlags => {
  48. console.error([
  49. `Unknown flag${unknownFlags.length > 1 ? 's' : ''}`,
  50. ...unknownFlags
  51. ].join('\n'));
  52. };
  53. const buildParserFlags = ({flags, booleanDefault}) => {
  54. const parserFlags = {};
  55. for (const [flagKey, flagValue] of Object.entries(flags)) {
  56. const flag = {...flagValue};
  57. if (
  58. typeof booleanDefault !== 'undefined' &&
  59. flag.type === 'boolean' &&
  60. !Object.prototype.hasOwnProperty.call(flag, 'default')
  61. ) {
  62. flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault;
  63. }
  64. if (flag.isMultiple) {
  65. flag.type = flag.type ? `${flag.type}-array` : 'array';
  66. flag.default = flag.default || [];
  67. delete flag.isMultiple;
  68. }
  69. parserFlags[flagKey] = flag;
  70. }
  71. return parserFlags;
  72. };
  73. const validateFlags = (flags, options) => {
  74. for (const [flagKey, flagValue] of Object.entries(options.flags)) {
  75. if (flagKey !== '--' && !flagValue.isMultiple && Array.isArray(flags[flagKey])) {
  76. throw new Error(`The flag --${flagKey} can only be set once.`);
  77. }
  78. }
  79. };
  80. const meow = (helpText, options) => {
  81. if (typeof helpText !== 'string') {
  82. options = helpText;
  83. helpText = '';
  84. }
  85. const foundPkg = readPkgUp.sync({
  86. cwd: parentDir,
  87. normalize: false
  88. });
  89. options = {
  90. pkg: foundPkg ? foundPkg.packageJson : {},
  91. argv: process.argv.slice(2),
  92. flags: {},
  93. inferType: false,
  94. input: 'string',
  95. help: helpText,
  96. autoHelp: true,
  97. autoVersion: true,
  98. booleanDefault: false,
  99. hardRejection: true,
  100. allowUnknownFlags: true,
  101. ...options
  102. };
  103. if (options.hardRejection) {
  104. hardRejection();
  105. }
  106. let parserOptions = {
  107. arguments: options.input,
  108. ...buildParserFlags(options)
  109. };
  110. parserOptions = decamelizeKeys(parserOptions, '-', {exclude: ['stopEarly', '--']});
  111. if (options.inferType) {
  112. delete parserOptions.arguments;
  113. }
  114. parserOptions = buildParserOptions(parserOptions);
  115. parserOptions.configuration = {
  116. ...parserOptions.configuration,
  117. 'greedy-arrays': false
  118. };
  119. if (parserOptions['--']) {
  120. parserOptions.configuration['populate--'] = true;
  121. }
  122. if (!options.allowUnknownFlags) {
  123. // Collect unknown options in `argv._` to be checked later.
  124. parserOptions.configuration['unknown-options-as-args'] = true;
  125. }
  126. const {pkg} = options;
  127. const argv = parseArguments(options.argv, parserOptions);
  128. let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2);
  129. normalizePackageData(pkg);
  130. process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
  131. let {description} = options;
  132. if (!description && description !== false) {
  133. ({description} = pkg);
  134. }
  135. help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
  136. const showHelp = code => {
  137. console.log(help);
  138. process.exit(typeof code === 'number' ? code : 2);
  139. };
  140. const showVersion = () => {
  141. console.log(typeof options.version === 'string' ? options.version : pkg.version);
  142. process.exit(0);
  143. };
  144. if (argv._.length === 0 && options.argv.length === 1) {
  145. if (argv.version === true && options.autoVersion) {
  146. showVersion();
  147. }
  148. if (argv.help === true && options.autoHelp) {
  149. showHelp(0);
  150. }
  151. }
  152. const input = argv._;
  153. delete argv._;
  154. if (!options.allowUnknownFlags) {
  155. const unknownFlags = input.filter(item => typeof item === 'string' && item.startsWith('-'));
  156. if (unknownFlags.length > 0) {
  157. reportUnknownFlags(unknownFlags);
  158. process.exit(2);
  159. }
  160. }
  161. const flags = camelCaseKeys(argv, {exclude: ['--', /^\w$/]});
  162. const unnormalizedFlags = {...flags};
  163. validateFlags(flags, options);
  164. for (const flagValue of Object.values(options.flags)) {
  165. delete flags[flagValue.alias];
  166. }
  167. const missingRequiredFlags = getMissingRequiredFlags(options.flags, flags, input);
  168. if (missingRequiredFlags.length > 0) {
  169. reportMissingRequiredFlags(missingRequiredFlags);
  170. process.exit(2);
  171. }
  172. return {
  173. input,
  174. flags,
  175. unnormalizedFlags,
  176. pkg,
  177. help,
  178. showHelp,
  179. showVersion
  180. };
  181. };
  182. module.exports = meow;