ShellJS is a **portable** (Windows included) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands.
if (exec('git commit -am "Auto-commit"').code !== 0) {
echo('Error: Git commit failed');
exit(1);
}
```
### Global vs. Local
The example above uses the convenience script `shelljs/global` to reduce verbosity. If polluting your global namespace is not desirable, simply require `shelljs`.
Example:
```javascript
var shell = require('shelljs');
shell.echo('hello world');
```
### Make tool
A convenience script `shelljs/make` is also provided to mimic the behavior of a Unix Makefile. In this case all shell objects are global, and command line arguments will cause the script to execute only the corresponding function in the global `target` object. To avoid redundant calls, target functions are executed only once per script.
Example:
```javascript
//
// Example file: make.js
//
require('shelljs/make');
target.all = function() {
target.bundle();
target.docs();
}
// Bundle JS files
target.bundle = function() {
cd(__dirname);
mkdir('build');
cd('lib');
cat('*.js').to('../build/output.js');
}
// Generate docs
target.docs = function() {
cd(__dirname);
mkdir('docs');
cd('lib');
for (file in ls('*.js')) {
var text = grep('//@', file); // extract special comments
text.replace('//@', ''); // remove comment tags
text.to('docs/my_docs.md');
}
}
```
To run the target `all`, call the above script without arguments: `$ node make`. To run the target `docs`: `$ node make docs`, and so on.
### Installing
Via npm:
```bash
$ npm install shelljs
```
Or simply copy `shell.js` into your project's directory, and `require()` accordingly.
<!--
DO NOT MODIFY BEYOND THIS POINT - IT'S AUTOMATICALLY GENERATED
-->
# Command reference
All commands run synchronously, unless otherwise stated.
#### cd('dir')
Changes to directory `dir` for the duration of the script
#### pwd()
Returns the current directory.
#### ls([options ,] path [,path ...])
#### ls([options ,] path_array)
Available options:
+ `-R`: recursive
+ `-a`: all files (include files beginning with `.`)
Examples:
```javascript
ls('projs/*.js');
ls('-R', '/users/me', '/tmp');
ls('-R', ['/users/me', '/tmp']); // same as above
```
Returns list of files in the given path, or in current directory if no path provided.
For convenient iteration via `for (file in ls())`, the format returned is a hash object: