index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var parse = require('./parse');
  2. /**
  3. * Converts a string to command line args, in particular
  4. * groups together quoted values.
  5. * This is a utility function to allow calling nodemon as a required
  6. * library, but with the CLI args passed in (instead of an object).
  7. *
  8. * @param {String} string
  9. * @return {Array}
  10. */
  11. function stringToArgs(string) {
  12. var args = [];
  13. var parts = string.split(' ');
  14. var length = parts.length;
  15. var i = 0;
  16. var open = false;
  17. var grouped = '';
  18. var lead = '';
  19. for (; i < length; i++) {
  20. lead = parts[i].substring(0, 1);
  21. if (lead === '"' || lead === '\'') {
  22. open = lead;
  23. grouped = parts[i].substring(1);
  24. } else if (open && parts[i].slice(-1) === open) {
  25. open = false;
  26. grouped += ' ' + parts[i].slice(0, -1);
  27. args.push(grouped);
  28. } else if (open) {
  29. grouped += ' ' + parts[i];
  30. } else {
  31. args.push(parts[i]);
  32. }
  33. }
  34. return args;
  35. }
  36. module.exports = {
  37. parse: function (argv) {
  38. if (typeof argv === 'string') {
  39. argv = stringToArgs(argv);
  40. }
  41. return parse(argv);
  42. },
  43. };