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
* Bump dependencies
* [feature] #38 Count number of matches
* Update make-replacements.js
* [feature] #42 Differentiate number of matches and number of replacements
* [enhance] #56 Support for CWD parameter
* Default config value
* [enhance] #63 Add --quiet flag to supress console output in CLI
* Update success-handler.js
* Update readme and add change log
The return value is now a results array instead of an array with changed files. The new results array includes each file that was processed, with a flag to indicate whether or not the file was changed, and optionally information about the number of matches and replacements that were made. See the readme for more details.
5
+
6
+
To update existing code and obtain an array of changed files again, simply convert the results array as follows:
7
+
8
+
```js
9
+
constresults=awaitreplace(options);
10
+
constchangedFiles= results
11
+
.filter(result=>result.hasChanged)
12
+
.map(result=>result.file);
13
+
```
14
+
15
+
## New features
16
+
- Added `countMatches` flag to count the number of matches and replacements per file [#38](https://github.com/adamreisnz/replace-in-file/issues/38), [#42](https://github.com/adamreisnz/replace-in-file/issues/42), [#61](https://github.com/adamreisnz/replace-in-file/issues/61)
17
+
- Added `--quiet` flag for CLI to suppress success output [#63](https://github.com/adamreisnz/replace-in-file/issues/63)
18
+
- Added `cwd` configuration parameter for network drive replacements [#56](https://github.com/adamreisnz/replace-in-file/issues/56)
19
+
20
+
# 3.0.0
21
+
22
+
## Breaking changes
23
+
From version 3.0.0 onwards, replace in file requires Node 6 or higher. If you need support for Node 4 or 5, please use version 2.x.x.
The return value of the library is an array of file names of files that were modified (e.g.
114
-
had some of the contents replaced). If no replacements were made, the return array will be empty.
115
+
The return value of the library is an array of replacement results against each file that was processed. This includes files in which no replacements were made.
116
+
117
+
Each result contains the following values:
118
+
119
+
-`file`: The path to the file that was processed
120
+
-`hasChanged`: Flag to indicate if the file was changed or not
115
121
116
122
```js
117
-
constchanges=replace.sync({
123
+
constresults=replace.sync({
118
124
files:'path/to/files/*.html',
119
-
from:'foo',
125
+
from:/foo/g,
126
+
to:'bar',
127
+
});
128
+
129
+
console.log(results);
130
+
131
+
// [
132
+
// {
133
+
// file: 'path/to/files/file1.html',
134
+
// hasChanged: true,
135
+
// },
136
+
// {
137
+
// file: 'path/to/files/file2.html',
138
+
// hasChanged: true,
139
+
// },
140
+
// {
141
+
// file: 'path/to/files/file3.html',
142
+
// hasChanged: false,
143
+
// },
144
+
// ]
145
+
146
+
```
147
+
148
+
To get an array of changed files, simply map the results as follows:
149
+
150
+
```js
151
+
constchangedFiles= results
152
+
.filter(result=>result.hasChanged)
153
+
.map(result=>result.file);
154
+
```
155
+
156
+
### Counting matches and replacements
157
+
By setting the `countMatches` configuration flag to `true`, the number of matches and replacements per file will be counted and present in the results array.
158
+
159
+
-`numMatches`: Indicates the number of times a match was found in the file
160
+
-`numReplacements`: Indicates the number of times a replacement was made in the file
161
+
162
+
Note that the number of matches can be higher than the number of replacements if a match and replacement are the same string.
163
+
164
+
```js
165
+
constresults=replace.sync({
166
+
files:'path/to/files/*.html',
167
+
from:/foo/g,
120
168
to:'bar',
169
+
countMatches:true,
121
170
});
122
171
123
-
console.log(changes);
172
+
console.log(results);
124
173
125
174
// [
126
-
// 'path/to/files/file1.html',
127
-
// 'path/to/files/file3.html',
128
-
// 'path/to/files/file5.html',
175
+
// {
176
+
// file: 'path/to/files/file1.html',
177
+
// hasChanged: true,
178
+
// numMatches: 3,
179
+
// numReplacements: 3,
180
+
// },
181
+
// {
182
+
// file: 'path/to/files/file2.html',
183
+
// hasChanged: true,
184
+
// numMatches: 1,
185
+
// numReplacements: 1,
186
+
// },
187
+
// {
188
+
// file: 'path/to/files/file3.html',
189
+
// hasChanged: false,
190
+
// numMatches: 0,
191
+
// numReplacements: 0,
192
+
// },
129
193
// ]
130
194
```
131
195
@@ -293,6 +357,9 @@ const options = {
293
357
294
358
Please note that the setting `nodir` will always be passed as `false`.
295
359
360
+
### Making replacements on network drives
361
+
To make replacements in files on network drives, you may need to specify the UNC path as the `cwd` config option. This will then be passed to glob and prefixed to your paths accordingly. See [#56](https://github.com/adamreisnz/replace-in-file/issues/56) for more details.
362
+
296
363
### Specify character encoding
297
364
Use a different character encoding for reading/writing files. Defaults to `utf-8`.
298
365
@@ -321,6 +388,7 @@ replace-in-file from to some/file.js,some/**/glob.js
321
388
[--disableGlobs]
322
389
[--isRegex]
323
390
[--verbose]
391
+
[--quiet]
324
392
[--dry]
325
393
```
326
394
@@ -331,7 +399,9 @@ The flags `--disableGlobs`, `--ignore` and `--encoding` are supported in the CLI
331
399
The setting `allowEmptyPaths` is not supported in the CLI as the replacement is
332
400
synchronous, and this setting is only relevant for asynchronous replacement.
333
401
334
-
To list the changed files, use the `--verbose` flag. To do a dry run, use `--dry`.
402
+
To list the changed files, use the `--verbose` flag. Success output can be suppressed by using the `--quiet` flag.
403
+
404
+
To do a dry run without making any actual changes, use `--dry`.
335
405
336
406
A regular expression may be used for the `from` parameter by specifying the `--isRegex` flag.
337
407
@@ -343,7 +413,9 @@ Node’s built in `path.resolve()`, so you can pass in an absolute or relative p
343
413
## Version information
344
414
From version 3.0.0 onwards, replace in file requires Node 6 or higher. If you need support for Node 4 or 5, please use version 2.x.x.
345
415
416
+
See the [Changelog](CHANGELOG.md) for more information.
417
+
346
418
## License
347
419
(MIT License)
348
420
349
-
Copyright 2015-2018, [Adam Reis](https://adam.reis.nz), co-founder at [Hello Club](https://helloclub.com/?source=npm)
421
+
Copyright 2015-2019, [Adam Reis](https://adam.reis.nz), Co-founder at [Hello Club](https://helloclub.com/?source=npm)
0 commit comments