2012-03-02 12:21:53 -05:00
2012-03-02 12:04:10 -05:00
2012-03-02 11:30:42 -05:00
2012-03-01 20:22:44 -05:00
2012-03-01 20:22:44 -05:00
2012-03-02 11:38:34 -05:00
2012-03-02 12:21:53 -05:00
2012-03-02 12:21:53 -05:00

Shell.js - Unix shell commands for Node.js Build Status

Why? Because you want a portable (Windows included) solution to your shell scripts, and you're already familiar with Unix shell commands. No dependencies other than Node.js. No asynchronous callback hell. Just good old shell scripting.

Shell.js is unit-tested and battle-tested at Mozilla's pdf.js.

Example

require('shell/global');

// Copy files to release dir

mkdir('-p', 'out/Release');
cp('-R', 'lib/*.js', 'out/Release');

// Replace macros in each .js file

cd('lib');
for (file in ls('*.js')) {
  sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
  sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
}
cd('..');

// Run external tool synchronously

if (exec('git commit -am "Auto-commit"').code !== 0) {
  echo('Error: Git commit failed');
  exit(1);
}

See also scripts/ for real-life examples.

API reference

cd('dir')

Changes to directory dir for the duration of the script

shell.pwd()

Returns the current directory.

ls([options] [,path] [,path ...])

Available options:

  • -R: recursive
  • -a: all files (include files beginning with .)

Examples:

ls('projs/*.js');
ls('-R', '/users/me', '/tmp');

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: { 'file1':null, 'dir1/file2':null, ...}.

cp('[options ,] source [,source ...] , dest')

Available options:

  • -f: force
  • -r, -R: recursive

Examples:

cp('file1', 'dir1');
cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');

Copies files. The wildcard * is accepted.

rm([options ,] file [, file ...])

Available options:

  • -f: force
  • -r, -R: recursive

Examples:

rm('some_file.txt', 'another_file.txt');
rm('-rf', '/tmp/*');

Removes files. The wildcard * is accepted.

mv(source [, source ...], dest')

Available options:

  • f: force

Moves files. The wildcard * is accepted.

mkdir([options ,] dir [, dir ...]')

Available options:

  • p: full path (will create intermediate dirs if necessary)

Examples:

mkdir('-p', '/tmp/a/b/c/d');

Creates directories.

cat(file [, file ...]')

Examples:

var str = cat('file*.txt');

Returns a string containing the given file, or a concatenated string containing the files if more than one file is given (a new line character is introduced between each file). Wildcard * accepted.

'string'.to(file)

Examples:

cat('input.txt').to('output.txt');

Analogous to the redirection operator > in Unix, but works with JavaScript strings (such as those returned by cat, grep, etc). Like Unix redirections, to() will overwrite any existing file!

sed([options ,] search_regex, replace_str, file)

Available options:

  • -i: Replace contents of 'file' in-place. Note that no backups will be created!

Examples:

sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');

Reads an input string from file and performs a JavaScript replace() on the input using the given search regex and replacement string. Returns the new string after replacement.

grep(regex_filter, file [, file ...]')

Examples:

grep('GLOBAL_VARIABLE', '*.js');

Reads input string from given files and returns a string containing all lines of the file that match the given regex_filter. Wildcard * accepted.

which(command)

Examples:

var nodeExec = which('node');

Searches for command in the system's PATH. On Windows looks for .exe, .cmd, and .bat extensions. Returns string containing the absolute path to the command.

echo(string [,string ...])

Examples:

echo('hello world');
var str = echo('hello world');

Prints string to stdout, and returns string with additional utility methods like .to().

exit(code)

Exits the current process with the given exit code.

env['VAR_NAME']

Object containing environment variables (both getter and setter). Shortcut to process.env.

exec(command [, options] [, callback])

Available options (all false by default):

  • async: Asynchronous execution. Needs callback.
  • silent: Do not echo program output to console.

Examples:

var version = exec('node --version', {silent:true}).output;

Executes the given command synchronously, unless otherwise specified. When in synchronous mode returns the object { code:..., output:... }, containing the program's output (stdout + stderr) and its exit code. Otherwise the callback gets the arguments (code, output).

Non-Unix commands

tempdir()

Searches and returns string containing a writeable, platform-dependent temporary directory. Follows Python's tempfile algorithm.

exists(path [, path ...])

Returns true if all the given paths exist.

error()

Tests if error occurred in the last command. Returns null if no error occurred, otherwise returns string explaining the error

verbose()

Enables all output (default)

silent()

Suppresses all output, except for explict echo() calls

Description
🐚 Portable Unix shell commands for Node.js
Readme BSD-3-Clause 4.9 MiB
Languages
JavaScript 100%