@@ -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 */
127121export 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
0 commit comments