Sin descripción

index.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var path = require('path');
  3. var minimist = require('minimist');
  4. var objectAssign = require('object-assign');
  5. var camelcaseKeys = require('camelcase-keys');
  6. var decamelize = require('decamelize');
  7. var mapObj = require('map-obj');
  8. var trimNewlines = require('trim-newlines');
  9. var redent = require('redent');
  10. var readPkgUp = require('read-pkg-up');
  11. var loudRejection = require('loud-rejection');
  12. var normalizePackageData = require('normalize-package-data');
  13. // get the uncached parent
  14. delete require.cache[__filename];
  15. var parentDir = path.dirname(module.parent.filename);
  16. module.exports = function (opts, minimistOpts) {
  17. loudRejection();
  18. if (Array.isArray(opts) || typeof opts === 'string') {
  19. opts = {help: opts};
  20. }
  21. opts = objectAssign({
  22. pkg: readPkgUp.sync({
  23. cwd: parentDir,
  24. normalize: false
  25. }).pkg,
  26. argv: process.argv.slice(2)
  27. }, opts);
  28. minimistOpts = objectAssign({}, minimistOpts);
  29. minimistOpts.default = mapObj(minimistOpts.default || {}, function (key, value) {
  30. return [decamelize(key, '-'), value];
  31. });
  32. if (Array.isArray(opts.help)) {
  33. opts.help = opts.help.join('\n');
  34. }
  35. var pkg = typeof opts.pkg === 'string' ? require(path.join(parentDir, opts.pkg)) : opts.pkg;
  36. var argv = minimist(opts.argv, minimistOpts);
  37. var help = redent(trimNewlines(opts.help || ''), 2);
  38. normalizePackageData(pkg);
  39. process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
  40. var description = opts.description;
  41. if (!description && description !== false) {
  42. description = pkg.description;
  43. }
  44. help = (description ? '\n ' + description + '\n' : '') + (help ? '\n' + help : '\n');
  45. var showHelp = function (code) {
  46. console.log(help);
  47. process.exit(code || 0);
  48. };
  49. if (argv.version && opts.version !== false) {
  50. console.log(typeof opts.version === 'string' ? opts.version : pkg.version);
  51. process.exit();
  52. }
  53. if (argv.help && opts.help !== false) {
  54. showHelp();
  55. }
  56. var _ = argv._;
  57. delete argv._;
  58. return {
  59. input: _,
  60. flags: camelcaseKeys(argv),
  61. pkg: pkg,
  62. help: help,
  63. showHelp: showHelp
  64. };
  65. };