mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
Merge pull request #119 from utensil/118-grep-glob
grep() support for globing, fixes #118
This commit is contained in:
commit
8ba50dcf6b
@ -92,8 +92,22 @@ exports.parseOptions = parseOptions;
|
||||
function expand(list) {
|
||||
var expanded = [];
|
||||
list.forEach(function(listEl) {
|
||||
// Wildcard present?
|
||||
if (listEl.search(/\*/) > -1) {
|
||||
// Wildcard present on directory names ?
|
||||
if(listEl.search(/\*[^\/]*\//) > -1 || listEl.search(/\*\*[^\/]*\//) > -1) {
|
||||
var match = listEl.match(/^([^*]+\/|)(.*)/);
|
||||
var root = match[1];
|
||||
var rest = match[2];
|
||||
var restRegex = rest.replace(/\*\*/g, ".*").replace(/\*/g, "[^\\/]*");
|
||||
restRegex = new RegExp(restRegex);
|
||||
|
||||
_ls('-R', root).filter(function (e) {
|
||||
return restRegex.test(e);
|
||||
}).forEach(function(file) {
|
||||
expanded.push(file);
|
||||
});
|
||||
}
|
||||
// Wildcard present on file names ?
|
||||
else if (listEl.search(/\*/) > -1) {
|
||||
_ls('', listEl).forEach(function(file) {
|
||||
expanded.push(file);
|
||||
});
|
||||
|
||||
51
test/common.js
Normal file
51
test/common.js
Normal file
@ -0,0 +1,51 @@
|
||||
var shell = require('..');
|
||||
var common = require('../src/common');
|
||||
|
||||
var assert = require('assert');
|
||||
|
||||
shell.config.silent = true;
|
||||
|
||||
shell.rm('-rf', 'tmp');
|
||||
shell.mkdir('tmp');
|
||||
|
||||
//
|
||||
// Invalids
|
||||
//
|
||||
|
||||
// too few args
|
||||
assert.throws(function () {
|
||||
common.expand();
|
||||
}, TypeError);
|
||||
|
||||
// should be a list
|
||||
assert.throws(function () {
|
||||
common.expand("resources");
|
||||
}, TypeError);
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
// single file, array syntax
|
||||
var result = common.expand(['resources/file1.txt']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.deepEqual(result, ['resources/file1.txt']);
|
||||
|
||||
// multiple file, glob syntax, * for file name
|
||||
var result = common.expand(['resources/file*.txt']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort());
|
||||
|
||||
// multiple file, glob syntax, * for directory name
|
||||
var result = common.expand(['*/file*.txt']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort());
|
||||
|
||||
// multiple file, glob syntax, ** for directory name
|
||||
var result = common.expand(['**/file*.js']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.deepEqual(result.sort(), ["resources/file1.js","resources/file2.js","resources/ls/file1.js","resources/ls/file2.js"].sort());
|
||||
|
||||
shell.exit(123);
|
||||
|
||||
|
||||
15
test/grep.js
15
test/grep.js
@ -48,4 +48,19 @@ var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test1\ntest2\n');
|
||||
|
||||
// multiple files, glob syntax, * for file name
|
||||
var result = shell.grep(/test/, 'resources/file*.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n');
|
||||
|
||||
// multiple files, glob syntax, * for directory name
|
||||
var result = shell.grep(/test/, '*/file*.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.ok(result == 'test1\ntest2\n' || result == 'test2\ntest1\n');
|
||||
|
||||
// multiple files, glob syntax, ** for directory name
|
||||
var result = shell.grep(/test/, '**/file*.js');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'test\ntest\ntest\ntest\n');
|
||||
|
||||
shell.exit(123);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user