mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
simple test()
This commit is contained in:
parent
79133e53a7
commit
c2ab790cde
15
README.md
15
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)
|
||||
|
||||
|
||||
37
shell.js
37
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)
|
||||
|
||||
51
test/test.js
Normal file
51
test/test.js
Normal file
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user