mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
grep()
This commit is contained in:
parent
1ed8eabd1c
commit
2d0ff6fa52
19
shell.js
19
shell.js
@ -482,7 +482,7 @@ function ShellString(str) {
|
||||
//@ + `sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js')`
|
||||
//@
|
||||
//@ Reads an input string from `file` and performs a JavaScript `replace()` on the input
|
||||
//@ using the given search regex and replacement string. Returns the modified string.
|
||||
//@ using the given search regex and replacement string. Returns the replaced `String()` object.
|
||||
function _sed(options, regex, replacement, file) {
|
||||
options = parseOptions(options, {
|
||||
'i': 'inplace'
|
||||
@ -510,14 +510,19 @@ function _sed(options, regex, replacement, file) {
|
||||
exports.sed = wrap('sed', _sed);
|
||||
|
||||
//@
|
||||
//@ #### grep(regex_filter, 'file [file ...]')
|
||||
//@ Reads input string from given files and returns a string containing all lines of the
|
||||
//@ file that match the given `regex_filter`. Wildcards are accepted for file names.
|
||||
function _grep(options, regex, filesStr) {
|
||||
if (!filesStr)
|
||||
//@ #### grep([options ,] regex_filter, file [, file ...]')
|
||||
//@
|
||||
//@ Examples:
|
||||
//@
|
||||
//@ + `grep('GLOBAL_VARIABLE', '*.js')`
|
||||
//@
|
||||
//@ Reads input string from given files and returns a `String()` containing all lines of the
|
||||
//@ file that match the given `regex_filter`. Wildcard `*` accepted.
|
||||
function _grep(options, regex, file) {
|
||||
if (!file)
|
||||
error('no file given');
|
||||
|
||||
var files = parsePaths(filesStr);
|
||||
var files = [].slice.call(arguments, 2);
|
||||
files = expand(files);
|
||||
|
||||
var grep = '';
|
||||
|
||||
30
test/grep.js
30
test/grep.js
@ -1,4 +1,4 @@
|
||||
require('../maker');
|
||||
var shell = require('..');
|
||||
|
||||
var assert = require('assert'),
|
||||
path = require('path'),
|
||||
@ -7,7 +7,7 @@ var assert = require('assert'),
|
||||
// Node shims for < v0.7
|
||||
fs.existsSync = fs.existsSync || path.existsSync;
|
||||
|
||||
silent();
|
||||
shell.silent();
|
||||
|
||||
function numLines(str) {
|
||||
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
||||
@ -20,30 +20,30 @@ shell.mkdir('tmp')
|
||||
// Invalids
|
||||
//
|
||||
|
||||
grep();
|
||||
assert.ok(error());
|
||||
shell.grep();
|
||||
assert.ok(shell.error());
|
||||
|
||||
grep(/asdf/g); // too few args
|
||||
assert.ok(error());
|
||||
shell.grep(/asdf/g); // too few args
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
grep(/asdf/g, '/asdfasdf'); // no such file
|
||||
assert.ok(error());
|
||||
shell.grep(/asdf/g, '/asdfasdf'); // no such file
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
// Valids
|
||||
//
|
||||
|
||||
var result = grep('line', 'resources/a.txt');
|
||||
assert.equal(error(), null);
|
||||
var result = shell.grep('line', 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result.split('\n').length - 1, 4);
|
||||
|
||||
var result = grep('line one', 'resources/a.txt');
|
||||
assert.equal(error(), null);
|
||||
var result = shell.grep('line one', 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'This is line one\n');
|
||||
|
||||
var result = grep(/line one/, 'resources/a.txt');
|
||||
assert.equal(error(), null);
|
||||
var result = shell.grep(/line one/, 'resources/a.txt');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'This is line one\n');
|
||||
|
||||
exit(123);
|
||||
shell.exit(123);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user