Skip to content

Commit

Permalink
Support globs for filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Mar 17, 2017
1 parent fac13ce commit bf692cf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
[![Build Status](https://travis-ci.org/mathiasrw/rexreplace.svg?branch=master)](https://travis-ci.org/mathiasrw/rexreplace)
[![npm version](https://badge.fury.io/js/rexreplace.svg)](https://www.npmjs.com/package/rexreplace)

# RexReplace

CLI Regexp search and replace for files using lookahead and
backreference to matching groups in the replacement. Defaults to global
multiline case-insensitive search. Needs Node v6 or higher.
backreference to matching groups in the replacement. Defaults to global multiline case-insensitive search. Will run on node v6+.

Files can be given in _glob_ notation, so `docs/*.md` will give same result as explicitly naming each markdown file in the `docs/` dir.

### Install
```bash
> npm install -g rexreplace
```



### Usages
```bash
> rexreplace searchFor replaceWith filenameA filenameB filenameC ...
> rexreplace searchFor replaceWith filenameOrGlob_A filenameOrGlob_B filenameOrGlob_C ...
```

### Examples
Expand All @@ -26,11 +24,14 @@ multiline case-insensitive search. Needs Node v6 or higher.

> rexreplace Foo xxx myfile.md -I
# 'foobar' in myfile.md will remain 'foobar'

> rexreplace '^#' '##' *.md
# All markdown files in this dir got all headlines moved one level deeper

> rexreplace '(f?(o))o(.*)' '$3$1$2' myfile.md
# 'foobar' in myfile.md will become 'barfoo'

# Some commandline tools (bash/zsh/...) can be a bit funny with the `$` sign. Use the '-€' flag to let `€` alias a `$` in pattern and replacement
# Some commandline tools (bash/zsh/...) is a bit funny with the `$` sign. use the '-€' flag to have `€` alias a `$`
> rexreplace '(f?(o))o(.*)' '€3€1€2' myfile.md -€
# 'foobar' in myfile.md will become 'barfoo'

Expand Down Expand Up @@ -63,12 +64,25 @@ multiline case-insensitive search. Needs Node v6 or higher.
-h, --help Show help [boolean]
```

### Future
- Handle globs
- Handle piped data
### Test
All tests are defined in [test.sh](https://github.com/mathiasrw/rexreplace/) and are invoked with

```bash
> npm test
```

### Future ideas
- Pipe while no globs = this is content to be searched (will always output) (when no -rp flags)
- Pipe while having 1 globs = output is to be stored in this file (when no -rpo flags)
- Pipe while having 2+ globs = error (when no -rpg flags)
- Let pattern, replacement, globs be piped
- Let Pattern, replacement, globs come from file
- Let pattern, replacement, glob be javascript code returning string as result
- Error != warning
- Debug != all debug


### inspiration

.oO(_What "sed" should have been by now_)
.oO(_What should "sed" have looked like by now?_)

The replace-in-file npm package (but with better naming and backreferences)
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rexreplace",
"version": "1.0.0",
"version": "1.1.0",
"description": "CLI regexp replacement for files with lookahead and backreference to matching groups in the replacement.",
"author": "Mathias Rangel Wulff",
"license": "MIT",
Expand All @@ -13,7 +13,8 @@
"rexreplace": "./rexreplace-cli.js"
},
"scripts": {
"test": "npm -g install ./ && bash test.sh"
"test": "npm -g install ./ && bash test.sh",
"pretest": "node -e 'file=`rexreplace-cli.js`;fs = require(`fs`);fs.writeFileSync(file,fs.readFileSync(file,`utf8`).replace(/^(const version = `).*?(?=`;)/mi,String.fromCharCode(36,49)+require(`./package.json`).version));'"
},
"keywords": [
"regexp",
Expand All @@ -23,10 +24,10 @@
"regular",
"expression"
],
"devDependencies": {
},
"devDependencies": {},
"dependencies": {
"chalk": "^1.1.3",
"globs": "^0.1.3",
"yargs": "^7.0.2"
}
}
55 changes: 45 additions & 10 deletions rexreplace-cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

const version = `1.1.0`;
const fs = require('fs');
const path = require('path');
const version = "1.0.0";
const font = require('chalk');
const globs = require('globs');

let yargs = require('yargs')
.strict()
Expand All @@ -13,6 +14,8 @@ let yargs = require('yargs')
.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')
Expand Down Expand Up @@ -70,19 +73,51 @@ let yargs = require('yargs')
.describe('€', "Stop replacing '€' with '$' in pattern and replacement")
.alias('€', 'void-euro')
.boolean('d')
.describe('d', 'Dump matches as json output')
.alias('d', 'dump')
.boolean('v')
.describe('v', "More chatty output")
.alias('v', 'verbose')
.boolean('p')
.describe('p', "Pattern will be piped in. Note that replacement must then be first argument. Other elements like -P and -€ will be applyed afterwards.")
.alias('p', 'pattern-pipe')
.boolean('r')
.describe('r', "Replacement will be piped in. Note that filename/globs must then be second argument")
.alias('r', 'replacement-pipe')
.boolean('P')
.describe('P', "Pattern is a filename from where the pattern will be generated. Pattern will be defined by each line trimmed and having newlines removed followed by other other rules (like -€).)")
.alias('P', 'pattern-file')
.boolean('R')
.describe('R', "Replacement is a filename from where the replacement will be generated. Replacement will be defined by each line trimmed and having newlines removed followed by other other rules (like -€).")
.alias('R', 'replacement-file')
.boolean('G')
.describe('G', "filename/globas are filename(s) for files containing one filename/globs on each line to be search/replaced")
.alias('G', 'globs-file')
.boolean('g')
.describe('g', "filename/globs will be piped in. If any filename/globs are given in command the piped data will be prepened")
.alias('g', 'glob-pipe')
.boolean('j')
.describe('j', "Pattern is javascript source that will return a string giving the pattern to use")
.alias('j', 'pattern-js')
.boolean('J')
.describe('J', "Replacement is javascript source that will return a string giving the replacement to use to use")
.alias('j', 'replacement-js')
.boolean('glob-js')
.describe('glob-js', "filename/globs are javascript source that will return a string with newline seperating each glob to work on")
.boolean('n')
.describe('n', "Do replacement on file names instead of file content (rename the files)")
.alias('n', 'name')
*/

.help('h')
Expand Down Expand Up @@ -130,7 +165,7 @@ try{
const replace = args._.shift();

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

files
// Correct filepath
Expand Down
6 changes: 6 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ assert "rexreplace x x myfile --output" "foobar"
reset
assert "rexreplace o x myfile --output" "fxxbar"

reset
echo foobar >> my_file
assert "rexreplace o x my*le -o" "fxxbar\nfxxbar"
rm my_file

reset
assert "rexreplace '.€' € myfile -o --eurodollar" 'fooba$'

Expand Down Expand Up @@ -85,6 +90,7 @@ assert "rexreplace '^.' 'x' myfile -o --void-multiline" "xoobar\nfoobar"
# reset

reset
rm myfile

assert_end "rexreplace"

Expand Down

0 comments on commit bf692cf

Please sign in to comment.