Нет описания

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const reorder = require('./lib/reorder');
  2. const respawn = require('./lib/respawn');
  3. const remover = require('./lib/remover');
  4. const FORBID_RESPAWNING_FLAG = '--no-respawning';
  5. module.exports = function (flags, argv, forcedFlags, execute) {
  6. if (!flags) {
  7. throw new Error('You must specify flags to respawn with.');
  8. }
  9. if (!argv) {
  10. throw new Error('You must specify an argv array.');
  11. }
  12. if (typeof forcedFlags === 'function') {
  13. execute = forcedFlags;
  14. forcedFlags = [];
  15. }
  16. if (typeof forcedFlags === 'string') {
  17. forcedFlags = [forcedFlags];
  18. }
  19. if (!Array.isArray(forcedFlags)) {
  20. forcedFlags = [];
  21. }
  22. var index = argv.indexOf(FORBID_RESPAWNING_FLAG);
  23. if (index >= 0) {
  24. argv = argv.slice(0, index).concat(argv.slice(index + 1));
  25. argv = remover(flags, argv);
  26. execute(true, process, argv);
  27. return;
  28. }
  29. var proc = process;
  30. var reordered = reorder(flags, argv);
  31. var ready = JSON.stringify(argv) === JSON.stringify(reordered);
  32. if (forcedFlags.length) {
  33. reordered = reordered.slice(0, 1)
  34. .concat(forcedFlags)
  35. .concat(reordered.slice(1));
  36. ready = false;
  37. }
  38. if (!ready) {
  39. reordered.push(FORBID_RESPAWNING_FLAG);
  40. proc = respawn(reordered);
  41. }
  42. execute(ready, proc, reordered);
  43. };