This commit is contained in:
Artur Adib 2012-03-02 09:58:21 -05:00
parent 1ed8eabd1c
commit 2d0ff6fa52
2 changed files with 27 additions and 22 deletions

View File

@ -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 = '';

View File

@ -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);