node.js實現(xiàn)批量修改git項目的數(shù)據(jù)源(步驟詳解)
在項目開發(fā)過程中,大型項目會分塊,每一塊都會擁有一個git地址,當(dāng)想切換git地址的域名時,如果手動一個一個去修改對我們來說費時費力的事情,如果能有一個腳本,一次性批量修改,可以給大家節(jié)省很多時間成本。
一般來講,git源切換只是修改了域名,項目名稱基本不會變化
步驟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:定義一個函數(shù),用于遍歷當(dāng)前目錄下的所有項目,當(dāng)然,你可以根據(jù)文件夾名稱進行過濾
// 查找 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地址(注意:進入一個文件夾執(zhí)行修改命令后,需要退出當(dāng)前文件夾,回到上一級目錄,不然可能會出現(xiàn)找不到下一個項目的報錯提示)
process.chdir(folderPath); // 修改當(dāng)前工作目錄
process.chdir('..'); // 返回上一級目錄
// 切換 Git 遠程倉庫 URL(只替換域名) function changeGitRemote(folderPath) { try { process.chdir(folderPath); // 獲取當(dāng)前遠程倉庫的 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è)置新的遠程倉庫 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 是對當(dāng)前進程的修改,但為清晰起見還是加上 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 倉庫并切換遠程 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); // 獲取當(dāng)前遠程倉庫的 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è)置新的遠程倉庫 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 是對當(dāng)前進程的修改,但為清晰起見還是加上 process.chdir('..'); } catch (error) { console.error(`Error processing ${folderPath}:`, error); } } // 主函數(shù) function main() { changeGitRemotesInFolders(SEARCH_DIR); } main();
到此這篇關(guān)于node.js實現(xiàn)批量修改git項目的數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)node.js批量修改git項目的數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法實例分析
這篇文章主要介紹了node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法,結(jié)合實例形式分析了node.js stream流可讀流和可寫流基本分類、原理、定義、使用方法及相關(guān)注意事項,需要的朋友可以參考下2020-02-02node.js Sequelize實現(xiàn)單實例字段或批量自增、自減
Sequelize 可以實現(xiàn)針對單個實例的一或多個字段的自增、自減操作,也可以對符合條件的數(shù)據(jù)進行批量的自增、自減操作。單個實例字段的自增、自減可以利用Instance的相應(yīng)方法實現(xiàn),而批量自增、自減則需要借助sequelize提供的字面量方法實現(xiàn)。下面來看看詳細(xì)的介紹吧。2016-12-12使用ExcelJS快速處理Node.js爬蟲數(shù)據(jù)
Excel.js是一個強大的JavaScript庫,它提供了方法處理Excel文件,例如創(chuàng)建和編輯工作簿、讀取和寫入數(shù)據(jù)、處理行和列、設(shè)置樣式、導(dǎo)入和導(dǎo)出數(shù)據(jù)等,本文介紹使用ExcelJS快速處理Node.js爬蟲數(shù)據(jù)的方法,一起看看吧2024-01-01nodeJS實現(xiàn)簡單網(wǎng)頁爬蟲功能的實例(分享)
下面小編就為大家?guī)硪黄猲odeJS實現(xiàn)簡單網(wǎng)頁爬蟲功能的實例(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06