Grep directory support by c-pranshu · Pull Request #1044 · shelljs/shelljs · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"dependencies": {
"execa": "^1.0.0",
"fs-readdir-recursive": "^1.1.0",
"glob": "^7.0.0",
"interpret": "^1.0.0",
"rechoir": "^0.6.2"
Expand Down
44 changes: 32 additions & 12 deletions src/grep.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var common = require('./common');
var fs = require('fs');
var path = require('path');
var read = require('fs-readdir-recursive');


common.register('grep', _grep, {
globStart: 2, // don't glob-expand the regex
Expand All @@ -8,19 +11,20 @@ common.register('grep', _grep, {
'v': 'inverse',
'l': 'nameOnly',
'i': 'ignoreCase',
'r': 'recursive',
},
});

//@
//@ ### grep([options,] regex_filter, file [, file ...])
//@ ### grep([options,] regex_filter, file_array)
//@ ### grep([options,] regex_filter, file [ ...file, ...directory])
//@ ### grep([options,] regex_filter, file_array,directory_array)
//@
//@ Available options:
//@
//@ + `-v`: Invert `regex_filter` (only print non-matching lines).
//@ + `-l`: Print only filenames of matching files.
//@ + `-i`: Ignore case.
//@
//@ + `-r`: recursive search
//@ Examples:
//@
//@ ```javascript
Expand All @@ -31,29 +35,45 @@ common.register('grep', _grep, {
//@ Reads input string from given files and returns a
//@ [ShellString](#shellstringstr) containing all lines of the @ file that match
//@ the given `regex_filter`.
function _grep(options, regex, files) {
// Check if this is coming from a pipe
var pipe = common.readFromPipe();

if (!files && !pipe) common.error('no paths given', 2);
function getFilesInDir(directory) {
var files = read(directory);
for (var i = 0; i < files.length; i++) {
files[i] = path.join(directory, path.dirname(files[i]), path.basename(files[i]));
}
return files;
}

function _grep(options, regex, files) {
var pipe = common.readFromPipe();
if (!files && !pipe) common.error('No paths given', 2);
files = [].slice.call(arguments, 2);

if (pipe) {
files.unshift('-');
}

var grep = [];
var contents = '';
if (options.ignoreCase) {
regex = new RegExp(regex, 'i');
}
files.forEach(function (file) {

for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!fs.existsSync(file) && file !== '-') {
common.error('no such file or directory: ' + file, 2, { continue: true });
common.error('no such file or directory: ' + file, 2, { silent: true });
} else if (!fs.existsSync(file) && file === '-') {
contents = pipe;
} else if (options.recursive && fs.statSync(file).isDirectory()) {
files = files.concat(getFilesInDir(file));
} else if (!options.recursive && fs.statSync(file).isDirectory()) {
common.error(file + ' Is a directory', 2, { silent: true });
return;
} else if (fs.statSync(file).isFile()) {
contents = fs.readFileSync(file, 'utf8');
}

var contents = file === '-' ? pipe : fs.readFileSync(file, 'utf8');
if (options.nameOnly) {
if (contents.match(regex)) {
grep.push(file);
Expand All @@ -67,12 +87,12 @@ function _grep(options, regex, files) {
}
});
}
});

}
if (grep.length === 0 && common.state.errorCode !== 2) {
// We didn't hit the error above, but pattern didn't match
common.error('', { silent: true });
}
return grep.join('\n') + '\n';
}

module.exports = _grep;
42 changes: 42 additions & 0 deletions test/grep.js