wait-on 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const minimist = require('minimist');
  4. const path = require('path');
  5. const waitOn = require('../');
  6. const interval = ['timeout', 'httpTimeout', 'tcpTimeout'];
  7. const minimistOpts = {
  8. string: ['c', 'd', 'i', 's', 't', 'w'].concat(interval),
  9. boolean: ['h', 'l', 'r', 'v'],
  10. alias: {
  11. c: 'config',
  12. d: 'delay',
  13. i: 'interval',
  14. l: 'log',
  15. r: 'reverse',
  16. s: 'simultaneous',
  17. t: 'timeout',
  18. v: 'verbose',
  19. w: 'window',
  20. h: 'help'
  21. }
  22. };
  23. const argv = minimist(process.argv.slice(2), minimistOpts);
  24. // if a js/json configuration file is provided require it
  25. const configOpts = argv.config ? require(path.resolve(argv.config)) : {};
  26. const hasResources = argv._.length || (configOpts.resources && configOpts.resources.length);
  27. if (argv.help || !hasResources) {
  28. // help
  29. fs.createReadStream(path.join(__dirname, '/usage.txt'))
  30. .pipe(process.stdout)
  31. .on('close', function () {
  32. process.exit(1);
  33. });
  34. } else {
  35. // if resources are present in the command line then they take
  36. // precedence over those in the config file.
  37. if (argv._.length) {
  38. configOpts.resources = argv._;
  39. }
  40. // now check for specific options and set those
  41. const opts = [
  42. 'delay',
  43. 'httpTimeout',
  44. 'interval',
  45. 'log',
  46. 'reverse',
  47. 'simultaneous',
  48. 'timeout',
  49. 'tcpTimeout',
  50. 'verbose',
  51. 'window'
  52. ].reduce(function (accum, x) {
  53. if (argv[x]) {
  54. let value = argv[x];
  55. if (interval.includes(x)) {
  56. value = parseInterval(value);
  57. }
  58. accum[x] = value;
  59. }
  60. return accum;
  61. }, configOpts);
  62. waitOn(opts, function (err) {
  63. if (err) {
  64. return errorExit(err);
  65. }
  66. // success, could just let it exit on its own, however since
  67. // rxjs window waits an extra loop before heeding the unsubscribe
  68. // we can exit to speed things up
  69. process.exit(0);
  70. });
  71. }
  72. function errorExit(err) {
  73. if (err.stack) {
  74. console.error(err.stack);
  75. } else {
  76. console.error(String(err));
  77. }
  78. process.exit(1);
  79. }
  80. function parseInterval(arg) {
  81. const res = /^([\d.]+)(|ms|s|m|h)$/i.exec(arg);
  82. if (!res) {
  83. return arg;
  84. }
  85. const value = parseFloat(res[1]);
  86. switch (res[2]) {
  87. case '':
  88. case 'ms': return Math.floor(value);
  89. case 's': return Math.floor(value * 1000);
  90. case 'm': return Math.floor(value * 1000 * 60);
  91. case 'h': return Math.floor(value * 1000 * 60 * 60);
  92. }
  93. }