Turn npm package scripts into CLI commands
Since npm 2.0, you can pass arguments to scripts... wait... what if you could use that for creating CLIs? Homerun is a little experiment that lets you just do that. If you need more, I highly recommend minimist.
Let's say you have a script called
add that you can run this way:
npm run add -- 1 23
npm install homerun --save
Add it to your
"name": "cli""bin": "./node_modules/.bin/homerun" // <-- here"scripts":"add": "node commands/add"
Now, without any other setup, if you
npm link or
npm publish you get a CLI for free:
cli add 1 23
And of course, while you develop, you can still use
npm run add -- 1 2 to test your command.
Homerun will use these scripts in case no command is provided or matches.
"scripts":"index": "node commands/index" // no command provided"unknown": "node commands/help" // unknown command provided
If you need to customize, homerun can be used as a module.
// index.jsvar homerun =var scripts = scripts
// package.json"bin": "index.js"
To test your commands, you can use
var assert =var argv = 'add' '1' '2'
Homerun doesn't support multiple commands. For example,
echo foo && echo bar won't work.
MIT - Typicode