diff --git a/src/grep.js b/src/grep.js index 33b8ad5..00c7d6a 100644 --- a/src/grep.js +++ b/src/grep.js @@ -19,18 +19,14 @@ var fs = require('fs'); //@ file that match the given `regex_filter`. Wildcard `*` accepted. function _grep(options, regex, files) { options = common.parseOptions(options, { - 'v': 'inverse', - 'l': 'files-with-matches', - 's': 'no-messages' + 'v': 'inverse' }); - if (!files) { + if (!files) common.error('no paths given'); - } - if (typeof files === 'string') { + if (typeof files === 'string') files = [].slice.call(arguments, 2); - } // if it's array leave it as it is files = common.expand(files); @@ -38,13 +34,7 @@ function _grep(options, regex, files) { var grep = ''; files.forEach(function(file) { if (!fs.existsSync(file)) { - if (!options['no-messages']) { - common.error('no such file or directory: ' + file, true); - } - return; - } - - if (!fs.lstatSync(file).isFile()) { + common.error('no such file or directory: ' + file, true); return; } @@ -52,15 +42,8 @@ function _grep(options, regex, files) { lines = contents.split(/\r*\n/); lines.forEach(function(line) { var matched = line.match(regex); - if ((options.inverse && !matched) || (!options.inverse && matched)) { - if (options['files-with-matches']) { - if (grep.indexOf(file) === -1) { - grep += file + '\n'; - } - } else { - grep += line + '\n'; - } - } + if ((options.inverse && !matched) || (!options.inverse && matched)) + grep += line + '\n'; }); }); diff --git a/test/grep.js b/test/grep.js index ee7a636..df57483 100644 --- a/test/grep.js +++ b/test/grep.js @@ -40,48 +40,13 @@ assert.equal(result, 'This is line one\n'); // multiple files var result = shell.grep(/test/, 'resources/file1.txt', 'resources/file2.txt'); -var values = result.trim().split('\n'); assert.equal(shell.error(), null); -assert.equal(values.length, 2); -assert.equal(values.sort().join('\n'), 'test1\ntest2'); +assert.equal(result, 'test1\ntest2\n'); // multiple files, array syntax var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']); -var values = result.trim().split('\n'); assert.equal(shell.error(), null); -assert.equal(values.length, 2); -assert.equal(values.sort().join('\n'), 'test1\ntest2'); - -// list file names of matches -var result = shell.grep('-l', /test/, ['resources/file1.txt', 'resources/file2.txt']); -var values = result.trim().split('\n'); -assert.equal(shell.error(), null); -assert.equal(values.length, 2); -assert.equal(values.sort().join('\n'), 'resources/file1.txt\nresources/file2.txt'); - -// glob (and -s to silence missing files found via glob) -shell.cd('./resources'); -var result = shell.grep('-s', /test/, '*'); -var values = result.trim().split('\n'); -assert.equal(shell.error(), null); -assert.equal(values.length, 6); -assert.equal(values.sort().join('\n'), 'test\ntest\ntest1\ntest1\ntest2\ntest2'); -shell.cd('..'); - -// glob (and -s to silence missing files found via glob) -shell.cd('./resources'); -var result = shell.grep('-s', /test/, '*'); -var values = result.trim().split('\n'); -assert.equal(shell.error(), null); -assert.equal(values.length, 6); -assert.equal(values.sort().join('\n'), 'test\ntest\ntest1\ntest1\ntest2\ntest2'); - -// glob listing file names of matches -shell.cd('./resources'); -var result = shell.grep('-ls', /test/, '*'); -var values = result.trim().split('\n'); -assert.equal(shell.error(), null); -assert.equal(values.sort().join('\n'), "file1\nfile1.js\nfile1.txt\nfile2\nfile2.js\nfile2.txt"); +assert.equal(result, 'test1\ntest2\n'); // multiple files, glob syntax, * for file name var result = shell.grep(/test/, 'resources/file*.txt');