Skip to content

Commit

Permalink
Fix pat. or regex starting with '-'
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Mar 22, 2017
1 parent 33c3f69 commit 5632c5a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Please note that the data might look very differently when the files get so larg

### Future ideas


- Test-run a with info outputted about what will happen (sets -t and does not change anything)
- Let search and replace be withing the names of the files (ask for overwriting. -Y = no questions)
- Let search and replace be within the path of the files (ask for overwriting. -Y = no questions)
Expand All @@ -188,7 +189,9 @@ Please note that the data might look very differently when the files get so larg
- Flag for simple string search (all other chars than [\n\r\t])
- Flag for plain string search litteral (no regex, no special chars, no escape chars)
- Check if https://github.com/eugeneware/replacestream is good to rely on

- Check if regex engine from spidermonkey can be wrapped in someting thatdoes not need node
- Implement in go, so all platforms can be supported with no need for node (might be based on)
- let https://github.com/dthree/vorpal deal with the interface?



Expand Down
38 changes: 26 additions & 12 deletions bin/rexreplace.cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#!/usr/bin/env node
'use strict';

var version = '2.0.4';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var version = '2.1.0';
var fs = require('fs');
var path = require('path');
var font = require('chalk');
var globs = require('globs');

var pattern = void 0,
replacement = void 0;
var helpAndDie = false;
if (process.argv.length < 4) {
helpAndDie = true;
} else {
var _process$argv$splice = process.argv.splice(2, 2);

var _process$argv$splice2 = _slicedToArray(_process$argv$splice, 2);

pattern = _process$argv$splice2[0];
replacement = _process$argv$splice2[1];
}

var yargs = require('yargs').strict().usage('RexReplace ' + version + ': Regexp search and replace for files using lookahead and backreference to matching groups in the replacement. Defaults to global multiline case-insensitive search.\n\n> rexreplace searchFor replaceWith filename').example("> rexreplace '(f?(o))o(.*)' '$3$1$2' myfile.md", "'foobar' in myfile.md will become 'barfoo'").example('').example("> rexreplace -I 'Foo' 'xxx' myfile.md", "'foobar' in myfile.md will remain 'foobar'").example('').example('> rexreplace \'^#\' \'##\' *.md', 'All markdown files in this dir got all headlines moved one level deeper').version('v', 'Echo rexreplace version', version).alias('v', 'version').boolean('I').describe('I', 'Void case insensitive search pattern.').alias('I', 'void-ignore-case').boolean('M').describe('M', 'Void multiline search pattern. Makes ^ and $ match start/end of whole content rather than each line.').alias('M', 'void-multiline').boolean('u').describe('u', 'Treat pattern as a sequence of unicode code points.').alias('u', 'unicode').describe('e', 'Encoding of files.').alias('e', 'encoding').default('e', 'utf8').boolean('o').describe('o', 'Output the result instead of saving to file. Will also output content even if no replacement have taken place.').alias('o', 'output')
//.conflicts('o', 'd')

Expand Down Expand Up @@ -60,13 +76,13 @@ var args = yargs.argv;

debug(args);

if (args._.length < 3) {
die('Need more than 2 arguments', args._.length + ' was found', true);
if (helpAndDie) {
die('Need both pattern and replacement as arguments', true);
}

if (!args['€']) {
args._[0] = args._[0].replace('€', '$');
args._[1] = args._[1].replace('€', '$');
pattern = pattern.replace('€', '$');
replacement = replacement.replace('€', '$');
}

var flags = 'g';
Expand All @@ -83,16 +99,14 @@ if (args.unicode) {
debug(flags);

// Get regex pattern
var regex = args._.shift();

var regex = void 0;
try {
regex = new RegExp(regex, flags);
regex = new RegExp(pattern, flags);
} catch (err) {
die('Wrong formatted regexp', regex);
die('Wrong formatted regexp', err);
}

// Get replacement
var replace = args._.shift();

// The rest are files
var files = globs.sync(args._);

Expand All @@ -109,7 +123,7 @@ files

// Do the replacement
.forEach(function (filepath) {
return replaceInFile(filepath, regex, replace, args.encoding);
return replaceInFile(filepath, regex, replacement, args.encoding);
});

function replaceInFile(file, regex, replace, encoding) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rexreplace",
"version": "2.0.4",
"version": "2.1.0",
"description": "Friendly CLI regexp search and replace in files.",
"author": "Mathias Rangel Wulff",
"license": "MIT",
Expand Down
28 changes: 18 additions & 10 deletions src/rexreplace.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const path = require('path');
const font = require('chalk');
const globs = require('globs');

let pattern, replacement;
let helpAndDie = false;
if(process.argv.length<4){
helpAndDie = true;
} else {
[pattern, replacement] = process.argv.splice(2,2);
}

let yargs = require('yargs')
.strict()

Expand Down Expand Up @@ -129,13 +137,13 @@ const args = yargs.argv;

debug(args);

if(args._.length<3){
die('Need more than 2 arguments',args._.length+' was found',true);
if(helpAndDie){
die('Need both pattern and replacement as arguments',true);
}

if(!args['€']){
args._[0] = args._[0].replace('€','$');
args._[1] = args._[1].replace('€','$');
pattern = pattern.replace('€','$');
replacement = replacement.replace('€','$');
}

let flags = 'g';
Expand All @@ -152,15 +160,15 @@ if(args.unicode){
debug(flags);

// Get regex pattern
let regex = args._.shift();

let regex;
try{
regex = new RegExp(regex,flags);
regex = new RegExp(pattern,flags);
} catch (err){
die('Wrong formatted regexp', regex);
die('Wrong formatted regexp', err);
}

// Get replacement
const replace = args._.shift();


// The rest are files
const files = globs.sync(args._);
Expand All @@ -173,7 +181,7 @@ files
.filter(filepath=>fs.existsSync(filepath)?true:error('File not found:',filepath))

// Do the replacement
.forEach(filepath=>replaceInFile(filepath,regex,replace,args.encoding))
.forEach(filepath=>replaceInFile(filepath,regex,replacement,args.encoding))
;


Expand Down
5 changes: 5 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ reset
rexreplace o x myfile
assert "cat myfile" "fxxbar"

# rr can handle a pattern and replcaement starting with '-'
reset
rexreplace '^(.+)$' '- $1' myfile
rexreplace '- f' '_' myfile
assert "cat myfile" "_oobar"

# -v
reset
Expand Down

0 comments on commit 5632c5a

Please sign in to comment.