index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export { parseArgsStringToArgv };
  2. export default function parseArgsStringToArgv(value, env, file) {
  3. // ([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*) Matches nested quotes until the first space outside of quotes
  4. // [^\s'"]+ or Match if not a space ' or "
  5. // (['"])([^\5]*?)\5 or Match "quoted text" without quotes
  6. // `\3` and `\5` are a backreference to the quote style (' or ") captured
  7. var myRegexp = /([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*)|[^\s'"]+|(['"])([^\5]*?)\5/gi;
  8. var myString = value;
  9. var myArray = [];
  10. if (env) {
  11. myArray.push(env);
  12. }
  13. if (file) {
  14. myArray.push(file);
  15. }
  16. var match;
  17. do {
  18. // Each call to exec returns the next regex match as an array
  19. match = myRegexp.exec(myString);
  20. if (match !== null) {
  21. // Index 1 in the array is the captured group if it exists
  22. // Index 0 is the matched text, which we use if no captured group exists
  23. myArray.push(firstString(match[1], match[6], match[0]));
  24. }
  25. } while (match !== null);
  26. return myArray;
  27. }
  28. // Accepts any number of arguments, and returns the first one that is a string
  29. // (even an empty string)
  30. function firstString() {
  31. var args = [];
  32. for (var _i = 0; _i < arguments.length; _i++) {
  33. args[_i] = arguments[_i];
  34. }
  35. for (var i = 0; i < args.length; i++) {
  36. var arg = args[i];
  37. if (typeof arg === "string") {
  38. return arg;
  39. }
  40. }
  41. }