zkat / npx

execute npm package binaries (moved)
https://github.com/npm/npx
Other
2.63k stars 105 forks source link

be able to pre-parse `npm` commands #55

Closed gabrielcsapo closed 7 years ago

gabrielcsapo commented 7 years ago

Would it be possible for npx to take an npm command like install tap-html@0.0.4 and be able to parse it into an AST into which people could abstract the desired behavior of that command?

So install tap-html@0.0.4 would turn into;

{
    "command": "install",
    "module": "tap-html",
    "version": "0.0.4"
}

needed for https://github.com/local-npm/local-npm/issues/154 🤕

Arguably the code could be as simple as

var string = "install tap-html@0.0.4";
parse(s); 
// |
// V
// {
//     "command": "install",
//     "module": "tap-html",
//     "version": "0.0.4"
// }

var parse = function parse(s) {
    var command = s.split(' ');
    var modV = command[1].split('@');

    return {
        "command": command[0],
        "module": modV[0],
        "version": modV[1]
    }
}

but just thought it would be something interesting to add to npx!

zkat commented 7 years ago

@gabrielcsapo this is almost certainly not something that makes sense to have in npx itself.

If you want to replicate npm's CLI parsing, you can do it using two things: yargs and npm-package-arg. You use yargs to figure out the npm command (taking into account all the --flags), and then use npm-package-arg on the install targets, which will give you an object with all the information you're looking for here.