Skip to content

Commit 660616b

Browse files
committed
fix: remove prop to be enabled by default
1 parent d8ee585 commit 660616b

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
lines changed

README.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,33 @@ const results = await replaceRegex({
2929
from: /foo/g,
3030
to: 'bar',
3131
dry: false,
32-
countMatches: true,
3332
})
3433

3534
console.log(results)
3635
```
3736

37+
### CLI Usage
38+
39+
You can use the `replace-regex` CLI to find and replace text in files using regular expressions right from the terminal.
40+
41+
```sh
42+
Usage
43+
$ replace-regex <files…>
44+
45+
Options
46+
--from Regex pattern or string to find (Can be set multiple times)
47+
--to Replacement string or function (Required)
48+
--dry Dry run (do not actually replace, just show what would be replaced)
49+
--no-glob Disable globbing
50+
--ignore Ignore files matching this pattern (Can be set multiple times)
51+
--ignore-case Search case-insensitively
52+
53+
Examples
54+
$ replace-regex --from='fox' --to='🦊' foo.md
55+
$ replace-regex --from='v\\d+\\.\\d+\\.\\d+' --to='v$npm_package_version' foo.css
56+
$ replace-regex --from='blob' --to='blog' 'some/**/[gb]lob/*' '!some/glob/foo'
57+
```
58+
3859
### Options
3960

4061
- `files` (string or string[]): Glob patterns or file paths to process.
@@ -44,9 +65,40 @@ console.log(results)
4465
- `ignore` (string[], optional): An array of glob patterns to exclude matches.
4566
- `disableGlobs` (boolean, optional): If true, disables glob pattern matching. Default is `false`.
4667
- `fastGlobOptions` (object, optional): Options to pass to fast-glob.
47-
- `countMatches` (boolean, optional): If true, counts the number of matches and replacements. Default is `false`.
4868

49-
### Examples
69+
### CLI Examples
70+
71+
To replace all occurrences of the word `fox` with `🦊` in the file `foo.md`:
72+
73+
```sh
74+
replace-regex --from='fox' --to='🦊' foo.md
75+
```
76+
77+
To replace version numbers in a CSS file with the version from your `package.json`:
78+
79+
```sh
80+
replace-regex --from='v\\d+\\.\\d+\\.\\d+' --to='v$npm_package_version' foo.css
81+
```
82+
83+
To replace the word `blob` with `blog` in files matching a glob pattern, while ignoring certain files:
84+
85+
```sh
86+
replace-regex --from='blob' --to='blog' 'some/**/[gb]lob/*' '!some/glob/foo'
87+
```
88+
89+
To perform a dry run (no files will be overwritten):
90+
91+
```sh
92+
replace-regex --from='fox' --to='🦊' --dry foo.md
93+
```
94+
95+
To perform a case-insensitive search and replace:
96+
97+
```sh
98+
replace-regex --from='fox' --to='🦊' --ignore-case foo.md
99+
```
100+
101+
### API Examples
50102

51103
**Replace text in JavaScript files**
52104

@@ -56,7 +108,6 @@ replaceRegex({
56108
from: /console\.log/g,
57109
to: 'logger.log',
58110
dry: false,
59-
countMatches: true,
60111
})
61112
```
62113

@@ -68,7 +119,6 @@ const result = await replaceRegex({
68119
from: /foo/g,
69120
to: 'bar',
70121
dry: true, // No files will be overwritten
71-
countMatches: true,
72122
})
73123

74124
console.log(`result → `, result)
@@ -82,6 +132,5 @@ replaceRegex({
82132
from: /foo/g,
83133
to: (match, file) => `${match.toUpperCase()} in ${file}`,
84134
dry: false,
85-
countMatches: true,
86135
})
87136
```

cli.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const cli = meow(
1414
--to Replacement string or function (Required)
1515
--dry Dry run (do not actually replace, just show what would be replaced)
1616
--no-glob Disable globbing
17-
--count-matches Count matches and replacements
1817
--ignore Ignore files matching this pattern (Can be set multiple times)
1918
--ignore-case Search case-insensitively
2019
@@ -42,10 +41,6 @@ const cli = meow(
4241
type: 'boolean',
4342
default: true,
4443
},
45-
countMatches: {
46-
type: 'boolean',
47-
default: false,
48-
},
4944
ignore: {
5045
type: 'string',
5146
isMultiple: true,
@@ -74,7 +69,6 @@ const replaceOptions = {
7469
to: cli.flags.to,
7570
dry: cli.flags.dry,
7671
disableGlobs: !cli.flags.glob,
77-
countMatches: cli.flags.countMatches,
7872
ignore: cli.flags.ignore,
7973
ignoreCase: cli.flags.ignoreCase,
8074
}

src/index.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export type ReplaceRegexOptions = {
1212
ignore?: string[]
1313
disableGlobs?: boolean
1414
fastGlobOptions?: Parameters<typeof fastGlob>[1]
15-
countMatches?: boolean
1615
/**
1716
* when passing a `string` to `from` you can make it ignore case with this flag.
1817
* otherwise, you need to embed `i` into your regex
@@ -34,12 +33,12 @@ async function getPathsAsync(
3433
patterns: MaybeArr<string>,
3534
options: ReplaceRegexOptions,
3635
): Promise<string[]> {
37-
const { ignore, disableGlobs, fastGlobOptions: cfg } = options
36+
const { ignore, disableGlobs, fastGlobOptions } = options
3837

3938
// disable globs, just ensure file(s) name
4039
if (disableGlobs) return isString(patterns) ? [patterns] : patterns
4140

42-
return await fastGlob(patterns, { ignore, ...cfg })
41+
return await fastGlob(patterns, { ignore, ...fastGlobOptions })
4342
}
4443

4544
/**
@@ -50,13 +49,12 @@ function replaceFactory(options: {
5049
file: string
5150
from: string | RegExp | ((file: string) => string | RegExp)
5251
to: ReplaceRegexOptions['to']
53-
countMatches?: ReplaceRegexOptions['countMatches']
5452
ignoreCase?: ReplaceRegexOptions['ignoreCase']
5553
}): {
5654
result: ReplaceRegexResult
5755
newContents: string
5856
} {
59-
const { contents, from, to, file, countMatches, ignoreCase } = options
57+
const { contents, from, to, file, ignoreCase } = options
6058
const result: ReplaceRegexResult = {
6159
file,
6260
changed: false,
@@ -68,13 +66,11 @@ function replaceFactory(options: {
6866
const flags = ignoreCase ? 'gi' : 'g'
6967
const fromRegex = isString(_from) ? new RegExp(_from, flags) : _from
7068

71-
if (countMatches) {
72-
const matches = contents.match(fromRegex)
73-
if (matches) {
74-
const replacements = matches.filter((match) => match !== to)
75-
result.matchCount = matches.length
76-
result.replaceCount = replacements.length
77-
}
69+
const matches = contents.match(fromRegex)
70+
if (matches) {
71+
const replacements = matches.filter((match) => match !== to)
72+
result.matchCount = matches.length
73+
result.replaceCount = replacements.length
7874
}
7975

8076
const newContents = isFunction(to)
@@ -96,11 +92,10 @@ async function replaceFileAsync(options: {
9692
file: string
9793
from: string | RegExp | ((file: string) => string | RegExp)
9894
to: ReplaceRegexOptions['to']
99-
countMatches: ReplaceRegexOptions['countMatches']
10095
dry: ReplaceRegexOptions['dry']
10196
ignoreCase?: ReplaceRegexOptions['ignoreCase']
10297
}): Promise<ReplaceRegexResult> {
103-
const { file, from, to, dry, countMatches, ignoreCase } = options
98+
const { file, from, to, dry, ignoreCase } = options
10499

105100
const contents = await fs.readFile(file)
106101

@@ -110,7 +105,6 @@ async function replaceFileAsync(options: {
110105
from,
111106
to,
112107
file,
113-
countMatches,
114108
ignoreCase,
115109
})
116110

@@ -125,7 +119,7 @@ async function replaceFileAsync(options: {
125119
* Uses fast-glob to find and replace text in files. Supports RegExp.
126120
*/
127121
export async function replaceRegex(options: ReplaceRegexOptions): Promise<ReplaceRegexResult[]> {
128-
const { files, from, dry, countMatches, to, ignoreCase } = options
122+
const { files, from, dry, to, ignoreCase } = options
129123
// dry mode, do not replace
130124
if (dry) console.log('[dry mode] no files will be overwritten')
131125

@@ -137,7 +131,7 @@ export async function replaceRegex(options: ReplaceRegexOptions): Promise<Replac
137131

138132
for (const from of fromClauses) {
139133
for (const file of foundFiles) {
140-
results.push(replaceFileAsync({ file, from, to, countMatches, dry, ignoreCase }))
134+
results.push(replaceFileAsync({ file, from, to, dry, ignoreCase }))
141135
}
142136
}
143137

test/index.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('replaceRegex', () => {
2020
to: 'TEST',
2121
disableGlobs: false,
2222
dry: false,
23-
countMatches: true,
2423
}
2524

2625
const results = await replaceRegex(options)
@@ -54,7 +53,6 @@ describe('replaceRegex', () => {
5453
to: 'TEST',
5554
disableGlobs: false,
5655
dry: true,
57-
countMatches: true,
5856
}
5957

6058
const results = await replaceRegex(options)
@@ -89,7 +87,6 @@ describe('replaceRegex', () => {
8987
to: 'TEST',
9088
disableGlobs: false,
9189
dry: false,
92-
countMatches: true,
9390
}
9491

9592
const results = await replaceRegex(options)

0 commit comments

Comments
 (0)