As someone who has been a kind of windows, and Linux dual boot type person for over ten years now, it would be great to have some way of always having some of the commands I have grown to like when working in a Linux environment always with me regardless of the operating system environment that i am working with when making a node.js project. Lucky for me there is a project called shell.js that can help with this.
To use shell.js as a module in a node.js project just install it in the usual manner when adding a dependency to a project like normal.
Many of the demos I will be writing about with this project will involve using shelljs this way, if you want to use the commands as one liners from the command line interface in windows that can be done aw well by installing shelljs globally.
The which method searches for a native command in the systems path, which is useful to find out if something like git or mongodb is installed on the system or not.
If you are familiar with the child-process module, then you should also be familiar with exec and spawn.
As such an exec method is given for convenience for launching external commands.
An optional options object, and callback can be given to exec as well.
Using the silent option prevents exec from logging the raw standard output to the console. In addition I can give a callback to exec that will give me the exit code, as well as the standard output, and standard error in the event of an error.
Maybe one of the best know unix like commands out there is grep, which is used for text pattern matching tasks.
One of the best features of a unix style Command Line Interface is the ability to pipe commands, or in other words supply the input of a command as the output of another. This can be done by chaining commands.
This as you would expect results in a list of *.js files begin with the text pattern ‘app_’.
Although shell.js can be used globally, as a way to bring Linux like commands to windows the developers recommend against this. However if you really want to it can be done easly by just installing the script globally.
Once shelljs is installed globally, A command can then be used by calling shjs, and then the command you want to use.
Shelljs looks like a great solution for making unix like commands more portable across different operating systems. Even when developing, and deploying with posix environments I would say it is a nice alternative to fiddling with the child-process module.