You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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!
7
7
8
8
*[Installation](#installation)
9
9
*[Node API usage](#node-api-usage)
@@ -40,13 +40,15 @@ Where `/* options */` is an object containing:
40
40
41
41
| Option | Type | Default | Description |
42
42
| --- | --- | --- | --- |
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)|
45
47
| content | string |*undefined*| Content to be replaced (takes precedence over file input) |
46
48
| 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)|
47
49
| 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)|
48
50
| 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)|
> 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.
65
67
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`.
67
69
68
70
| Option | Type | Default | Description |
69
71
| --- | --- | --- | --- |
70
-
|‑i, ‑‑input | string |*-*| File to read & replace from |
71
-
|‑‑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
+
|‑i, ‑‑input | string or \<string\>array |*-*| Files/[fast-glob](https://github.com/mrmlnc/fast-glob) pattern to files to read & replace from |
73
+
|‑‑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
+
|‑‑i-glob-opts | object |*undefined*| Passed to [fast-glob](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
75
+
|‑‑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)|
72
76
|‑o, ‑‑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
-
|‑‑out-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync)|
77
+
|‑‑o-write-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync)|
74
78
|‑f, ‑‑flags | combination of *gim* flags | g |[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) flags |
75
79
|‑c, ‑‑content | string |*-*| Content to be replaced (takes precedence over stream & file input) |
76
80
|‑‑stdout | boolean | true if piped input present, false otherwise | Force sending output on stdout |
@@ -140,7 +144,50 @@ const result = require('FRS-replace').sync({
140
144
FRS-replace a b -i foo.js -o foo_replaced.js
141
145
```
142
146
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
+
constresult=require('FRS-replace').sync({
152
+
input : ['foo.js', 'foo2.js'],
153
+
regex :newRegExp('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
+
constresult=require('FRS-replace').sync({
177
+
input :'foo/*.js',
178
+
regex :newRegExp('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`
144
191
145
192
###### API
146
193
```javascript
@@ -157,21 +204,21 @@ const result = require('FRS-replace').sync({
157
204
FRS-replace a b --content abcd -o foo_replaced.js
158
205
```
159
206
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:
161
208
162
209
###### CLI
163
210
```bash
164
211
<read-file>| FRS-replace a b ><output-file-path>
165
212
```
166
213
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
168
215
169
216
###### CLI
170
217
```bash
171
218
<read-file>| FRS-replace a b |<next-command>
172
219
```
173
220
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
0 commit comments