Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.3 KiB

  1. 'use strict';
  2. const path = require('path');
  3. const buildMinimistOptions = require('minimist-options');
  4. const minimist = require('minimist');
  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 loudRejection = require('loud-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.filename);
  15. module.exports = (helpMessage, opts) => {
  16. loudRejection();
  17. if (typeof helpMessage === 'object' && !Array.isArray(helpMessage)) {
  18. opts = helpMessage;
  19. helpMessage = '';
  20. }
  21. opts = Object.assign({
  22. pkg: readPkgUp.sync({
  23. cwd: parentDir,
  24. normalize: false
  25. }).pkg || {},
  26. argv: process.argv.slice(2),
  27. inferType: false,
  28. input: 'string',
  29. help: helpMessage,
  30. autoHelp: true,
  31. autoVersion: true
  32. }, opts);
  33. let minimistOpts = Object.assign({
  34. arguments: opts.input
  35. }, opts.flags);
  36. minimistOpts = decamelizeKeys(minimistOpts, '-', {exclude: ['stopEarly', '--']});
  37. if (opts.inferType) {
  38. delete minimistOpts.arguments;
  39. }
  40. minimistOpts = buildMinimistOptions(minimistOpts);
  41. const pkg = opts.pkg;
  42. const argv = minimist(opts.argv, minimistOpts);
  43. let help = redent(trimNewlines((opts.help || '').replace(/\t+\n*$/, '')), 2);
  44. normalizePackageData(pkg);
  45. process.title = pkg.bin ? Object.keys(pkg.bin)[0] : pkg.name;
  46. let description = opts.description;
  47. if (!description && description !== false) {
  48. description = pkg.description;
  49. }
  50. help = (description ? `\n ${description}\n` : '') + (help ? `\n${help}\n` : '\n');
  51. const showHelp = code => {
  52. console.log(help);
  53. process.exit(typeof code === 'number' ? code : 2);
  54. };
  55. const showVersion = () => {
  56. console.log(typeof opts.version === 'string' ? opts.version : pkg.version);
  57. process.exit();
  58. };
  59. if (argv.version && opts.autoVersion) {
  60. showVersion();
  61. }
  62. if (argv.help && opts.autoHelp) {
  63. showHelp(0);
  64. }
  65. const input = argv._;
  66. delete argv._;
  67. const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});
  68. return {
  69. input,
  70. flags,
  71. pkg,
  72. help,
  73. showHelp,
  74. showVersion
  75. };
  76. };