From e76cfa9fa15f45c373468b03346ef7a7eab7f4e5 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Fri, 2 Mar 2012 15:51:44 -0500 Subject: [PATCH] add support to array arguments --- README.md | 36 +++++++++++++++---- shell.js | 98 ++++++++++++++++++++++++++++++++++++--------------- test/cat.js | 5 +++ test/cp.js | 25 ++++++++----- test/grep.js | 10 ++++-- test/mkdir.js | 14 ++++++++ test/mv.js | 17 ++++++++- test/rm.js | 4 +-- 8 files changed, 160 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 4fcb469..d6fd949 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/shell.js b/shell.js index 5f98001..bf6212b 100644 --- a/shell.js +++ b/shell.js @@ -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)) diff --git a/test/cat.js b/test/cat.js index 8842de6..6a8b793 100644 --- a/test/cat.js +++ b/test/cat.js @@ -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 diff --git a/test/cp.js b/test/cp.js index 6628dee..f9288c8 100644 --- a/test/cp.js +++ b/test/cp.js @@ -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); diff --git a/test/grep.js b/test/grep.js index 6d25da2..4fdd8dc 100644 --- a/test/grep.js +++ b/test/grep.js @@ -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); diff --git a/test/mkdir.js b/test/mkdir.js index 338cff1..ba0cd91 100644 --- a/test/mkdir.js +++ b/test/mkdir.js @@ -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); diff --git a/test/mv.js b/test/mv.js index cd8db00..dd2e003 100644 --- a/test/mv.js +++ b/test/mv.js @@ -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); diff --git a/test/rm.js b/test/rm.js index e6fab34..a429f5f 100644 --- a/test/rm.js +++ b/test/rm.js @@ -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);