Skip to content

Commit b289ffe

Browse files
authored
feat(input): add support for globbing matching (#14)
BREAKING CHANGE: api options rename: 'inputOptions' to 'inputReadOptions', 'outputOptions' to 'outputWriteOptions' BREAKING CHANGE: cli options rename: 'in-opts' to 'i-read-opts', 'out-opts' to 'o-write-opts' Add possibility to set input or output options through cli Docs - fixes & new example Turn off camel-case-expansion to speed up yargs a bit fixes #3
1 parent ed8051f commit b289ffe

File tree

10 files changed

+836
-412
lines changed

10 files changed

+836
-412
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
77

8+
# Idea
9+
.idea/workspace.xml
10+
811
# Runtime data
912
pids
1013
*.pid

.idea/workspace.xml

Lines changed: 250 additions & 296 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Coverage Status](https://coveralls.io/repos/github/FRSource/FRS-replace/badge.svg?branch=master)](https://coveralls.io/github/FRSource/FRS-replace?branch=master)
44
# FRS-replace
55

6-
CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input file), piping and many more!
6+
CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input files), [globbing](https://en.wikipedia.org/wiki/Glob_(programming)), [piping](https://en.wikipedia.org/wiki/Pipeline_(Unix)) and many more!
77

88
* [Installation](#installation)
99
* [Node API usage](#node-api-usage)
@@ -40,13 +40,15 @@ Where `/* options */` is an object containing:
4040
4141
| Option | Type | Default | Description |
4242
| --- | --- | --- | --- |
43-
| input | string | *undefined* | Path to file to read & replace from |
44-
| inputOptions | string or object | utf8 | Options passed to [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
43+
| input | string or \<string\>array | *undefined* | Path/[fast-glob](https://github.com/mrmlnc/fast-glob) pattern to files to read & replace from, if multiple files are specified results are joined with inputJoinString option's value |
44+
| inputReadOptions | string or object | utf8 | Options passed to [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
45+
| inputGlobOptions | object | *undefined* | Options passed to [fast-glob](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
46+
| inputJoinString | string | \n | String used when joining multiple files, passed directly to [javascript join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join#Syntax) |
4547
| content | string | *undefined* | Content to be replaced (takes precedence over file input) |
4648
| regex | string or [RegExp Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax)| *-* | Used as a first argument of [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
4749
| replacement | string | *-* | Passed as a second argument to [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
4850
| output | string | *undefined* | Path of an output file |
49-
| outputOptions | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
51+
| outputWriteOptions | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
5052

5153
## CLI usage
5254

@@ -63,14 +65,16 @@ FRS-replace <regex> <replacement> [options]
6365
#### Options:
6466
> Note: Every boolean option can be negated with use of `--no-` prefix, e.g. `--stdout` or `--no-stdout` turn stdout output on or off, respectively.
6567
66-
> Note: Object types can be set using [dot notation](https://github.com/yargs/yargs-parser#dot-notation). So, e.g. if you want to pass `utf8` value under in-opts encoding field you should write `--in-opts.encoding utf8`.
68+
> Note: Object types can be set using [dot notation](https://github.com/yargs/yargs-parser#dot-notation). So, e.g. if you want to pass `utf8` value under i-read-opts encoding field you should write `--i-read-opts.encoding utf8`.
6769
6870
| Option | Type | Default | Description |
6971
| --- | --- | --- | --- |
70-
|&#8209;i, &#8209;&#8209;input | string | *-* | File to read & replace from |
71-
| &#8209;&#8209;in-opts | string or object | utf8 | Passed to [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
72+
|&#8209;i, &#8209;&#8209;input | string or \<string\>array | *-* | Files/[fast-glob](https://github.com/mrmlnc/fast-glob) pattern to files to read & replace from |
73+
| &#8209;&#8209;i-read-opts | string or object | utf8 | Passed to [readFileSync](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
74+
| &#8209;&#8209;i-glob-opts | object | *undefined* | Passed to [fast-glob](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
75+
| &#8209;&#8209;i-join-str | string | \n | Used when joining multiple files, passed directly to [javascript join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join#Syntax) |
7276
| &#8209;o, &#8209;&#8209;output | string | *-* | Output file name/path (replaces the file if it already exists and creates any intermediate directories if they don't already exist) |
73-
| &#8209;&#8209;out-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
77+
| &#8209;&#8209;o-write-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
7478
| &#8209;f, &#8209;&#8209;flags | combination of *gim* flags | g | [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) flags |
7579
| &#8209;c, &#8209;&#8209;content | string | *-* | Content to be replaced (takes precedence over stream & file input) |
7680
| &#8209;&#8209;stdout | boolean | true if piped input present, false otherwise | Force sending output on stdout |
@@ -140,7 +144,50 @@ const result = require('FRS-replace').sync({
140144
FRS-replace a b -i foo.js -o foo_replaced.js
141145
```
142146

143-
#### 3. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`
147+
#### 3. Replace all `a` occurences with `b` from given array of files and save result to `foo_replaced.js` using default `\n` as result-joining string :
148+
149+
###### API
150+
```javascript
151+
const result = require('FRS-replace').sync({
152+
input : ['foo.js', 'foo2.js'],
153+
regex : new RegExp('a', 'g'),
154+
replacement : 'b',
155+
output : 'foo_replaced.js'
156+
})
157+
```
158+
159+
###### CLI
160+
```bash
161+
FRS-replace a b -i foo.js foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
162+
```
163+
164+
*or*
165+
166+
```bash
167+
FRS-replace a b -i foo.js -i foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
168+
```
169+
170+
> Note: Arrays can be passed under single flag-entry as a space-separated list *or* under same flag repeated multiple times (all values will be concatenated into single array using, details - [yargs array notation](https://github.com/yargs/yargs-parser#dot-notation)).
171+
172+
#### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to `foo_replaced.js` using `\n/////\n` as result-joining string :
173+
174+
###### API
175+
```javascript
176+
const result = require('FRS-replace').sync({
177+
input : 'foo/*.js',
178+
regex : new RegExp('a', 'g'),
179+
replacement : 'b',
180+
inputJoinString : '\n/////\n',
181+
output : 'foo_replaced.js'
182+
})
183+
```
184+
185+
###### CLI
186+
```bash
187+
FRS-replace a b -i foo/*.js -o foo_replaced.js --i-join-str "\n/////\n"
188+
```
189+
190+
#### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`
144191

145192
###### API
146193
```javascript
@@ -157,21 +204,21 @@ const result = require('FRS-replace').sync({
157204
FRS-replace a b --content abcd -o foo_replaced.js
158205
```
159206

160-
#### 4. Replace all `a` occurences with `b` from piped stream and save it to output file:
207+
#### 6. Replace all `a` occurences with `b` from piped stream and save it to output file:
161208

162209
###### CLI
163210
```bash
164211
<read-file> | FRS-replace a b > <output-file-path>
165212
```
166213

167-
#### 5. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command
214+
#### 7. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command
168215

169216
###### CLI
170217
```bash
171218
<read-file> | FRS-replace a b | <next-command>
172219
```
173220

174-
#### 6. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command
221+
#### 8. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command
175222

176223
###### CLI
177224
```bash

bin/cli.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,40 @@ require('get-stdin')().then((stdin) => {
4040
.option('i', { demandOption: !isContentPresent && !isHelpPresent })
4141
.alias('i', 'input')
4242
.describe('i', 'File to read & replace from')
43-
.string('i')
44-
.nargs('i', 1)
43+
.array('i')
4544

46-
.option('in-opts')
47-
.describe('in-opts', 'Passed to fs.readFileSync when reading input file')
48-
.default('in-opts', void 0, 'utf8') // will use node's default value
49-
.implies('in-opts', 'i')
45+
.option('i-read-opts')
46+
.describe('i-read-opts', 'Passed to fs.readFileSync when reading input file')
47+
.default('i-read-opts', void 0, 'utf8') // will use node's default value
48+
.implies('i-read-opts', 'i')
49+
50+
.option('i-glob-opts')
51+
.describe('i-glob-opts', 'Passed to fast-glob.sync when resolving glob patterns')
52+
.implies('i-glob-opts', 'i')
53+
54+
.option('i-join-str')
55+
.describe('i-join-str', 'Used when joining multiple files')
56+
.default('i-join-str', void 0, 'newline (\\n)') // will use node's default value
57+
.implies('i-join-str', 'i')
5058

5159
.option('o')
5260
.alias('o', 'output')
5361
.describe('o', 'Output file name/path (replaces the file if it already exists and creates any intermediate directories if they don\'t already exist)')
5462
.string('o')
5563
.nargs('o', 1)
5664

57-
.option('out-opts')
58-
.describe('out-opts', 'Passed as options argument of write\'s .sync method')
59-
.default('out-opts', void 0, 'utf8') // will use node's default value
60-
.implies('out-opts', 'o')
65+
.option('o-write-opts')
66+
.describe('o-write-opts', 'Passed as options argument of write\'s .sync method')
67+
.default('o-write-opts', void 0, 'utf8') // will use node's default value
68+
.implies('o-write-opts', 'o')
6169

6270
.option('f')
6371
.alias('f', 'flags')
6472
.describe('f', 'RegExp flags (supporting gim)')
6573
.nargs('f', 1)
6674
.choices('f', ['', 'g', 'm', 'i', 'gm', 'gi', 'mi', 'mg', 'ig', 'im', 'gmi', 'gim', 'mig', 'mgi', 'igm', 'img'])
6775
.default('f', 'g')
68-
.coerce('f', (arg) => arg.trim())
76+
.coerce('f', arg => arg.trim())
6977

7078
.option('c', { demandOption: isContentPresent })
7179
.alias('c', 'content')
@@ -92,26 +100,26 @@ require('get-stdin')().then((stdin) => {
92100
.epilog('Brought to you with open-source love by FRSource')
93101
.argv
94102

95-
let result
96-
97103
try {
98-
result = require('../src/replace').sync({
104+
const result = require('../src/replace').sync({
99105
content: argv.c,
100106
input: argv.i,
101-
inputOptions: argv['in-opts'],
107+
inputReadOptions: argv['i-read-opts'],
108+
inputGlobOptions: argv['i-glob-opts'],
109+
inputJoinString: argv['i-join-str'],
102110
regex: new RegExp(argv.regex, argv.f),
103111
replacement: argv.r ? require(argv.replacement) : argv.replacement,
104112
output: argv.o,
105-
outputOptions: argv['out-opts']
113+
outputWriteOptions: argv['o-write-opts']
106114
})
115+
116+
if (argv.stdout) {
117+
process.stdout.write(result)
118+
}
119+
120+
return process.exit()
107121
} catch (e) /* istanbul ignore next */ {
108122
process.stderr.write(e.toString())
109123
return process.exit(1)
110124
}
111-
112-
if (argv.stdout) {
113-
process.stdout.write(result)
114-
}
115-
116-
process.exit()
117125
})

0 commit comments

Comments
 (0)