node.js實(shí)現(xiàn)批量修改git項(xiàng)目的數(shù)據(jù)源(步驟詳解)
在項(xiàng)目開(kāi)發(fā)過(guò)程中,大型項(xiàng)目會(huì)分塊,每一塊都會(huì)擁有一個(gè)git地址,當(dāng)想切換git地址的域名時(shí),如果手動(dòng)一個(gè)一個(gè)去修改對(duì)我們來(lái)說(shuō)費(fèi)時(shí)費(fèi)力的事情,如果能有一個(gè)腳本,一次性批量修改,可以給大家節(jié)省很多時(shí)間成本。
一般來(lái)講,git源切換只是修改了域名,項(xiàng)目名稱(chēng)基本不會(huì)變化
步驟1:引入對(duì)應(yīng)模塊
// 引入對(duì)應(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ù)文件夾名稱(chēng)進(jìn)行過(guò)濾
// 查找 Git 倉(cāng)庫(kù)并切換遠(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 倉(cāng)庫(kù) changeGitRemote(fullPath); } } } }); }
步驟4:逐個(gè)修改項(xiàng)目git地址(注意:進(jìn)入一個(gè)文件夾執(zhí)行修改命令后,需要退出當(dāng)前文件夾,回到上一級(jí)目錄,不然可能會(huì)出現(xiàn)找不到下一個(gè)項(xiàng)目的報(bào)錯(cuò)提示)
process.chdir(folderPath); // 修改當(dāng)前工作目錄
process.chdir('..'); // 返回上一級(jí)目錄
// 切換 Git 遠(yuǎn)程倉(cāng)庫(kù) URL(只替換域名) function changeGitRemote(folderPath) { try { process.chdir(folderPath); // 獲取當(dāng)前遠(yuǎn)程倉(cāng)庫(kù)的 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)程倉(cāng)庫(kù) 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 是對(duì)當(dāng)前進(jìn)程的修改,但為清晰起見(jià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 倉(cāng)庫(kù)并切換遠(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 倉(cāng)庫(kù) changeGitRemote(fullPath); } } } }); } // 切換 Git 遠(yuǎn)程倉(cāng)庫(kù) URL(只替換域名) function changeGitRemote(folderPath) { try { process.chdir(folderPath); // 獲取當(dāng)前遠(yuǎn)程倉(cāng)庫(kù)的 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)程倉(cāng)庫(kù) 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 是對(duì)當(dāng)前進(jìn)程的修改,但為清晰起見(jià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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
node.js中stream流中可讀流和可寫(xiě)流的實(shí)現(xiàn)與使用方法實(shí)例分析
這篇文章主要介紹了node.js中stream流中可讀流和可寫(xiě)流的實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了node.js stream流可讀流和可寫(xiě)流基本分類(lèi)、原理、定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02node.js中的console.time方法使用說(shuō)明
這篇文章主要介紹了node.js中的console.time方法使用說(shuō)明,本文介紹了console.time的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12node.js Sequelize實(shí)現(xiàn)單實(shí)例字段或批量自增、自減
Sequelize 可以實(shí)現(xiàn)針對(duì)單個(gè)實(shí)例的一或多個(gè)字段的自增、自減操作,也可以對(duì)符合條件的數(shù)據(jù)進(jìn)行批量的自增、自減操作。單個(gè)實(shí)例字段的自增、自減可以利用Instance的相應(yīng)方法實(shí)現(xiàn),而批量自增、自減則需要借助sequelize提供的字面量方法實(shí)現(xiàn)。下面來(lái)看看詳細(xì)的介紹吧。2016-12-12使用ExcelJS快速處理Node.js爬蟲(chóng)數(shù)據(jù)
Excel.js是一個(gè)強(qiáng)大的JavaScript庫(kù),它提供了方法處理Excel文件,例如創(chuàng)建和編輯工作簿、讀取和寫(xiě)入數(shù)據(jù)、處理行和列、設(shè)置樣式、導(dǎo)入和導(dǎo)出數(shù)據(jù)等,本文介紹使用ExcelJS快速處理Node.js爬蟲(chóng)數(shù)據(jù)的方法,一起看看吧2024-01-01Node.js console控制臺(tái)簡(jiǎn)單用法分析
這篇文章主要介紹了Node.js console控制臺(tái)簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了nodejs console控制臺(tái)功能、常見(jiàn)函數(shù)與簡(jiǎn)單使用技巧,需要的朋友可以參考下2019-01-01node中IO以及定時(shí)器優(yōu)先級(jí)詳解
這篇文章主要給大家介紹了關(guān)于node中IO以及定時(shí)器優(yōu)先級(jí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05gyp?ERR!報(bào)錯(cuò)問(wèn)題解決辦法
這篇文章主要給大家介紹了關(guān)于gyp?ERR!報(bào)錯(cuò)問(wèn)題的解決辦法,文中將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01nodeJS實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)爬蟲(chóng)功能的實(shí)例(分享)
下面小編就為大家?guī)?lái)一篇nodeJS實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)爬蟲(chóng)功能的實(shí)例(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06