当前位置: 代码网 > it编程>前端脚本>Node.js > node.js实现批量修改git项目的数据源(步骤详解)

node.js实现批量修改git项目的数据源(步骤详解)

2024年11月25日 Node.js 我要评论
在项目开发过程中,大型项目会分块,每一块都会拥有一个git地址,当想切换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();

到此这篇关于node.js实现批量修改git项目的数据源的文章就介绍到这了,更多相关node.js批量修改git项目的数据源内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com