@frsource/frs-replace 4.1.1 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,314 +1,259 @@
1
- # @frsource/frs-replace
2
-
3
- [![NPM version](https://img.shields.io/npm/v/@frsource/frs-replace.svg?style=flat)](https://www.npmjs.com/package/@frsource/frs-replace)
4
- [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
5
- [![Build Status](https://travis-ci.org/FRSource/frs-replace.svg?branch=master)](https://travis-ci.org/FRSource/frs-replace)
6
- [![Coverage Status](https://coveralls.io/repos/github/FRSource/frs-replace/badge.svg?branch=master)](https://coveralls.io/github/FRSource/frs-replace?branch=master)
7
- [![Dependabot badge](https://api.dependabot.com/badges/status?host=github&repo=FRSOURCE/frs-replace)](https://dependabot.com/)
8
- [![Dependencies status](https://david-dm.org/frsource/frs-replace.svg)](https://david-dm.org/frsource/frs-replace)
9
- [![Dev dependencies status](https://david-dm.org/frsource/frs-replace/dev-status.svg)](https://david-dm.org/frsource/frs-replace?type=dev)
10
- [![codebeat badge](https://codebeat.co/badges/5496a006-a13d-48cc-baeb-37c79a1f6444)](https://codebeat.co/projects/github-com-frsource-frs-replace-master)
11
- [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
12
-
13
- The fastest ([see benchmarks](#chart_with_upwards_trend-benchmarks)) 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!
14
-
15
- * [:scroll: Installation](#scroll-installation)
16
- * [:books: Node API](#books-node-api)
17
- * [:keyboard: CLI API](#keyboard-cli-api)
18
- * [:mag_right: Example usage](#mag_right-example-usage)
19
- * [1. Replace all `a` occurences with `b` from given `foo.js` file and return result / write result to the console](#1-replace-all-a-occurences-with-b-from-given-foojs-file-and-return-result--write-result-to-the-console)
20
- * [2. Replace all `a` occurences with `b` from given `foo.js` and save result to the `foo_replaced.js`](#2-replace-all-a-occurences-with-b-from-given-foojs-and-save-result-to-the-foo_replacedjs)
21
- * [3. Replace all `a` occurences with `b` from given array of files and save result to the `foo_replaced.js` using default `\n` as result-joining string](#3-replace-all-a-occurences-with-b-from-given-array-of-files-and-save-result-to-the-foo_replacedjs-using-default-n-as-result-joining-string)
22
- * [4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to the `foo_replaced.js` using `\n/////\n` as result-joining string](#4-replace-all-a-occurences-with-b-from-all-js-files-in-foo-directory-and-save-result-to-the-foo_replacedjs-using-nn-as-result-joining-string)
23
- * [5. Replace all `a` occurences with `b` in given content string `abcd` and save result to the `foo_replaced.js`](#5-replace-all-a-occurences-with-b-in-given-content-string-abcd-and-save-result-to-the-foo_replacedjs)
24
- * [6. Replace all `a` occurences with `b` from piped stream and save it to the output file](#6-replace-all-a-occurences-with-b-from-piped-stream-and-save-it-to-the-output-file)
25
- * [7. Replace all `a` occurences with `b` from piped stream and pass it through `stdout` stream to the `<next-command>`](#7-replace-all-a-occurences-with-b-from-piped-stream-and-pass-it-through-stdout-stream-to-the-next-command)
26
- * [8. Both pipe & options styles can be mixed together, here - getting input from `-i` argument and passing output down the stream to the `<next-command>`](#8-both-pipe--options-styles-can-be-mixed-together-here---getting-input-from--i-argument-and-passing-output-down-the-stream-to-the-next-command)
27
- * [:chart_with_upwards_trend: Benchmarks](#chart_with_upwards_trend-benchmarks)
1
+ <p align="center">
2
+ <a href="https://www.npmjs.com/package/@frsource/frs-replace">
3
+ <img src="https://img.shields.io/npm/v/@frsource/frs-replace" alt="NPM version badge">
4
+ </a>
5
+ <a href="https://bundlejs.com/?q=%40frsource%2Ffrs-replace">
6
+ <img src="https://deno.bundlejs.com/badge?q=@frsource/frs-replace" alt="GZIP size calculated by bundlejs.com">
7
+ </a>
8
+ <a href="https://github.com/semantic-release/semantic-release">
9
+ <img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release badge">
10
+ </a>
11
+ <a href="https://github.com/FRSOURCE/frs-replace/blob/master/LICENSE">
12
+ <img src="https://img.shields.io/github/license/FRSOURCE/frs-replace" alt="license MIT badge">
13
+ </a>
14
+ </p>
15
+
16
+ <h1 align="center">@frsource/frs-replace - replace content directly in your files/streams with ease (CLI included)! 📝</h1>
17
+
18
+ <p align="center">
19
+ <a href="https://github.com/FRSOURCE/autoresize-textarea/issues">File an Issue</a>
20
+ ·
21
+ <a href="#questions">Question or an idea?</a>
22
+ ·
23
+ <a href="#chart_with_upwards_trend-benchmarks">Benchmarks</a>
24
+ <br>
25
+ </p>
26
+
27
+ <p align="center"><strong>The fastest</strong> (<a href="#chart_with_upwards_trend-benchmarks">see benchmarks</a>) CLI & Node wrapper around <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">javascript replace</a> which allows in-file replacing (with possibility to save changed file as a copy), <a href="https://en.wikipedia.org/wiki/Glob_(programming)">globbing</a>, <a href="https://en.wikipedia.org/wiki/Pipeline_(Unix)">piping</a> and many more!</p>
28
+
29
+ <p align="center">
30
+ <a href="#keyboard-cli">Getting Started (CLI)</a>
31
+ ·
32
+ <a href="#mag_right-examples-and-recipes---cli">Examples and recipes (CLI)</a>
33
+ <br>
34
+ <a href="#books-node-api">Getting Started (Node)</a>
35
+ ·
36
+ <a href="#mag_right-examples-and-recipes---node">Examples and recipes (Node)</a>
37
+ <br>
38
+ </p>
28
39
 
29
40
  ## :scroll: Installation
30
41
 
31
42
  ```bash
32
- yarn add @frsource/frs-replace
43
+ npm install @frsource/frs-replace
33
44
  ```
34
45
 
35
- or
46
+ or execute it right away:
36
47
 
37
48
  ```bash
38
- npm install @frsource/frs-replace
49
+ npx @frsource/frs-replace
39
50
  ```
40
51
 
41
- or download
42
- [zipped from `@frsource/frs-replace` releases](https://github.com/FRSource/frs-replace/releases)
43
-
44
- ## :books: Node API
45
-
46
- @frsource/frs-replace package provides 2 methods: for synchronous or for asynchronous (with promise and ES6 `async`/`await` syntax support) usage:
52
+ ## :keyboard: CLI
47
53
 
48
- ```javascript
49
- const FRSReplace = require('@frsource/frs-replace');
50
- // or
51
- import * as FRSReplace from '@frsource/frs-replace';
52
-
53
- FRSReplace.sync({/* options */})
54
- FRSReplace.async({/* options */})
55
-
56
- // you might also want to import methods separately (e.g. import only one of them):
57
- const FRSReplaceSync = require('@frsource/frs-replace/sync');
58
- const FRSReplaceAsync = require('@frsource/frs-replace/async');
59
- // or
60
- import { sync, async } from '@frsource/frs-replace';
54
+ ```bash
55
+ frs-replace <regex> <replacement> [flags]
61
56
  ```
62
57
 
63
- Where `/* options */` is an object containing:
64
- > Note: remember that you need to provide some input for @frsource/frs-replace to work, so **one of parameters: input or content is required**
65
-
66
- | Option | Type | Default | Description |
67
- | --- | --- | --- | --- |
68
- | input | string or string[] | *undefined* | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
69
- | inputReadOptions | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
70
- | inputGlobOptions | object | *undefined* | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
71
- | content | string | *undefined* | Content to be replaced (takes precedence over file input) |
72
- | strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. *"join"* - joins all input files and outputs them as a single file using path passed as: *"output"*. *"preserve-structure"* - saves all files to the *"output"* directory keeping relative directory structure.*"flatten"* - same as *"preserve-structure"* but flattens the directory structure |
73
- | needle | 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) |
74
- | 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) |
75
- | output | string | *undefined* | Path of an output file |
76
- | outputWriteOptions | string or object | "utf8" | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
77
- | outputJoinString | 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) |
78
-
79
- ## :keyboard: CLI API
58
+ Basic usage:
80
59
 
81
60
  ```bash
82
- frs-replace <regex> <replacement> [options]
61
+ frs-replace something anything -i foo.js -o foo_replaced.js
62
+
63
+ # all of the occurences of "something" in `foo.js` will be replaced with "anything" and saved to `foo_replaced.js`
83
64
  ```
84
65
 
85
- ### Positionals
66
+ More examples [below](#mag_right-examples-and-recipes---cli).
67
+
68
+ ### Arguments
86
69
 
87
- | Option | Type | Description |
88
- | --- | --- | --- |
89
- | \<regex\> | string | First parameter to [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) |
90
- | \<replacement\> | string | String or path to replacement function file (see &#8209;&#8209;replace&#8209;fn switch for details) |
70
+ | Argument | Type | Description |
71
+ | --------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------- |
72
+ | \<regex\> | string | First parameter to [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) |
73
+ | \<replacement\> | string | String or path to replacement function file (see &#8209;&#8209;replace&#8209;fn switch for details) |
91
74
 
92
- ### Options
75
+ ### Flags
93
76
 
94
77
  > 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.
95
78
 
96
79
  > 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`.
97
80
 
98
- | Option | Type | Default | Description |
99
- | --- | --- | --- | --- |
100
- |&#8209;i, &#8209;&#8209;input | string or string[] | *-* | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
101
- | &#8209;&#8209;i-read-opts | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
102
- | &#8209;&#8209;i-glob-opts | object | *undefined* | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
103
- | &#8209;&#8209;stdin | boolean | *true* | Wait for stdin input (should be set to *false* when used in non-interactive terminals) |
104
- | &#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) |
105
- | &#8209;&#8209;o-write-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
106
- | &#8209;&#8209;o-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) |
107
- | &#8209;c, &#8209;&#8209;content | string | *-* | Content to be replaced (takes precedence over stream & file input) |
108
- | &#8209;s, &#8209;&#8209;strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. *"join"* - joins all input files and outputs them as a single file using path passed as: *"output"*. *"preserve-structure"* - saves all files to the *"output"* directory keeping relative directory structure.*"flatten"* - same as *"preserve-structure"* but flattens the directory structure |
109
- | &#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 |
110
- | &#8209;&#8209;stdout | boolean | *true* if piped input present, *false* otherwise | Force sending output on stdout |
111
- | &#8209;r, &#8209;&#8209;replace&#8209;fn | boolean | false | Treat replacement argument as path to file containing replacement function |
112
- | &#8209;h, &#8209;&#8209;help | boolean | *-* | Show help |
113
- | &#8209;v, &#8209;&#8209;version | boolean | *-* | Show version number |
114
-
115
- ## :mag_right: Example usage
116
-
117
- > Note: While most of examples are using synchronous API method in all cases `.async` is applicable as well.
118
-
119
- ### 1. Replace all `a` occurences with `b` from given `foo.js` file and return result / write result to the console
120
-
121
- <details><summary>Click to expand</summary>
122
-
123
- #### 1.1 API
124
-
125
- ```javascript
126
- const FRSReplace = require('@frsource/frs-replace')
127
-
128
- /* synchronously */
129
- const resultSync = FRSReplace.sync({
130
- input : 'foo.js',
131
- regex : new RegExp('a', 'g'),
132
- replacement : 'b',
133
- output : 'foo_replaced.js'
134
- })
135
- // work with result here
136
-
137
- /* asynchronously */
138
- FRSReplace.async({
139
- input : 'foo.js',
140
- regex : new RegExp('a', 'g'),
141
- replacement : 'b'
142
- })
143
- .then(resultAsync => {
144
- // work with result here */
145
- })
146
-
147
- /* asynchronously ES6 syntax (must be runned inside async function) */
148
- const resultAsync = await FRSReplace.async({
149
- input : 'foo.js',
150
- regex : new RegExp('a', 'g'),
151
- replacement : 'b'
152
- })
153
- // work with result here */
154
-
155
- ```
156
-
157
- #### 1.2 CLI
81
+ | Option | Type | Default | Description |
82
+ | ---------------------------------------- | ------------------------------------------- | ------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83
+ | &#8209;i, &#8209;&#8209;input | string or string[] | _-_ | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
84
+ | &#8209;&#8209;i-read-opts | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
85
+ | &#8209;&#8209;i-glob-opts | object | _undefined_ | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
86
+ | &#8209;&#8209;stdin | boolean | _true_ | Wait for stdin input (should be set to _false_ when used in non-interactive terminals) |
87
+ | &#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) |
88
+ | &#8209;&#8209;o-write-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
89
+ | &#8209;&#8209;o-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) |
90
+ | &#8209;c, &#8209;&#8209;content | string | _-_ | Content to be replaced (takes precedence over stream & file input) |
91
+ | &#8209;s, &#8209;&#8209;strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. _"join"_ - joins all input files and outputs them as a single file using path passed as: _"output"_. _"preserve-structure"_ - saves all files to the _"output"_ directory keeping relative directory structure._"flatten"_ - same as _"preserve-structure"_ but flattens the directory structure |
92
+ | &#8209;f, &#8209;&#8209;flags | string, combination of _gim_ flags | g | [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) flags |
93
+ | &#8209;&#8209;stdout | boolean | _true_ if piped input present, _false_ otherwise | Force sending output on stdout |
94
+ | &#8209;r, &#8209;&#8209;replace&#8209;fn | boolean | false | Treat replacement argument as path to file containing replacement function |
95
+ | &#8209;h, &#8209;&#8209;help | boolean | _-_ | Show help |
96
+ | &#8209;v, &#8209;&#8209;version | boolean | _-_ | Show version number |
97
+
98
+ ## :mag_right: Examples and recipes - CLI
99
+
100
+ ##### 1. Replace all `a` occurrences with `b` from the `foo.js` file and return the result (using CLI)
158
101
 
159
102
  ```bash
160
103
  frs-replace a b -i foo.js --stdout
161
104
  ```
162
105
 
163
- </details>
164
-
165
- ### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to the `foo_replaced.js`
166
-
167
- <details><summary>Click to expand</summary>
168
-
169
- #### 2.1 API
170
-
171
- ```javascript
172
- const result = require('@frsource/frs-replace').sync({
173
- input : 'foo.js',
174
- regex : new RegExp('a', 'g'),
175
- replacement : 'b',
176
- output : 'foo_replaced.js'
177
- })
178
- ```
179
-
180
- #### 2.2 CLI
106
+ ##### 2. Replace all `a` occurrences with `b` from `foo.js` and save the result to the `foo_replaced.js` (using CLI)
181
107
 
182
108
  ```bash
183
109
  frs-replace a b -i foo.js -o foo_replaced.js
184
110
  ```
185
111
 
186
- </details>
112
+ ##### 3. Replace all `a` occurrences with `b` from an array of files and save the result to the `foo_replaced.js` using default `\n` as a result-joining string (using CLI)
187
113
 
188
- ### 3. Replace all `a` occurences with `b` from given array of files and save result to the `foo_replaced.js` using default `\n` as result-joining string
189
-
190
- <details><summary>Click to expand</summary>
114
+ ```bash
115
+ frs-replace a b -i foo.js -i foo2.js -o foo_replaced.js
116
+ ```
191
117
 
192
- #### 3.1 API
118
+ ##### 4. Replace all `a` occurrences with `b` from all `.js` files in the `foo` directory and save the result to the `foo_replaced.js` using `\n/////\n` as a result-joining string (using CLI)
193
119
 
194
- ```javascript
195
- const result = require('@frsource/frs-replace').sync({
196
- input : ['foo.js', 'foo2.js'],
197
- regex : new RegExp('a', 'g'),
198
- replacement : 'b',
199
- output : 'foo_replaced.js'
200
- })
120
+ ```bash
121
+ frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
201
122
  ```
202
123
 
203
- #### 3.2 CLI
124
+ ##### 5. Replace all `a` occurrences with `b` in a content string `abcd` and save the result to the `foo_replaced.js` (using CLI)
204
125
 
205
126
  ```bash
206
- frs-replace a b -i foo.js foo2.js -o foo_replaced.js
127
+ frs-replace a b --content abcd -o foo_replaced.js
207
128
  ```
208
129
 
209
- or
130
+ ##### 6. Replace all `a` occurrences with `b` from a piped stream and save it to the output file (using CLI)
210
131
 
211
132
  ```bash
212
- frs-replace a b -i foo.js -i foo2.js -o foo_replaced.js
133
+ <read-file> | frs-replace a b > <output-file-path>
213
134
  ```
214
135
 
215
- > 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/blob/master/docs/tricks.md#arrays)).
216
-
217
- </details>
136
+ ##### 7. Replace all `a` occurrences with `b` from a piped stream and pass it through the `stdout` stream to the `<next-command>` (using CLI)
218
137
 
219
- ### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to the `foo_replaced.js` using `\n/////\n` as result-joining string
138
+ ```bash
139
+ <read-file> | frs-replace a b | <next-command>
140
+ ```
220
141
 
221
- <details><summary>Click to expand</summary>
142
+ ## :books: Node API
222
143
 
223
- #### 4.1 API
144
+ @frsource/frs-replace package is shipped in 2 flavors: synchronous and asynchronous:
224
145
 
225
146
  ```javascript
226
- const result = require('@frsource/frs-replace').sync({
227
- input : 'foo/*.js',
228
- regex : new RegExp('a', 'g'),
229
- replacement : 'b',
230
- outputJoinString : '\n/////\n',
231
- output : 'foo_replaced.js'
232
- })
147
+ import { sync, async } from '@frsource/frs-replace';
148
+
149
+ sync({
150
+ /* options */
151
+ });
152
+ await async({
153
+ /* options */
154
+ });
233
155
  ```
234
156
 
235
- #### 4.2 CLI
157
+ Where `/* options */` is an object containing:
236
158
 
237
- ```bash
238
- frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
239
- ```
159
+ > Note: one of parameters: **input** or **content** is required
240
160
 
241
- </details>
161
+ | Option | Type | Default | Description |
162
+ | ------------------ | ------------------------------------------------------------------------------------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
163
+ | input | string or string[] | _undefined_ | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
164
+ | inputReadOptions | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
165
+ | inputGlobOptions | object | _undefined_ | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
166
+ | content | string | _undefined_ | Content to be replaced (takes precedence over file input) |
167
+ | strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. _"join"_ - joins all input files and outputs them as a single file using path passed as: _"output"_. _"preserve-structure"_ - saves all files to the _"output"_ directory keeping relative directory structure._"flatten"_ - same as _"preserve-structure"_ but flattens the directory structure |
168
+ | needle | 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) |
169
+ | 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) |
170
+ | output | string | _undefined_ | Path of an output file |
171
+ | outputWriteOptions | string or object | "utf8" | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
172
+ | outputJoinString | 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) |
242
173
 
243
- ### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to the `foo_replaced.js`
174
+ ## :mag_right: Examples and recipes - Node
244
175
 
245
- <details><summary>Click to expand</summary>
176
+ > Note: While all of examples are using synchronous API method in all cases your can use `async` as well.
246
177
 
247
- #### 5.1 API
178
+ ##### 1. Replace all `a` occurrences with `b` from the `foo.js` file and return the result
248
179
 
249
180
  ```javascript
250
- const result = require('@frsource/frs-replace').sync({
251
- content : 'abcd',
252
- regex : new RegExp('a', 'g'),
253
- replacement : 'b',
254
- output : 'foo_replaced.js'
255
- })
181
+ import { sync } from '@frsource/frs-replace';
182
+
183
+ const result = sync({
184
+ input: 'foo.js',
185
+ regex: new RegExp('a', 'g'),
186
+ replacement: 'b',
187
+ output: 'foo_replaced.js',
188
+ });
256
189
  ```
257
190
 
258
- #### 5.2 CLI
191
+ ##### 2. Replace all `a` occurrences with `b` from `foo.js` and save the result to the `foo_replaced.js`
259
192
 
260
- ```bash
261
- frs-replace a b --content abcd -o foo_replaced.js
193
+ ```javascript
194
+ import { sync } from '@frsource/frs-replace';
195
+ const result = sync({
196
+ input: 'foo.js',
197
+ regex: new RegExp('a', 'g'),
198
+ replacement: 'b',
199
+ output: 'foo_replaced.js',
200
+ });
262
201
  ```
263
202
 
264
- </details>
203
+ ##### 3. Replace all `a` occurrences with `b` from an array of files and save the result to the `foo_replaced.js` using default `\n` as a result-joining string
265
204
 
266
- ### 6. Replace all `a` occurences with `b` from piped stream and save it to the output file
205
+ ```javascript
206
+ import { sync } from '@frsource/frs-replace';
207
+ const result = sync({
208
+ input: ['foo.js', 'foo2.js'],
209
+ regex: new RegExp('a', 'g'),
210
+ replacement: 'b',
211
+ output: 'foo_replaced.js',
212
+ });
213
+ ```
267
214
 
268
- #### 6.1 CLI
215
+ ##### 4. Replace all `a` occurrences with `b` from all `.js` files in the `foo` directory and save the result to the `foo_replaced.js` using `\n/////\n` as a result-joining string
269
216
 
270
- ```bash
271
- <read-file> | frs-replace a b > <output-file-path>
217
+ ```javascript
218
+ import { sync } from '@frsource/frs-replace';
219
+ const result = sync({
220
+ input: 'foo/*.js',
221
+ regex: new RegExp('a', 'g'),
222
+ replacement: 'b',
223
+ outputJoinString: '\n/////\n',
224
+ output: 'foo_replaced.js',
225
+ });
272
226
  ```
273
227
 
274
- ### 7. Replace all `a` occurences with `b` from piped stream and pass it through `stdout` stream to the `<next-command>`
275
-
276
- #### 7.1 CLI
228
+ ##### 5. Replace all `a` occurrences with `b` in a content string `abcd` and save the result to the `foo_replaced.js`
277
229
 
278
- ```bash
279
- <read-file> | frs-replace a b | <next-command>
230
+ ```javascript
231
+ import { sync } from '@frsource/frs-replace';
232
+ const result = sync({
233
+ content: 'abcd',
234
+ regex: new RegExp('a', 'g'),
235
+ replacement: 'b',
236
+ output: 'foo_replaced.js',
237
+ });
280
238
  ```
281
239
 
282
- ### 8. Both pipe & options styles can be mixed together, here - getting input from `-i` argument and passing output down the stream to the `<next-command>`
240
+ ## :chart_with_upwards_trend: Benchmarks
241
+
242
+ > Tested on Node v20.11.1.
283
243
 
284
- #### 8.1 CLI
244
+ ### input as glob pattern [40 files]
285
245
 
286
- ```bash
287
- frs-replace a b -i foo.js | <next-command>
288
- ```
246
+ | Rank | Library | Average latency [ms] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;average&nbsp;latency) |
247
+ | ---- | ----------------------------- | -------------------- | ----------------------------------------------------------------------------- |
248
+ | 1 | @frsource/frs-replace (sync) | 0.46 ± 1.27% | +0.00% |
249
+ | 2 | replace-in-file (async) | 0.80 ± 1.20% | +73.25% |
250
+ | 3 | @frsource/frs-replace (async) | 1.82 ± 1.38% | +294.33% |
251
+ | 4 | replace-in-file (sync) | 3.05 ± 1.19% | +558.81% |
289
252
 
290
- ## :chart_with_upwards_trend: Benchmarks
253
+ ### input & replacement as strings
291
254
 
292
- > Tested on Node v15.0.1.
293
-
294
- ### input as glob pattern [40 files x 1000 iterations x 100 repetitions]
295
-
296
- | Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
297
- | --- | --- | --- |
298
- | frs-replace async | 0.01959564 | 57.5020% |
299
- | **frs-replace sync** | 0.01244152 | 0.0000% |
300
- | replace-in-file | 0.02223758 | 78.7368% |
301
- | replace async | *N/A* | *N/A* |
302
- | replace sync | 0.06111267 | 391.1992% |
303
- | replace-string | *N/A* | *N/A* |
304
-
305
- ### input & replacement as strings [1000 iterations x 100 repetitions]
306
-
307
- | Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
308
- | --- | --- | --- |
309
- | frs-replace async | 0.00020093 | 516.9883% |
310
- | **frs-replace sync** | 0.00003257 | 0.0000% |
311
- | replace-in-file | *N/A* | *N/A* |
312
- | replace async | *N/A* | *N/A* |
313
- | replace sync | *N/A* | *N/A* |
314
- | replace-string | 0.00003438 | 5.5692% |
255
+ | Rank | Library | Average latency [ms] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;average&nbsp;latency) |
256
+ | ---- | ----------------------------- | -------------------- | ----------------------------------------------------------------------------- |
257
+ | 1 | @frsource/frs-replace (sync) | 0.01 ± 0.30% | +0.00% |
258
+ | 2 | @frsource/frs-replace (async) | 0.01 ± 0.30% | +13.68% |
259
+ | 3 | replaceString | 0.19 ± 0.94% | +2202.69% |