node.js实现批量修改git项目的数据源

        在项目开发过程中,大型项目会分块,每一块都会拥有一个git地址,当想切换git地址的域名时,如果手动一个一个去修改对我们来说费时费力的事情,如果能有一个脚本,一次性批量修改,可以给大家节省很多时间成本。

一般来讲,git源切换只是修改了域名,项目名称基本不会变化

步骤1:引入对应模块

// 引入对应模块
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

步骤2:声明新旧域名、搜索目录 常量并赋值

// 旧域名和新域名
const OLD_DOMAIN = 'http://xxx.xxx.xxx.xxx/';
const NEW_DOMAIN = 'http://xxx.xxx.xxx.xxx/';

// 要搜索的目录
const SEARCH_DIR = '.'; // 当前目录,可以修改为其他目录

 步骤3:定义一个函数,用于遍历当前目录下的所有项目,当然,你可以根据文件夹名称进行过滤

// 查找 Git 仓库并切换远程 URL
function changeGitRemotesInFolders(dir) {
    fs.readdirSync(dir).forEach(file => {
        const fullPath = path.join(dir, file);
        if(!fullPath.includes('.vscode') && !fullPath.includes('node_modules')){
          const stats = fs.statSync(fullPath);
          if (stats.isDirectory()) {
            if (fs.existsSync(path.join(fullPath, '.git'))) {
              // 这是一个 Git 仓库
              changeGitRemote(fullPath);
            }
          }
        }
    });
}

 步骤4:逐个修改项目git地址(注意:进入一个文件夹执行修改命令后,需要退出当前文件夹,回到上一级目录,不然可能会出现找不到下一个项目的报错提示)

process.chdir(folderPath); // 修改当前工作目录

process.chdir('..'); // 返回上一级目录

// 切换 Git 远程仓库 URL(只替换域名)
function changeGitRemote(folderPath) {
  try {
    process.chdir(folderPath);
    // 获取当前远程仓库的 URL
    const remoteUrl = execSync('git config --get remote.origin.url').toString().trim();
    // 检查是否需要替换域名
    if (remoteUrl.includes(OLD_DOMAIN)) {
        const newRemoteUrl = remoteUrl.replace(OLD_DOMAIN, NEW_DOMAIN);
        // 设置新的远程仓库 URL
        execSync(`git remote set-url origin ${newRemoteUrl}`);
        console.log(`Successfully changed remote URL for ${folderPath} from ${remoteUrl} to ${newRemoteUrl}`);
    } else {
        console.log(`No need to change remote URL for ${folderPath} (current URL: ${remoteUrl})`);
    }
    // process.chdir(process.cwd()); // 理论上这里不需要,因为 process.chdir 是对当前进程的修改,但为清晰起见还是加上
    process.chdir('..');
  } catch (error) {
    console.error(`Error processing ${folderPath}:`, error);
  }
}

完整代码 

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// 旧域名和新域名
const OLD_DOMAIN = 'http://xxx.xxx.xxx.xxx/';
const NEW_DOMAIN = 'http://xxx.xxx.xxx.xxx/';

// 要搜索的目录
const SEARCH_DIR = '.'; // 当前目录,可以修改为其他目录

// 查找 Git 仓库并切换远程 URL
function changeGitRemotesInFolders(dir) {
  fs.readdirSync(dir).forEach(file => {
    const fullPath = path.join(dir, file);
    if(!fullPath.includes('.vscode') && !fullPath.includes('node_modules')){
      const stats = fs.statSync(fullPath);
      if (stats.isDirectory()) {
        if (fs.existsSync(path.join(fullPath, '.git'))) {
          // 这是一个 Git 仓库
          changeGitRemote(fullPath);
        }
      }
    }
  });
}

// 切换 Git 远程仓库 URL(只替换域名)
function changeGitRemote(folderPath) {
  try {
    process.chdir(folderPath);
    // 获取当前远程仓库的 URL
    const remoteUrl = execSync('git config --get remote.origin.url').toString().trim();
    // 检查是否需要替换域名
    if (remoteUrl.includes(OLD_DOMAIN)) {
        const newRemoteUrl = remoteUrl.replace(OLD_DOMAIN, NEW_DOMAIN);
        // 设置新的远程仓库 URL
        execSync(`git remote set-url origin ${newRemoteUrl}`);
        console.log(`Successfully changed remote URL for ${folderPath} from ${remoteUrl} to ${newRemoteUrl}`);
    } else {
        console.log(`No need to change remote URL for ${folderPath} (current URL: ${remoteUrl})`);
    }
    // process.chdir(process.cwd()); // 理论上这里不需要,因为 process.chdir 是对当前进程的修改,但为清晰起见还是加上
    process.chdir('..');
  } catch (error) {
    console.error(`Error processing ${folderPath}:`, error);
  }
}

// 主函数
function main() {
  changeGitRemotesInFolders(SEARCH_DIR);
}

main();

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小灰灰学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值