During the project development process, large projects will be divided into blocks, and each block will have a git address. When you want to switch the domain name of the git address, it is time-consuming and laborious for us to manually modify it one by one. If you can have a script and batch modifications at one time, it can save you a lot of time and cost.
Generally speaking, git source switching only changes the domain name, and the project name will basically not change.
Step 1:Introduce corresponding modules
// Introduce corresponding modulesconst fs = require('fs'); const path = require('path'); const { execSync } = require('child_process');
Step 2:Declare old and new domain names, search directory constants and assign values
// Old domain name and new domain nameconst OLD_DOMAIN = '/'; const NEW_DOMAIN = '/'; // Directory to searchconst SEARCH_DIR = '.'; // Current directory,Can be modified to other directories
Step 3:Define a function to iterate through all projects in the current directory. Of course, you can filter according to the folder name
// Find the Git repository and switch the remote URLfunction changeGitRemotesInFolders(dir) { (dir).forEach(file => { const fullPath = (dir, file); if(!('.vscode') && !('node_modules')){ const stats = (fullPath); if (()) { if (((fullPath, '.git'))) { // This is a Git repository changeGitRemote(fullPath); } } } }); }
Step 4:Modify the project git address one by one (note: after entering a folder and executing the modification command, you need to exit the current folder and return to the previous directory, otherwise an error message may occur if the next project cannot be found)
(folderPath); // Modify the current working directory
('..'); // Return to the previous directory
// Switch the Git remote repository URL (replace only the domain name)function changeGitRemote(folderPath) { try { (folderPath); // Get the URL of the current remote repository const remoteUrl = execSync('git config --get ').toString().trim(); // Check whether the domain name needs to be replaced if ((OLD_DOMAIN)) { const newRemoteUrl = (OLD_DOMAIN, NEW_DOMAIN); // Set up a new remote repository URL execSync(`git remote set-url origin ${newRemoteUrl}`); (`Successfully changed remote URL for ${folderPath} from ${remoteUrl} to ${newRemoteUrl}`); } else { (`No need to change remote URL for ${folderPath} (current URL: ${remoteUrl})`); } // (()); // In theory, it is not needed here, because it is a modification to the current process, but for clarity, it is still added ('..'); } catch (error) { (`Error processing ${folderPath}:`, error); } }
Complete code
const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); // Old domain name and new domain nameconst OLD_DOMAIN = '/'; const NEW_DOMAIN = '/'; // Directory to searchconst SEARCH_DIR = '.'; // The current directory can be modified to another directory// Find the Git repository and switch the remote URLfunction changeGitRemotesInFolders(dir) { (dir).forEach(file => { const fullPath = (dir, file); if(!('.vscode') && !('node_modules')){ const stats = (fullPath); if (()) { if (((fullPath, '.git'))) { // This is a Git repository changeGitRemote(fullPath); } } } }); } // Switch the Git remote repository URL (replace only the domain name)function changeGitRemote(folderPath) { try { (folderPath); // Get the URL of the current remote repository const remoteUrl = execSync('git config --get ').toString().trim(); // Check whether the domain name needs to be replaced if ((OLD_DOMAIN)) { const newRemoteUrl = (OLD_DOMAIN, NEW_DOMAIN); // Set up a new remote repository URL execSync(`git remote set-url origin ${newRemoteUrl}`); (`Successfully changed remote URL for ${folderPath} from ${remoteUrl} to ${newRemoteUrl}`); } else { (`No need to change remote URL for ${folderPath} (current URL: ${remoteUrl})`); } // (()); // In theory, it is not needed here, because it is a modification to the current process, but for clarity, it is still added ('..'); } catch (error) { (`Error processing ${folderPath}:`, error); } } // Main functionfunction main() { changeGitRemotesInFolders(SEARCH_DIR); } main();
This is the end of this article about implementing batch modification of the data source of git projects. For more related batch modification of the data source of git projects, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!