@@ -11,20 +11,20 @@ CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-
11
11
* [ Examples] ( #examples )
12
12
13
13
## Installation
14
- ### yarn
14
+ #### yarn
15
15
```
16
16
$ yarn add frs-replace
17
17
```
18
18
19
- ### npm
19
+ #### npm
20
20
```
21
21
$ npm install frs-replace
22
22
```
23
23
24
- ### download
24
+ #### download
25
25
[ zipped from FRS-replace Releases] ( https://github.com/FRSource/FRS-replace/releases )
26
26
27
- ### Node API usage
27
+ ## Node API usage
28
28
29
29
FRS-replace package provides 2 methods for synchronous / asynchronous (with promise and ES6 ` async ` /` await ` syntax support) usage:
30
30
@@ -48,7 +48,7 @@ Where `/* options */` is an object containing:
48
48
| output | string | * undefined* | Path of an output file |
49
49
| outputOptions | string or object | utf8 | Passed as options argument of [ write's .sync] ( https://www.npmjs.com/package/write#sync ) |
50
50
51
- ### CLI usage
51
+ ## CLI usage
52
52
53
53
``` bash
54
54
FRS-replace < regex> < replacement> [options]
@@ -72,63 +72,101 @@ FRS-replace <regex> <replacement> [options]
72
72
| ‑ ; h, ‑ ;‑ ; help | boolean | * -* | Show help |
73
73
| ‑ ; v, ‑ ;‑ ; version | boolean | * -* | Show version number |
74
74
75
- ### Examples
75
+ ## Examples
76
76
77
77
> Note: while most of examples is using synchronous API method, in all cases ` .async ` is applicable as well.
78
78
79
- Replaces all ` a ` occurences with ` b ` from given ` foo.js ` and save result to ` foo_replaced.js ` :
79
+ #### 1. Replace all ` a ` occurences with ` b ` from given ` foo.js ` and returns result / writes result to console :
80
80
81
- ##### API
81
+ ###### API
82
82
``` javascript
83
- require (' FRS-replace' ).sync ({
83
+ const FRSReplace = require (' FRS-replace' )
84
+
85
+ /* synchronously */
86
+ const resultSync = FRSReplace .sync ({
84
87
input : ' foo.js' ,
85
88
regex : new RegExp (' a' , ' g' ),
86
89
replacement : ' b' ,
87
90
output : ' foo_replaced.js'
88
91
})
92
+ // work with result here
93
+
94
+ /* asynchronously */
95
+ FRSReplace .async ({
96
+ input : ' foo.js' ,
97
+ regex : new RegExp (' a' , ' g' ),
98
+ replacement : ' b'
99
+ })
100
+ .then (resultAsync => {
101
+ // work with result here */
102
+ })
103
+
104
+ /* asynchronously ES6 syntax (must be runned inside async function) */
105
+ const resultAsync = await FRSReplace .async ({
106
+ input : ' foo.js' ,
107
+ regex : new RegExp (' a' , ' g' ),
108
+ replacement : ' b'
109
+ })
110
+ // work with result here */
111
+
89
112
```
90
113
91
- ##### CLI
114
+ ###### CLI
115
+ ``` bash
116
+ FRS-replace a b -i foo.js --stdout
117
+ ```
118
+
119
+ #### 2. Replace all ` a ` occurences with ` b ` from given ` foo.js ` and save result to ` foo_replaced.js ` :
120
+
121
+ ###### API
122
+ ``` javascript
123
+ const result = require (' FRS-replace' ).sync ({
124
+ input : ' foo.js' ,
125
+ regex : new RegExp (' a' , ' g' ),
126
+ replacement : ' b' ,
127
+ output : ' foo_replaced.js'
128
+ })
129
+ ```
130
+
131
+ ###### CLI
92
132
``` bash
93
133
FRS-replace a b -i foo.js -o foo_replaced.js
94
134
```
95
135
96
- Replace all ` a ` occurences with ` b ` in given ` abcd ` and save result to ` foo_replaced.js `
136
+ #### 3. Replace all ` a ` occurences with ` b ` in given content string ` abcd ` and save result to ` foo_replaced.js `
97
137
98
138
##### API
99
139
``` javascript
100
- require (' FRS-replace' ).sync ({
140
+ const result = require (' FRS-replace' ).sync ({
101
141
content : ' abcd' ,
102
142
regex : new RegExp (' a' , ' g' ),
103
143
replacement : ' b' ,
104
144
output : ' foo_replaced.js'
105
145
})
106
146
```
107
147
108
- ##### CLI
148
+ ###### CLI
109
149
``` bash
110
150
FRS-replace a b --content abcd -o foo_replaced.js
111
151
```
112
152
113
- Replaces all ` a ` occurences with ` b ` from piped stream and save it to output file:
153
+ #### 4. Replace all ` a ` occurences with ` b ` from piped stream and save it to output file:
114
154
115
- ##### CLI
155
+ ###### CLI
116
156
``` bash
117
- < read-file> | FRS-replace a b > < output file path>
157
+ < read-file> | FRS-replace a b > < output- file- path>
118
158
```
119
159
120
- Replaces all ` a ` occurences with ` b ` from piped stream and pass it through ` stdout ` stream to next command
160
+ #### 5. Replaces all ` a ` occurences with ` b ` from piped stream and pass it through ` stdout ` stream to next command
121
161
122
- ##### CLI
162
+ ###### CLI
123
163
``` bash
124
- < read-file> | FRS-replace a b | < next command>
164
+ < read-file> | FRS-replace a b | < next- command>
125
165
```
126
166
127
- Both pipe & options styles can be mixed together, here - getting input from ` i ` argument and passing output down the stream to next command
167
+ #### 6. Both pipe & options styles can be mixed together, here - getting input from ` i ` argument and passing output down the stream to next command
128
168
129
- ##### CLI
169
+ ###### CLI
130
170
``` bash
131
- FRS-replace a b -i foo.js | < next command>
171
+ FRS-replace a b -i foo.js | < next- command>
132
172
```
133
-
134
-
0 commit comments