add support to array arguments

This commit is contained in:
Artur Adib 2012-03-02 15:51:44 -05:00
parent d93c4d1022
commit e76cfa9fa1
8 changed files with 160 additions and 49 deletions

View File

@ -111,7 +111,8 @@ Changes to directory `dir` for the duration of the script
#### pwd()
Returns the current directory.
#### ls([options] [,path] [,path ...])
#### ls([options ,] path [,path ...])
#### ls([options ,] path_array)
Available options:
+ `-R`: recursive
@ -122,13 +123,15 @@ 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:
`{ 'file1':null, 'dir1/file2':null, ...}`.
#### cp('[options ,] source [,source ...] , dest')
#### cp('[options ,] source [,source ...], dest')
#### cp('[options ,] source_array, dest')
Available options:
+ `-f`: force
@ -139,11 +142,13 @@ Examples:
```javascript
cp('file1', 'dir1');
cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above
```
Copies files. The wildcard `*` is accepted.
#### rm([options ,] file [, file ...])
#### rm([options ,] file_array)
Available options:
+ `-f`: force
@ -152,20 +157,31 @@ Available options:
Examples:
```javascript
rm('some_file.txt', 'another_file.txt');
rm('-rf', '/tmp/*');
rm('some_file.txt', 'another_file.txt');
rm(['some_file.txt', 'another_file.txt']); // same as above
```
Removes files. The wildcard `*` is accepted.
#### mv(source [, source ...], dest')
#### mv(source_array, dest')
Available options:
+ `f`: force
Examples:
```javascript
mv('-f', 'file', 'dir/');
mv('file1', 'file2', 'dir/');
mv(['file1', 'file2'], 'dir/'); // same as above
```
Moves files. The wildcard `*` is accepted.
#### mkdir([options ,] dir [, dir ...]')
#### mkdir([options ,] dir [, dir ...])
#### mkdir([options ,] dir_array)
Available options:
+ `p`: full path (will create intermediate dirs if necessary)
@ -173,17 +189,21 @@ Available options:
Examples:
```javascript
mkdir('-p', '/tmp/a/b/c/d');
mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
```
Creates directories.
#### cat(file [, file ...]')
#### cat(file [, file ...])
#### cat(file_array)
Examples:
```javascript
var str = cat('file*.txt');
var str = cat('file1', 'file2');
var str = cat(['file1', 'file2']); // same as above
```
Returns a string containing the given file, or a concatenated string
@ -216,7 +236,8 @@ 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 ...]')
#### grep(regex_filter, file [, file ...])
#### grep(regex_filter, file_array)
Examples:
@ -281,6 +302,7 @@ Searches and returns string containing a writeable, platform-dependent temporary
Follows Python's [tempfile algorithm](http://docs.python.org/library/tempfile.html#tempfile.tempdir).
#### exists(path [, path ...])
#### exists(path_array)
Returns true if all the given paths exist.
#### error()

View File

@ -58,7 +58,8 @@ function _pwd(options) {
exports.pwd = wrap('pwd', _pwd);
//@
//@ #### ls([options] [,path] [,path ...])
//@ #### ls([options ,] path [,path ...])
//@ #### ls([options ,] path_array)
//@ Available options:
//@
//@ + `-R`: recursive
@ -69,6 +70,7 @@ exports.pwd = wrap('pwd', _pwd);
//@ ```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.
@ -158,7 +160,8 @@ exports.ls = wrap('ls', _ls);
//@
//@ #### cp('[options ,] source [,source ...] , dest')
//@ #### cp('[options ,] source [,source ...], dest')
//@ #### cp('[options ,] source_array, dest')
//@ Available options:
//@
//@ + `-f`: force
@ -169,6 +172,7 @@ exports.ls = wrap('ls', _ls);
//@ ```javascript
//@ cp('file1', 'dir1');
//@ cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
//@ cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above
//@ ```
//@
//@ Copies files. The wildcard `*` is accepted.
@ -185,8 +189,12 @@ function _cp(options, sources, dest) {
} else if (arguments.length > 3) {
sources = [].slice.call(arguments, 1, arguments.length - 1);
dest = arguments[arguments.length - 1];
} else {
} else if (typeof sources === 'string') {
sources = [sources];
} else if ('length' in sources) {
sources = sources; // no-op for array
} else {
error('invalid arguments');
}
// Dest is not existing dir, but multiple sources given
@ -247,6 +255,7 @@ exports.cp = wrap('cp', _cp);
//@
//@ #### rm([options ,] file [, file ...])
//@ #### rm([options ,] file_array)
//@ Available options:
//@
//@ + `-f`: force
@ -255,8 +264,9 @@ exports.cp = wrap('cp', _cp);
//@ Examples:
//@
//@ ```javascript
//@ rm('some_file.txt', 'another_file.txt');
//@ rm('-rf', '/tmp/*');
//@ rm('some_file.txt', 'another_file.txt');
//@ rm(['some_file.txt', 'another_file.txt']); // same as above
//@ ```
//@
//@ Removes files. The wildcard `*` is accepted.
@ -266,11 +276,13 @@ function _rm(options, files) {
'r': 'recursive',
'R': 'recursive'
});
var files = [].slice.call(arguments, 1);
if (files.length === 0)
if (!files)
error('no paths given');
if (typeof files === 'string')
files = [].slice.call(arguments, 1);
// if it's array leave it as it is
files = expand(files);
files.forEach(function(file) {
@ -306,10 +318,19 @@ exports.rm = wrap('rm', _rm);
//@
//@ #### mv(source [, source ...], dest')
//@ #### mv(source_array, dest')
//@ Available options:
//@
//@ + `f`: force
//@
//@ Examples:
//@
//@ ```javascript
//@ mv('-f', 'file', 'dir/');
//@ mv('file1', 'file2', 'dir/');
//@ mv(['file1', 'file2'], 'dir/'); // same as above
//@ ```
//@
//@ Moves files. The wildcard `*` is accepted.
function _mv(options, sources, dest) {
options = parseOptions(options, {
@ -322,8 +343,12 @@ function _mv(options, sources, dest) {
} else if (arguments.length > 3) {
sources = [].slice.call(arguments, 1, arguments.length - 1);
dest = arguments[arguments.length - 1];
} else {
} else if (typeof sources === 'string') {
sources = [sources];
} else if ('length' in sources) {
sources = sources; // no-op for array
} else {
error('invalid arguments');
}
sources = expand(sources);
@ -366,7 +391,8 @@ function _mv(options, sources, dest) {
exports.mv = wrap('mv', _mv);
//@
//@ #### mkdir([options ,] dir [, dir ...]')
//@ #### mkdir([options ,] dir [, dir ...])
//@ #### mkdir([options ,] dir_array)
//@ Available options:
//@
//@ + `p`: full path (will create intermediate dirs if necessary)
@ -374,7 +400,8 @@ exports.mv = wrap('mv', _mv);
//@ Examples:
//@
//@ ```javascript
//@ mkdir('-p', '/tmp/a/b/c/d');
//@ mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
//@ mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
//@ ```
//@
//@ Creates directories.
@ -382,10 +409,12 @@ function _mkdir(options, dirs) {
options = parseOptions(options, {
'p': 'fullpath'
});
var dirs = [].slice.call(arguments, 1);
if (!dirs)
error('no paths given');
if (dirs.length === 0)
error('no directories given');
if (typeof dirs === 'string')
dirs = [].slice.call(arguments, 1);
// if it's array leave it as it is
dirs.forEach(function(dir) {
if (fs.existsSync(dir)) {
@ -410,26 +439,32 @@ function _mkdir(options, dirs) {
exports.mkdir = wrap('mkdir', _mkdir);
//@
//@ #### cat(file [, file ...]')
//@ #### cat(file [, file ...])
//@ #### cat(file_array)
//@
//@ Examples:
//@
//@ ```javascript
//@ var str = cat('file*.txt');
//@ var str = cat('file1', 'file2');
//@ var str = cat(['file1', 'file2']); // same as above
//@ ```
//@
//@ 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.
function _cat(options, files) {
var files = [].slice.call(arguments, 1),
cat = '';
var cat = '';
if (!files)
error('no paths given');
if (typeof files === 'string')
files = [].slice.call(arguments, 1);
// if it's array leave it as it is
files = expand(files);
if (files.length === 0)
error('no files given');
files.forEach(function(file) {
if (!fs.existsSync(file))
error('no such file or directory: ' + file);
@ -513,7 +548,8 @@ function _sed(options, regex, replacement, file) {
exports.sed = wrap('sed', _sed);
//@
//@ #### grep(regex_filter, file [, file ...]')
//@ #### grep(regex_filter, file [, file ...])
//@ #### grep(regex_filter, file_array)
//@
//@ Examples:
//@
@ -523,11 +559,14 @@ exports.sed = wrap('sed', _sed);
//@
//@ Reads input string from given files and returns a string containing all lines of the
//@ file that match the given `regex_filter`. Wildcard `*` accepted.
function _grep(options, regex, file) {
if (!file)
error('no file given');
function _grep(options, regex, files) {
if (!files)
error('no paths given');
if (typeof files === 'string')
files = [].slice.call(arguments, 2);
// if it's array leave it as it is
var files = [].slice.call(arguments, 2);
files = expand(files);
var grep = '';
@ -700,13 +739,16 @@ exports.tempdir = wrap('tempdir', tempDir);
//@
//@ #### exists(path [, path ...])
//@ #### exists(path_array)
//@ Returns true if all the given paths exist.
function _exists(options) {
var paths = [].slice.call(arguments, 1);
if (paths.length === 0)
function _exists(options, paths) {
if (!paths)
error('no paths given');
if (typeof paths === 'string')
paths = [].slice.call(arguments, 1);
// if it's array leave it as it is
var exists = true;
paths.forEach(function(p) {
if (!fs.existsSync(p))

View File

@ -44,6 +44,11 @@ var result = shell.cat('resources/file2', 'resources/file1');
assert.equal(shell.error(), null);
assert.equal(result, 'test2\ntest1');
// multiple files, array syntax
var result = shell.cat(['resources/file2', 'resources/file1']);
assert.equal(shell.error(), null);
assert.equal(result, 'test2\ntest1');
var result = shell.cat('resources/file*.txt');
assert.equal(shell.error(), null);
assert.ok(result.search('test1') > -1); // file order might be random

View File

@ -63,14 +63,30 @@ assert.equal(fs.existsSync('tmp/a_file'), false);
// Valids
//
// simple - to dir
shell.cp('resources/file1', 'tmp');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/file1'), true);
// simple - to file
shell.cp('resources/file2', 'tmp/file2');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/file2'), true);
// simple - file list
shell.rm('-rf', 'tmp/*');
shell.cp('resources/file1', 'resources/file2', 'tmp');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/file1'), true);
assert.equal(fs.existsSync('tmp/file2'), true);
// simple - file list, array syntax
shell.rm('-rf', 'tmp/*');
shell.cp(['resources/file1', 'resources/file2'], 'tmp');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/file1'), true);
assert.equal(fs.existsSync('tmp/file2'), true);
shell.cp('resources/file2', 'tmp/file3');
assert.equal(fs.existsSync('tmp/file3'), true);
shell.cp('-f', 'resources/file2', 'tmp/file3'); // file exists, but -f specified
@ -104,13 +120,4 @@ shell.cp('-Rf', 'resources/cp', 'tmp');
assert.equal(shell.error(), null);
assert.equal(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // after cp
//recursive, everything exists, with force flag - comma-syntax
shell.rm('-rf', 'tmp/*')
shell.cp('-R', 'resources/cp' ,'tmp');
'changing things around'.to('tmp/cp/dir_a/z');
assert.notEqual(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // before cp
shell.cp('-Rf', 'resources/cp', 'tmp');
assert.equal(shell.error(), null);
assert.equal(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // after cp
shell.exit(123);

View File

@ -42,8 +42,14 @@ var result = shell.grep('line one', 'resources/a.txt');
assert.equal(shell.error(), null);
assert.equal(result, 'This is line one\n');
var result = shell.grep(/line one/, 'resources/a.txt');
// multiple files
var result = shell.grep(/test/, 'resources/file1.txt', 'resources/file2.txt');
assert.equal(shell.error(), null);
assert.equal(result, 'This is line one\n');
assert.equal(result, 'test1\ntest2\n');
// multiple files, array syntax
var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']);
assert.equal(shell.error(), null);
assert.equal(result, 'test1\ntest2\n');
shell.exit(123);

View File

@ -62,4 +62,18 @@ assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/a/b/c'), true);
shell.rm('-Rf', 'tmp/a'); // revert
// multiple dirs
shell.mkdir('-p', 'tmp/zzza', 'tmp/zzzb', 'tmp/zzzc');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/zzza'), true);
assert.equal(fs.existsSync('tmp/zzzb'), true);
assert.equal(fs.existsSync('tmp/zzzc'), true);
// multiple dirs, array syntax
shell.mkdir('-p', ['tmp/yyya', 'tmp/yyyb', 'tmp/yyyc']);
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('tmp/yyya'), true);
assert.equal(fs.existsSync('tmp/yyyb'), true);
assert.equal(fs.existsSync('tmp/yyyc'), true);
shell.exit(123);

View File

@ -86,8 +86,23 @@ shell.mv('file3', 'file1'); // revert
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('file1'), true);
// two sources
shell.rm('-rf', 't');
shell.mkdir('-p', 't');
shell.mv('file1', 'file2', 't'); // two sources
shell.mv('file1', 'file2', 't');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('file1'), false);
assert.equal(fs.existsSync('file2'), false);
assert.equal(fs.existsSync('t/file1'), true);
assert.equal(fs.existsSync('t/file2'), true);
shell.mv('t/*', '.'); // revert
assert.equal(fs.existsSync('file1'), true);
assert.equal(fs.existsSync('file2'), true);
// two sources, array style
shell.rm('-rf', 't');
shell.mkdir('-p', 't');
shell.mv(['file1', 'file2'], 't'); // two sources
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('file1'), false);
assert.equal(fs.existsSync('file2'), false);

View File

@ -90,7 +90,7 @@ assert.equal(shell.error(), null);
var contents = fs.readdirSync('tmp');
assert.equal(contents.length, 0);
// recursive dir removal - comma-syntax
// recursive dir removal - array-syntax
shell.mkdir('-p', 'tmp/a/b/c');
shell.mkdir('-p', 'tmp/b');
shell.mkdir('-p', 'tmp/c');
@ -99,7 +99,7 @@ assert.equal(fs.existsSync('tmp/a/b/c'), true);
assert.equal(fs.existsSync('tmp/b'), true);
assert.equal(fs.existsSync('tmp/c'), true);
assert.equal(fs.existsSync('tmp/.hidden'), true);
shell.rm('-rf', 'tmp/*', 'tmp/.*');
shell.rm('-rf', ['tmp/*', 'tmp/.*']);
assert.equal(shell.error(), null);
var contents = fs.readdirSync('tmp');
assert.equal(contents.length, 0);