node.js實(shí)現(xiàn)批量修改git項(xiàng)目的數(shù)據(jù)源(步驟詳解)
在項(xiàng)目開發(fā)過程中,大型項(xiàng)目會分塊,每一塊都會擁有一個(gè)git地址,當(dāng)想切換git地址的域名時(shí),如果手動一個(gè)一個(gè)去修改對我們來說費(fèi)時(shí)費(fèi)力的事情,如果能有一個(gè)腳本,一次性批量修改,可以給大家節(jié)省很多時(shí)間成本。
一般來講,git源切換只是修改了域名,項(xiàng)目名稱基本不會變化
步驟1:引入對應(yīng)模塊
// 引入對應(yīng)模塊
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 = '.'; // 當(dāng)前目錄,可以修改為其他目錄
步驟3:定義一個(gè)函數(shù),用于遍歷當(dāng)前目錄下的所有項(xiàng)目,當(dāng)然,你可以根據(jù)文件夾名稱進(jìn)行過濾
// 查找 Git 倉庫并切換遠(yuǎn)程 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'))) {
// 這是一個(gè) Git 倉庫
changeGitRemote(fullPath);
}
}
}
});
}步驟4:逐個(gè)修改項(xiàng)目git地址(注意:進(jìn)入一個(gè)文件夾執(zhí)行修改命令后,需要退出當(dāng)前文件夾,回到上一級目錄,不然可能會出現(xiàn)找不到下一個(gè)項(xiàng)目的報(bào)錯(cuò)提示)
process.chdir(folderPath); // 修改當(dāng)前工作目錄
process.chdir('..'); // 返回上一級目錄
// 切換 Git 遠(yuǎn)程倉庫 URL(只替換域名)
function changeGitRemote(folderPath) {
try {
process.chdir(folderPath);
// 獲取當(dāng)前遠(yuǎn)程倉庫的 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);
// 設(shè)置新的遠(yuǎn)程倉庫 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()); // 理論上這里不需要,因?yàn)?process.chdir 是對當(dāng)前進(jìn)程的修改,但為清晰起見還是加上
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 = '.'; // 當(dāng)前目錄,可以修改為其他目錄
// 查找 Git 倉庫并切換遠(yuǎn)程 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'))) {
// 這是一個(gè) Git 倉庫
changeGitRemote(fullPath);
}
}
}
});
}
// 切換 Git 遠(yuǎn)程倉庫 URL(只替換域名)
function changeGitRemote(folderPath) {
try {
process.chdir(folderPath);
// 獲取當(dāng)前遠(yuǎn)程倉庫的 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);
// 設(shè)置新的遠(yuǎn)程倉庫 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()); // 理論上這里不需要,因?yàn)?process.chdir 是對當(dāng)前進(jìn)程的修改,但為清晰起見還是加上
process.chdir('..');
} catch (error) {
console.error(`Error processing ${folderPath}:`, error);
}
}
// 主函數(shù)
function main() {
changeGitRemotesInFolders(SEARCH_DIR);
}
main();到此這篇關(guān)于node.js實(shí)現(xiàn)批量修改git項(xiàng)目的數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)node.js批量修改git項(xiàng)目的數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
socket.io學(xué)習(xí)教程之基礎(chǔ)介紹(一)
socket.io提供了基于事件的實(shí)時(shí)雙向通訊,所以下面這篇文章主要介紹了關(guān)于socket.io的相關(guān)資料,主要介紹了學(xué)習(xí)socket.io的基礎(chǔ)知識,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04
nodejs express配置自簽名https服務(wù)器的方法
這篇文章主要介紹了nodejs express配置自簽名https服務(wù)器的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
基于Node.js實(shí)現(xiàn)一鍵生成個(gè)性化二維碼
這篇文章主要為大家詳細(xì)介紹了如何使用Node.js、Jimp和QRCode庫,結(jié)合一個(gè)簡單的腳本,通過命令行命令來快速給二維碼加上指定的背景,打造更有個(gè)性化的二維碼,感興趣的可以了解下2024-03-03
參考?EventEmitter實(shí)現(xiàn)一個(gè)簡單的訂閱發(fā)布功能函數(shù)
這篇文章主要為大家介紹了參考?EventEmitter實(shí)現(xiàn)一個(gè)簡單的訂閱發(fā)布功能函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

