My last post on Command Line Tools is missing some questions that you might find valuable when creating your own command line tools.
A) Can I create multiple command line tools within one package?
Yes. Just add new scripts and the associated keys into your package.json
Lets say we would like to write ccap foo instead of cap -c foo and
dcap FOO instead of cap -d FOO.
First we will create the 2 missing scripts in our bin directory and make them executable
Hector:~/capitalize j$ cd bin
Hector:~/capitalize/bin $ touch ccap.js
Hector:~/capitalize/bin $ chmod +x ccap.js
Hector:~/capitalize/bin $ chmod +x dcap.js
Then add the following code into this new files (ccap.js and dcap.js)
Then change the the package.json to have a new version number and to include the new entries within the bin key.
Finally publish the package and install it globally
Hector:~/capitalize $ npm update
Hector:~/capitalize $ cd ..
Hector:~ $ sudo npm install capitalize -g
/opt/local/bin/ccap -> /opt/local/lib/node_modules/capitalize/bin/ccap.js
/opt/local/bin/dcap -> /opt/local/lib/node_modules/capitalize/bin/dcap.js
/opt/local/bin/cap -> /opt/local/lib/node_modules/capitalize/bin/cap.js
As you see ccap and dcap are now linked into /opt/local/bin and available anywhere on your command line
B) Can I install a command line tool only locally?
Yes - but what is the purpose of that?!
If you install a package containing a command line tool locally then you would have to create a symlink yourself and you need adjust your PATH variable manually to include the symlink etc.
C) Can I install a command line tool that overwrites an already installed one?
Lets say we want to overwrite the coffee command as we have already installed CoffeeScript on our machine. First we will change the version number of our package again and add a new key called coffee to our bin key.
Now publish and install globally
Hector:~/capitalize robertj$ npm publish
Hector:~/capitalize robertj$ cd ..
Hector:~ robertj$ sudo npm install -g capitalize
npm ERR! error installing email@example.com
/usr/local/bin/cap -> /usr/local/lib/node_modules/capitalize/bin/cap.js
/usr/local/bin/ccap -> /usr/local/lib/node_modules/capitalize/bin/ccap.js
/usr/local/bin/dcap -> /usr/local/lib/node_modules/capitalize/bin/dcap.js
npm ERR! error rolling back firstname.lastname@example.org Error: Refusing to delete: /usr/local/bin/coffee not in /usr/local/lib/node_modules/capitalize
Unluckily for you from now on you will be punished. The package has been installed partly and any attempt to update or uninstall it will be greeted with the message that /usr/local/bin/coffee can't be deleted.
Your last help is to use the force to uninstall
Hector:~ robertj$ sudo npm uninstall --force -g capitalize
I hope you enjoyed the show. Next time as promised "pre/post/installation" scripts.