11import { readdir , readFile , rename , writeFile } from 'node:fs/promises'
22import { join } from 'node:path'
33
4- const EXCLUDED_DIRECTORIES = new Set ( [ 'dist' , 'coverage' , 'node_modules' , '.git' , '.github' , ' tmp'] )
4+ const EXCLUDED_DIRECTORIES = new Set ( [ 'dist' , 'coverage' , 'node_modules' , '.git' , 'tmp' ] )
55
66export async function searchAndReplace (
77 rootFolder : string ,
@@ -14,13 +14,6 @@ export async function searchAndReplace(
1414 throw new Error ( 'fromStrings and toStrings arrays must have the same length' )
1515 }
1616
17- if ( isVerbose ) {
18- console . log ( `searchAndReplace: ${ rootFolder } Searching for ${ fromStrings . join ( ', ' ) } ` , {
19- rootFolder,
20- fromStrings,
21- toStrings,
22- } )
23- }
2417 async function processFile ( filePath : string ) : Promise < void > {
2518 try {
2619 const content = await readFile ( filePath , 'utf8' )
@@ -88,14 +81,8 @@ export async function searchAndReplace(
8881 }
8982
9083 async function renamePaths ( directoryPath : string ) : Promise < void > {
91- if ( isVerbose ) {
92- console . log ( `searchAndReplace [renamePaths]: START Renaming paths in ${ directoryPath } ` )
93- }
9484 try {
9585 const entries = await readdir ( directoryPath , { withFileTypes : true } )
96- if ( isVerbose ) {
97- console . log ( `searchAndReplace [renamePaths]: Renaming paths in ${ directoryPath } ` , { entries } )
98- }
9986
10087 for ( const entry of entries ) {
10188 if ( EXCLUDED_DIRECTORIES . has ( entry . name ) ) {
@@ -105,36 +92,17 @@ export async function searchAndReplace(
10592 continue
10693 }
10794
108- if ( isVerbose ) {
109- console . log ( `searchAndReplace [renamePaths] => Renaming ${ entry . name } to ${ entry . name } ` )
110- }
11195 const oldPath = join ( directoryPath , entry . name )
112- let newPath = oldPath
96+ let newName = entry . name
11397
11498 for ( const [ i , fromString ] of fromStrings . entries ( ) ) {
115- if ( directoryPath . endsWith ( toStrings [ i ] ) ) {
116- if ( isVerbose ) {
117- console . log ( `searchAndReplace [renamePaths] => [${ i } ] Skipping ${ fromString } with ${ toStrings [ i ] } ` )
118- }
119- continue
120- }
121- if ( isVerbose ) {
122- console . log ( `searchAndReplace [renamePaths] => [${ i } ] Replacing ${ fromString } with ${ toStrings [ i ] } ` )
123- }
124- newPath = newPath . replace ( new RegExp ( fromString , 'g' ) , toStrings [ i ] )
125- if ( isVerbose ) {
126- console . log ( `searchAndReplace [renamePaths] => [${ i } ] Renamed ${ oldPath } -> ${ newPath } ` )
127- }
99+ newName = newName . replace ( new RegExp ( fromString , 'g' ) , toStrings [ i ] )
128100 }
129101
102+ const newPath = join ( directoryPath , newName )
103+
130104 if ( oldPath !== newPath ) {
131- if ( isVerbose ) {
132- console . log ( `searchAndReplace [renamePaths] => Renaming ${ oldPath } to ${ newPath } ` )
133- }
134105 if ( ! isDryRun ) {
135- if ( isVerbose ) {
136- console . log ( `searchAndReplace [renamePaths] => Renaming ${ oldPath } to ${ newPath } ` )
137- }
138106 await rename ( oldPath , newPath )
139107 }
140108 if ( isVerbose ) {
@@ -143,25 +111,16 @@ export async function searchAndReplace(
143111 }
144112
145113 if ( entry . isDirectory ( ) ) {
146- await renamePaths ( newPath )
114+ await renamePaths ( entry . isDirectory ( ) ? newPath : oldPath )
147115 }
148116 }
149- if ( isVerbose ) {
150- console . log ( `searchAndReplace [renamePaths]: END Renaming paths in ${ directoryPath } ` )
151- }
152117 } catch ( error ) {
153118 console . error ( `Error renaming paths in ${ directoryPath } :` , error )
154119 }
155120 }
156121
157122 try {
158- if ( isVerbose ) {
159- console . log ( `searchAndReplace: Processing directory ${ rootFolder } ` )
160- }
161123 await processDirectory ( rootFolder )
162- if ( isVerbose ) {
163- console . log ( `searchAndReplace: Renaming paths in ${ rootFolder } ` )
164- }
165124 await renamePaths ( rootFolder )
166125 if ( isVerbose ) {
167126 console . log ( isDryRun ? 'Dry run completed' : 'Search and replace completed' )
0 commit comments