Skip to content

Commit 65b658d

Browse files
committed
Merge pull request #116 from idearat/master
Add -l and -s support to grep.
2 parents e88679a + 5aaca88 commit 65b658d

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

src/grep.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,48 @@ var fs = require('fs');
1919
//@ file that match the given `regex_filter`. Wildcard `*` accepted.
2020
function _grep(options, regex, files) {
2121
options = common.parseOptions(options, {
22-
'v': 'inverse'
22+
'v': 'inverse',
23+
'l': 'files-with-matches',
24+
's': 'no-messages'
2325
});
2426

25-
if (!files)
27+
if (!files) {
2628
common.error('no paths given');
29+
}
2730

28-
if (typeof files === 'string')
31+
if (typeof files === 'string') {
2932
files = [].slice.call(arguments, 2);
33+
}
3034
// if it's array leave it as it is
3135

3236
files = common.expand(files);
3337

3438
var grep = '';
3539
files.forEach(function(file) {
3640
if (!fs.existsSync(file)) {
37-
common.error('no such file or directory: ' + file, true);
41+
if (!options['no-messages']) {
42+
common.error('no such file or directory: ' + file, true);
43+
}
44+
return;
45+
}
46+
47+
if (!fs.lstatSync(file).isFile()) {
3848
return;
3949
}
4050

4151
var contents = fs.readFileSync(file, 'utf8'),
4252
lines = contents.split(/\r*\n/);
4353
lines.forEach(function(line) {
4454
var matched = line.match(regex);
45-
if ((options.inverse && !matched) || (!options.inverse && matched))
46-
grep += line + '\n';
55+
if ((options.inverse && !matched) || (!options.inverse && matched)) {
56+
if (options['files-with-matches']) {
57+
if (grep.indexOf(file) === -1) {
58+
grep += file + '\n';
59+
}
60+
} else {
61+
grep += line + '\n';
62+
}
63+
}
4764
});
4865
});
4966

test/grep.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,48 @@ assert.equal(result, 'This is line one\n');
4040

4141
// multiple files
4242
var result = shell.grep(/test/, 'resources/file1.txt', 'resources/file2.txt');
43+
var values = result.trim().split('\n');
4344
assert.equal(shell.error(), null);
44-
assert.equal(result, 'test1\ntest2\n');
45+
assert.equal(values.length, 2);
46+
assert.equal(values.sort().join('\n'), 'test1\ntest2');
4547

4648
// multiple files, array syntax
4749
var result = shell.grep(/test/, ['resources/file1.txt', 'resources/file2.txt']);
50+
var values = result.trim().split('\n');
4851
assert.equal(shell.error(), null);
49-
assert.equal(result, 'test1\ntest2\n');
52+
assert.equal(values.length, 2);
53+
assert.equal(values.sort().join('\n'), 'test1\ntest2');
54+
55+
// list file names of matches
56+
var result = shell.grep('-l', /test/, ['resources/file1.txt', 'resources/file2.txt']);
57+
var values = result.trim().split('\n');
58+
assert.equal(shell.error(), null);
59+
assert.equal(values.length, 2);
60+
assert.equal(values.sort().join('\n'), 'resources/file1.txt\nresources/file2.txt');
61+
62+
// glob (and -s to silence missing files found via glob)
63+
shell.cd('./resources');
64+
var result = shell.grep('-s', /test/, '*');
65+
var values = result.trim().split('\n');
66+
assert.equal(shell.error(), null);
67+
assert.equal(values.length, 6);
68+
assert.equal(values.sort().join('\n'), 'test\ntest\ntest1\ntest1\ntest2\ntest2');
69+
shell.cd('..');
70+
71+
// glob (and -s to silence missing files found via glob)
72+
shell.cd('./resources');
73+
var result = shell.grep('-s', /test/, '*');
74+
var values = result.trim().split('\n');
75+
assert.equal(shell.error(), null);
76+
assert.equal(values.length, 6);
77+
assert.equal(values.sort().join('\n'), 'test\ntest\ntest1\ntest1\ntest2\ntest2');
78+
79+
// glob listing file names of matches
80+
shell.cd('./resources');
81+
var result = shell.grep('-ls', /test/, '*');
82+
var values = result.trim().split('\n');
83+
assert.equal(shell.error(), null);
84+
assert.equal(values.sort().join('\n'), "file1\nfile1.js\nfile1.txt\nfile2\nfile2.js\nfile2.txt");
5085

5186
// multiple files, glob syntax, * for file name
5287
var result = shell.grep(/test/, 'resources/file*.txt');

0 commit comments

Comments
 (0)