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
- Drop deprecated API
- Drop support for Node 4 and 5
- Refactor and clean up code base
- Leverage destructuring
- Option to disable globs (#33)
- Update readme
The return value of the library is an array of file names of files that were modified (e.g.
70
+
had some of the contents replaced). If no replacements were made, the return array will be empty.
71
+
72
+
```js
73
+
constchanges=replace.sync({
74
+
files:'path/to/files/*.html',
75
+
from:'foo',
76
+
to:'bar',
77
+
});
27
78
28
-
//Single file or glob
79
+
console.log(changes);
80
+
81
+
// [
82
+
// 'path/to/files/file1.html',
83
+
// 'path/to/files/file3.html',
84
+
// 'path/to/files/file5.html',
85
+
// ]
86
+
```
87
+
88
+
## Advanced usage
89
+
90
+
### Replace a single file or glob
91
+
```js
92
+
constoptions= {
29
93
files:'path/to/file',
94
+
};
95
+
```
96
+
97
+
### Replace multiple files or globs
30
98
31
-
//Multiple files or globs
99
+
```js
100
+
constoptions= {
32
101
files: [
33
102
'path/to/file',
34
103
'path/to/other/file',
35
104
'path/to/files/*.html',
36
105
'another/**/*.path',
37
106
],
107
+
};
108
+
```
38
109
39
-
//Replacement to make (string or regex)
40
-
from:/foo/g,
41
-
to:'bar',
110
+
### Replace first occurrence only
42
111
43
-
//Multiple replacements with the same string (replaced sequentially)
44
-
from: [/foo/g,/baz/g],
112
+
```js
113
+
constoptions= {
114
+
from:'foo',
45
115
to:'bar',
116
+
};
117
+
```
46
118
47
-
//Multiple replacements with different strings (replaced sequentially)
48
-
from: [/foo/g,/baz/g],
49
-
to: ['bar', 'bax'],
119
+
### Replace all occurrences
120
+
Please note that the value specified in the `from` parameter is passed straight to the native [String replace method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace). As such, if you pass a string as the `from` parameter, it will _only replace the first occurrence_.
50
121
51
-
//Specify if empty/invalid file paths are allowed (defaults to false)
52
-
//If set to true these paths will fail silently and no error will be thrown.
53
-
allowEmptyPaths:false,
122
+
To replace multiple occurrences at once, you must use a regular expression for the `from` parameter with the global flag enabled, e.g. `/foo/g`.
54
123
55
-
//Character encoding for reading/writing files (defaults to utf-8)
56
-
encoding:'utf8',
124
+
```js
125
+
constoptions= {
126
+
from:/foo/g,
127
+
to:'bar',
128
+
};
129
+
```
57
130
58
-
//Single file or glob to ignore
59
-
ignore:'path/to/ignored/file',
131
+
### Multiple values with the same replacement
60
132
61
-
//Multiple files or globs to ignore
62
-
ignore: [
63
-
'path/to/ignored/file',
64
-
'path/to/other/ignored_file',
65
-
'path/to/ignored_files/*.html',
66
-
'another/**/*.ignore',
67
-
]
133
+
These will be replaced sequentially.
134
+
135
+
```js
136
+
constoptions= {
137
+
from: [/foo/g,/baz/g],
138
+
to:'bar',
68
139
};
69
140
```
70
141
71
-
### Replacing multiple occurrences
72
-
Please note that the value specified in the `from` parameter is passed straight to the native [String replace method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace). As such, if you pass a string as the `from` parameter, it will _only replace the first occurrence_.
142
+
### Multiple values with different replacements
73
143
74
-
To replace multiple occurrences at once, you must use a regular expression for the `from` parameter with the global flag enabled, e.g. `/foo/g`.
144
+
These will be replaced sequentially.
145
+
146
+
```js
147
+
constoptions= {
148
+
from: [/foo/g,/baz/g],
149
+
to: ['bar', 'bax'],
150
+
};
151
+
```
75
152
76
153
### Using callbacks for `to`
77
154
As the `to` parameter is passed straight to the native [String replace method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), you can also specify a callback. The following example uses a callback to convert matching strings to lowercase:
You can disable globs if needed using this flag. Use this when you run into issues with file paths like files like `//SERVER/share/file.txt`. Defaults to `false`.
123
196
124
-
The return value of the library is an array of file names of files that were modified (e.g.
125
-
had some of the contents replaced). If no replacements were made, the return array will be empty.
197
+
```js
198
+
constoptions= {
199
+
disableGlobs:true,
200
+
};
201
+
```
126
202
127
-
For example:
203
+
### Specify character encoding
204
+
Use a different character encoding for reading/writing files. Defaults to `utf-8`.
128
205
129
206
```js
130
-
constchangedFiles=replace.sync({
131
-
files:'path/to/files/*.html',
132
-
from:'a',
133
-
to:'b',
134
-
});
135
-
136
-
// changedFiles could be an array like:
137
-
[
138
-
'path/to/files/file1.html',
139
-
'path/to/files/file3.html',
140
-
'path/to/files/file5.html',
141
-
]
207
+
constoptions= {
208
+
encoding:'utf8',
209
+
};
142
210
```
143
211
144
-
###CLI usage
212
+
## CLI usage
145
213
146
214
```sh
147
215
replace-in-file from to some/file.js,some/**/glob.js
216
+
[--configFile=replace-config.js]
148
217
[--ignore=ignore/files.js,ignore/**/glob.js]
149
218
[--encoding=utf-8]
150
-
[--allowEmptyPaths]
219
+
[--disableGlobs]
151
220
[--isRegex]
152
221
[--verbose]
153
222
```
154
223
155
-
The flags `allowEmptyPaths`, `ignore` and `encoding` are supported in the CLI.
156
-
In addition, the CLI supports the `verbose` flag to list the changed files.
157
-
158
224
Multiple files or globs can be replaced by providing a comma separated list.
159
225
226
+
The flags `disableGlobs`, `ignore` and `encoding` are supported in the CLI.
227
+
228
+
The flag `allowEmptyPaths` is not supported in the CLI as the replacement is
229
+
synchronous, and the flag is only relevant for asynchronous replacement.
230
+
231
+
To list the changed files, use the `verbose` flag.
232
+
160
233
A regular expression may be used for the `from` parameter by specifying the `--isRegex` flag.
161
234
235
+
## Version information
236
+
From version 3.0.0 onwards, replace in file requires Node 6 or higher. If you need support for Node 4 or 5, use version 2.x.x.
0 commit comments