mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
feat(sed): support multiple file names
fixes #231. Semantics are like unix sed.
This commit is contained in:
parent
b170d20122
commit
bccf620787
@ -331,7 +331,8 @@ Analogous to the redirect-and-append operator `>>` in Unix, but works with JavaS
|
||||
those returned by `cat`, `grep`, etc).
|
||||
|
||||
|
||||
### sed([options,] search_regex, replacement, file)
|
||||
### sed([options,] search_regex, replacement, file [, file ...])
|
||||
### sed([options,] search_regex, replacement, file_array)
|
||||
Available options:
|
||||
|
||||
+ `-i`: Replace contents of 'file' in-place. _Note that no backups will be created!_
|
||||
@ -343,7 +344,7 @@ sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
|
||||
sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');
|
||||
```
|
||||
|
||||
Reads an input string from `file` and performs a JavaScript `replace()` on the input
|
||||
Reads an input string from `files` and performs a JavaScript `replace()` on the input
|
||||
using the given search regex and replacement string or function. Returns the new string after replacement.
|
||||
|
||||
|
||||
|
||||
40
src/sed.js
40
src/sed.js
@ -2,7 +2,8 @@ var common = require('./common');
|
||||
var fs = require('fs');
|
||||
|
||||
//@
|
||||
//@ ### sed([options,] search_regex, replacement, file)
|
||||
//@ ### sed([options,] search_regex, replacement, file [, file ...])
|
||||
//@ ### sed([options,] search_regex, replacement, file_array)
|
||||
//@ Available options:
|
||||
//@
|
||||
//@ + `-i`: Replace contents of 'file' in-place. _Note that no backups will be created!_
|
||||
@ -14,9 +15,9 @@ var fs = require('fs');
|
||||
//@ sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js');
|
||||
//@ ```
|
||||
//@
|
||||
//@ Reads an input string from `file` and performs a JavaScript `replace()` on the input
|
||||
//@ Reads an input string from `files` and performs a JavaScript `replace()` on the input
|
||||
//@ using the given search regex and replacement string or function. Returns the new string after replacement.
|
||||
function _sed(options, regex, replacement, file) {
|
||||
function _sed(options, regex, replacement, files) {
|
||||
options = common.parseOptions(options, {
|
||||
'i': 'inplace'
|
||||
});
|
||||
@ -32,19 +33,32 @@ function _sed(options, regex, replacement, file) {
|
||||
if (typeof regex === 'string')
|
||||
regex = RegExp(regex);
|
||||
|
||||
if (!file)
|
||||
common.error('no file given');
|
||||
if (!files)
|
||||
common.error('no files given');
|
||||
|
||||
if (!fs.existsSync(file))
|
||||
common.error('no such file or directory: ' + file);
|
||||
if (typeof files === 'string')
|
||||
files = [].slice.call(arguments, 3);
|
||||
// if it's array leave it as it is
|
||||
|
||||
var result = fs.readFileSync(file, 'utf8').split('\n').map(function (line) {
|
||||
return line.replace(regex, replacement);
|
||||
}).join('\n');
|
||||
files = common.expand(files);
|
||||
|
||||
if (options.inplace)
|
||||
fs.writeFileSync(file, result, 'utf8');
|
||||
var sed = [];
|
||||
files.forEach(function(file) {
|
||||
if (!fs.existsSync(file)) {
|
||||
common.error('no such file or directory: ' + file, true);
|
||||
return;
|
||||
}
|
||||
|
||||
return common.ShellString(result);
|
||||
var result = fs.readFileSync(file, 'utf8').split('\n').map(function (line) {
|
||||
return line.replace(regex, replacement);
|
||||
}).join('\n');
|
||||
|
||||
sed.push(result);
|
||||
|
||||
if (options.inplace)
|
||||
fs.writeFileSync(file, result, 'utf8');
|
||||
});
|
||||
|
||||
return common.ShellString(sed.join('\n'));
|
||||
}
|
||||
module.exports = _sed;
|
||||
|
||||
31
test/sed.js
31
test/sed.js
@ -21,8 +21,15 @@ assert.ok(shell.error());
|
||||
shell.sed(/asdf/g, 'nada'); // too few args
|
||||
assert.ok(shell.error());
|
||||
|
||||
assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check
|
||||
shell.sed(/asdf/g, 'nada', '/asdfasdf'); // no such file
|
||||
assert.equal(fs.existsSync('asdfasdf'), false); // sanity check
|
||||
shell.sed(/asdf/g, 'nada', 'asdfasdf'); // no such file
|
||||
assert.ok(shell.error());
|
||||
|
||||
// if at least one file is missing, this should be an error
|
||||
shell.cp('-f', 'resources/file1', 'tmp/file1');
|
||||
assert.equal(fs.existsSync('asdfasdf'), false); // sanity check
|
||||
assert.equal(fs.existsSync('tmp/file1'), true); // sanity check
|
||||
shell.sed(/asdf/g, 'nada', 'tmp/file1', 'asdfasdf');
|
||||
assert.ok(shell.error());
|
||||
|
||||
//
|
||||
@ -74,4 +81,24 @@ var result = shell.sed('l*\\.js', '', 'resources/grep/file');
|
||||
assert.ok(!shell.error());
|
||||
assert.equal(result, 'alphaaaaaaabeta\nhowareyou\nalphbeta\nthis line ends in\n\n');
|
||||
|
||||
shell.cp('-f', 'resources/file1', 'tmp/file1');
|
||||
shell.cp('-f', 'resources/file2', 'tmp/file2');
|
||||
|
||||
// multiple file names
|
||||
var result = shell.sed('test', 'hello', 'tmp/file1', 'tmp/file2');
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'hello1\nhello2');
|
||||
|
||||
// array of file names (and try it out with a simple regex)
|
||||
var result = shell.sed(/t.*st/, 'hello', ['tmp/file1', 'tmp/file2']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'hello1\nhello2');
|
||||
|
||||
// multiple file names, with in-place-replacement
|
||||
var result = shell.sed('-i', 'test', 'hello', ['tmp/file1', 'tmp/file2']);
|
||||
assert.equal(shell.error(), null);
|
||||
assert.equal(result, 'hello1\nhello2');
|
||||
assert.equal(shell.cat('tmp/file1'), 'hello1');
|
||||
assert.equal(shell.cat('tmp/file2'), 'hello2');
|
||||
|
||||
shell.exit(123);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user