@@ -12,7 +12,6 @@ export type ReplaceRegexOptions = {
12
12
ignore ?: string [ ]
13
13
disableGlobs ?: boolean
14
14
fastGlobOptions ?: Parameters < typeof fastGlob > [ 1 ]
15
- countMatches ?: boolean
16
15
/**
17
16
* when passing a `string` to `from` you can make it ignore case with this flag.
18
17
* otherwise, you need to embed `i` into your regex
@@ -34,12 +33,12 @@ async function getPathsAsync(
34
33
patterns : MaybeArr < string > ,
35
34
options : ReplaceRegexOptions ,
36
35
) : Promise < string [ ] > {
37
- const { ignore, disableGlobs, fastGlobOptions : cfg } = options
36
+ const { ignore, disableGlobs, fastGlobOptions } = options
38
37
39
38
// disable globs, just ensure file(s) name
40
39
if ( disableGlobs ) return isString ( patterns ) ? [ patterns ] : patterns
41
40
42
- return await fastGlob ( patterns , { ignore, ...cfg } )
41
+ return await fastGlob ( patterns , { ignore, ...fastGlobOptions } )
43
42
}
44
43
45
44
/**
@@ -50,13 +49,12 @@ function replaceFactory(options: {
50
49
file : string
51
50
from : string | RegExp | ( ( file : string ) => string | RegExp )
52
51
to : ReplaceRegexOptions [ 'to' ]
53
- countMatches ?: ReplaceRegexOptions [ 'countMatches' ]
54
52
ignoreCase ?: ReplaceRegexOptions [ 'ignoreCase' ]
55
53
} ) : {
56
54
result : ReplaceRegexResult
57
55
newContents : string
58
56
} {
59
- const { contents, from, to, file, countMatches , ignoreCase } = options
57
+ const { contents, from, to, file, ignoreCase } = options
60
58
const result : ReplaceRegexResult = {
61
59
file,
62
60
changed : false ,
@@ -68,13 +66,11 @@ function replaceFactory(options: {
68
66
const flags = ignoreCase ? 'gi' : 'g'
69
67
const fromRegex = isString ( _from ) ? new RegExp ( _from , flags ) : _from
70
68
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
78
74
}
79
75
80
76
const newContents = isFunction ( to )
@@ -96,11 +92,10 @@ async function replaceFileAsync(options: {
96
92
file : string
97
93
from : string | RegExp | ( ( file : string ) => string | RegExp )
98
94
to : ReplaceRegexOptions [ 'to' ]
99
- countMatches : ReplaceRegexOptions [ 'countMatches' ]
100
95
dry : ReplaceRegexOptions [ 'dry' ]
101
96
ignoreCase ?: ReplaceRegexOptions [ 'ignoreCase' ]
102
97
} ) : Promise < ReplaceRegexResult > {
103
- const { file, from, to, dry, countMatches , ignoreCase } = options
98
+ const { file, from, to, dry, ignoreCase } = options
104
99
105
100
const contents = await fs . readFile ( file )
106
101
@@ -110,7 +105,6 @@ async function replaceFileAsync(options: {
110
105
from,
111
106
to,
112
107
file,
113
- countMatches,
114
108
ignoreCase,
115
109
} )
116
110
@@ -125,7 +119,7 @@ async function replaceFileAsync(options: {
125
119
* Uses fast-glob to find and replace text in files. Supports RegExp.
126
120
*/
127
121
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
129
123
// dry mode, do not replace
130
124
if ( dry ) console . log ( '[dry mode] no files will be overwritten' )
131
125
@@ -137,7 +131,7 @@ export async function replaceRegex(options: ReplaceRegexOptions): Promise<Replac
137
131
138
132
for ( const from of fromClauses ) {
139
133
for ( const file of foundFiles ) {
140
- results . push ( replaceFileAsync ( { file, from, to, countMatches , dry, ignoreCase } ) )
134
+ results . push ( replaceFileAsync ( { file, from, to, dry, ignoreCase } ) )
141
135
}
142
136
}
143
137
0 commit comments