diff --git a/src/common.js b/src/common.js index fe20871..d8c2312 100644 --- a/src/common.js +++ b/src/common.js @@ -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); }); diff --git a/test/common.js b/test/common.js new file mode 100644 index 0000000..74f824b --- /dev/null +++ b/test/common.js @@ -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); + + diff --git a/test/grep.js b/test/grep.js index c3b8642..df57483 100644 --- a/test/grep.js +++ b/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);