diff --git a/README.md b/README.md index 68692a7..82f5353 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,21 @@ mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above Creates directories. +#### test(expression) +Available expression primaries: + ++ `'-d', 'path'`: true if path is a directory ++ `'-f', 'path'`: true if path is a regular file + +Examples: + +```javascript +if (test('-d', path)) { /* do something with dir */ }; +if (!test('-f', path)) continue; // skip if it's a regular file +``` + +Evaluates expression using the available primaries and returns corresponding value. + #### cat(file [, file ...]) #### cat(file_array) diff --git a/shell.js b/shell.js index 6477c54..5f21a9f 100644 --- a/shell.js +++ b/shell.js @@ -57,6 +57,7 @@ function _pwd(options) { }; exports.pwd = wrap('pwd', _pwd); + //@ //@ #### ls([options ,] path [,path ...]) //@ #### ls([options ,] path_array) @@ -486,6 +487,42 @@ function _mkdir(options, dirs) { }; // mkdir exports.mkdir = wrap('mkdir', _mkdir); +//@ +//@ #### test(expression) +//@ Available expression primaries: +//@ +//@ + `'-d', 'path'`: true if path is a directory +//@ + `'-f', 'path'`: true if path is a regular file +//@ +//@ Examples: +//@ +//@ ```javascript +//@ if (test('-d', path)) { /* do something with dir */ }; +//@ if (!test('-f', path)) continue; // skip if it's a regular file +//@ ``` +//@ +//@ Evaluates expression using the available primaries and returns corresponding value. +function _test(options, path) { + if (!path) + error('no path given'); + + // hack - only works with unary primaries + options = parseOptions(options, { + 'd': 'directory', + 'f': 'file' + }); + if (!options.directory && !options.file) + error('could not interpret expression'); + + if (options.directory) + return fs.existsSync(path) && fs.statSync(path).isDirectory(); + + if (options.file) + return fs.existsSync(path) && fs.statSync(path).isFile(); +}; // test +exports.test = wrap('test', _test); + + //@ //@ #### cat(file [, file ...]) //@ #### cat(file_array) diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..43dfc04 --- /dev/null +++ b/test/test.js @@ -0,0 +1,51 @@ +var shell = require('..'); + +var assert = require('assert'), + path = require('path'), + fs = require('fs'); + +// Node shims for < v0.7 +fs.existsSync = fs.existsSync || path.existsSync; + +shell.silent(); + +shell.rm('-rf', 'tmp'); +shell.mkdir('tmp') + +// +// Invalids +// + +var result = shell.test(); // no expression given +assert.ok(shell.error()); + +var result = shell.test('asdf'); // bad expression +assert.ok(shell.error()); + +var result = shell.test('f', 'resources/file1'); // bad expression +assert.ok(shell.error()); + +var result = shell.test('-f'); // no file +assert.ok(shell.error()); + +// +// Valids +// + +var result = shell.test('-f', 'resources/file1'); +assert.equal(shell.error(), null); +assert.equal(result, true); + +var result = shell.test('-d', 'resources/file1'); +assert.equal(shell.error(), null); +assert.equal(result, false); + +var result = shell.test('-f', 'resources'); +assert.equal(shell.error(), null); +assert.equal(result, false); + +var result = shell.test('-d', 'resources'); +assert.equal(shell.error(), null); +assert.equal(result, true); + +shell.exit(123);