@@ -96,61 +96,65 @@ export async function quickCreateNewVenv(envManagers: EnvironmentManagers, destF
9696}
9797
9898/**
99- * Recursively replaces all occurrences of a string in file and folder names, as well as file contents, within a directory tree.
100- * @param dir - The root directory to start the replacement from.
99+ * Replaces all occurrences of a string in file and folder names, as well as file contents, within a directory tree or a single file .
100+ * @param targetPath - The root directory or file path to start the replacement from.
101101 * @param searchValue - The string to search for in names and contents.
102102 * @param replaceValue - The string to replace with.
103103 * @returns {Promise<void> } Resolves when all replacements are complete.
104104 */
105- export async function replaceInFilesAndNames ( dir : string , searchValue : string , replaceValue : string ) {
106- const entries = await fs . readdir ( dir , { withFileTypes : true } ) ;
107- for ( const entry of entries ) {
108- let entryName = entry . name ;
109- let fullPath = path . join ( dir , entryName ) ;
110- let newFullPath = fullPath ;
111- // If the file or folder name contains searchValue, rename it
112- if ( entryName . includes ( searchValue ) ) {
113- const newName = entryName . replace ( new RegExp ( searchValue , 'g' ) , replaceValue ) ;
114- newFullPath = path . join ( dir , newName ) ;
115- await fs . rename ( fullPath , newFullPath ) ;
116- entryName = newName ;
117- }
118- if ( entry . isDirectory ( ) ) {
119- await replaceInFilesAndNames ( newFullPath , searchValue , replaceValue ) ;
120- } else {
121- let content = await fs . readFile ( newFullPath , 'utf8' ) ;
122- if ( content . includes ( searchValue ) ) {
123- content = content . replace ( new RegExp ( searchValue , 'g' ) , replaceValue ) ;
124- await fs . writeFile ( newFullPath , content , 'utf8' ) ;
105+ export async function replaceInFilesAndNames ( targetPath : string , searchValue : string , replaceValue : string ) {
106+ const stat = await fs . stat ( targetPath ) ;
107+
108+ if ( stat . isDirectory ( ) ) {
109+ const entries = await fs . readdir ( targetPath , { withFileTypes : true } ) ;
110+ for ( const entry of entries ) {
111+ let entryName = entry . name ;
112+ let fullPath = path . join ( targetPath , entryName ) ;
113+ let newFullPath = fullPath ;
114+ // If the file or folder name contains searchValue, rename it
115+ if ( entryName . includes ( searchValue ) ) {
116+ const newName = entryName . replace ( new RegExp ( searchValue , 'g' ) , replaceValue ) ;
117+ newFullPath = path . join ( targetPath , newName ) ;
118+ await fs . rename ( fullPath , newFullPath ) ;
119+ entryName = newName ;
125120 }
121+ await replaceInFilesAndNames ( newFullPath , searchValue , replaceValue ) ;
122+ }
123+ } else if ( stat . isFile ( ) ) {
124+ let content = await fs . readFile ( targetPath , 'utf8' ) ;
125+ if ( content . includes ( searchValue ) ) {
126+ content = content . replace ( new RegExp ( searchValue , 'g' ) , replaceValue ) ;
127+ await fs . writeFile ( targetPath , content , 'utf8' ) ;
126128 }
127129 }
128130}
129131
130132/**
131133 * Ensures the .github/copilot-instructions.md file exists at the given root, creating or appending as needed.
134+ * Performs multiple find-and-replace operations as specified by the replacements array.
132135 * @param destinationRootPath - The root directory where the .github folder should exist.
133- * @param instructionsText - The text to write or append to the copilot-instructions.md file.
136+ * @param instructionsFilePath - The path to the instructions template file.
137+ * @param replacements - An array of tuples [{ text_to_find_and_replace, text_to_replace_it_with }]
134138 */
135139export async function manageCopilotInstructionsFile (
136140 destinationRootPath : string ,
137- packageName : string ,
138141 instructionsFilePath : string ,
142+ replacements : Array < { searchValue : string ; replaceValue : string } > ,
139143) {
140- const instructionsText = `\n \n` + ( await fs . readFile ( instructionsFilePath , 'utf-8' ) ) ;
144+ let instructionsText = `\n\n` + ( await fs . readFile ( instructionsFilePath , 'utf-8' ) ) ;
145+ for ( const { searchValue : text_to_find_and_replace , replaceValue : text_to_replace_it_with } of replacements ) {
146+ instructionsText = instructionsText . replace ( new RegExp ( text_to_find_and_replace , 'g' ) , text_to_replace_it_with ) ;
147+ }
141148 const githubFolderPath = path . join ( destinationRootPath , '.github' ) ;
142149 const customInstructionsPath = path . join ( githubFolderPath , 'copilot-instructions.md' ) ;
143150 if ( ! ( await fs . pathExists ( githubFolderPath ) ) ) {
144- // make the .github folder if it doesn't exist
145151 await fs . mkdir ( githubFolderPath ) ;
146152 }
147153 const customInstructions = await fs . pathExists ( customInstructionsPath ) ;
148154 if ( customInstructions ) {
149- // Append to the existing file
150- await fs . appendFile ( customInstructionsPath , instructionsText . replace ( / < p a c k a g e _ n a m e > / g, packageName ) ) ;
155+ await fs . appendFile ( customInstructionsPath , instructionsText ) ;
151156 } else {
152- // Create the file if it doesn't exist
153- await fs . writeFile ( customInstructionsPath , instructionsText . replace ( / < p a c k a g e _ n a m e > / g, packageName ) ) ;
157+ await fs . writeFile ( customInstructionsPath , instructionsText ) ;
154158 }
155159}
156160
0 commit comments