mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
Revert "Merge pull request #116 from idearat/master"
This reverts commit 65b658d1e47f380e4a50a859a871c8c48d1bd775, reversing changes made to e88679a4a9146017cc9821efa4645acc7ef428c4.
This commit is contained in:
parent
65b658d1e4
commit
20db182fcb
29
src/grep.js
29
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';
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
39
test/grep.js
39
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');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user