emeraldwalk / vscode-runonsave

Visual Studio Code extension to run commands whenever a file is saved.
Apache License 2.0
182 stars 57 forks source link

Request: captures in match argument made available as substitutions in command #19

Open fatbird opened 6 years ago

fatbird commented 6 years ago

I have a workspace that's a set of repos: a Meteor application and a variety of related npm modules. RunOnSave has been great for configuring babel to transpile whatever file I just saved, placing it based on the path by substitution of segment (basically, take the file path, and swap 'src' for 'lib').

For slightly more complicated scenarios (i.e., mocha testing), I'm unable to do the same because I need to access the repos' package.json, so I need to go into a node script to traverse up from the file. If captures in the match regex were made available as substitutions, it would simplify my task quite a bit and be generally pretty useful I think.

For example, If I could do this: "match": "code/client/(.+)/src/.+\\.js$" And then access it like this: "cmd": "${workspacePath}/${1}/node_modules/.bin/mocha" I could avoid dropping into a node script altogether.

I don't use typescript, but from looking at the code it looks like the following would be all that's needed:

  1. At 139, change RegExp(pattern).test to RegExp(pattern).exec; this would return the match and still function as a boolean at 153
  2. Inside the filter starting at 142, augment cfg with the match/notMatch results:

    cfg.matches = matchPattern.length === 0 || match(matchPattern) || [];
    cfg.notMatches = match(negatePattern) || [];
    return !cfg.notMatches && cfg.matches;
  3. After replacing environment variables, replace matches with something like this around 181:

    cfg.matches.forEach((match, ii) => {
      cmdStr = cmdStr.replace(`\${${ii}}`, match);
    });

https://codepen.io/anon/pen/jYQRZW?editors=0012

fatbird commented 6 years ago

Come to think of it, my babel task would also be similarly simplified by this, coming down to a one-liner instead of the node --eval I'm doing now.