DEV Community

Discussion on: A simple way to replace npm scripts in Deno

Collapse
 
09wattry profile image
Ryan

github.com/BentoumiTech/denox/pull/15 I took the liberty to add the functionality you're looking for. Denox is a pretty awesome package so hopefully, this gets picked up.

Thread Thread
 
vonheikemen profile image
Heiker

That's awesome. But I'm a little bit worried by this right here: const args = script.split(' ');, it doesn't handle quoted arguments. If you give it something like this

some-cli-tool -m "a message" -m "some other message"

You would get.

["some-cli-tool" "-m", "\"a", "message\"", "-m", "\"some", "other", "message\"" ]

Would Deno.run handle that?

Thread Thread
 
09wattry profile image
Ryan

That's a great observation. Do you have any suggestions on how to handle this case?

Thread Thread
 
vonheikemen profile image
Heiker

I found this package: exec. It does the same thing as that function with more options. You could use that. It only handles double quotes but it's an improvement.

If anyone is curious how they do it, here it is.

function splitCommand(command) {
  var myRegexp = /[^\s"]+|"([^"]*)"/gi;
  var splits = [];

  do {
    //Each call to exec returns the next regex match as an array
    var match = myRegexp.exec(command);
    if (match != null) {
      //Index 1 in the array is the captured group if it exists
      //Index 0 is the matched text, which we use if no captured group exists
      splits.push(match[1] ? match[1] : match[0]);
    }
  } while (match != null);

  return splits;
}
Thread Thread
 
09wattry profile image
Ryan • Edited

What about double quotes enclosed within single quotes, echo 'And he said: "My name is!" ${name}'. It's a contrived example but you get the gist.

Thread Thread
 
vonheikemen profile image
Heiker

For that you would get.

[ "echo", "'And", "he", "said:", "My name is!", "aa'" ]

See, the double quotes stayed, but everything else is broken. Having a bulletproof regex is a real pain. If you want to improve it, you have to modify this var myRegexp = /[^\s"]+|"([^"]*)"/gi;.