t-comm
Version:
专业、稳定、纯粹的工具库
68 lines (65 loc) • 1.69 kB
JavaScript
import * as fs from 'fs';
import { isDirectory } from '../fs/fs.js';
import { execCommand } from '../node/node-command.js';
import '@babel/runtime/helpers/typeof';
/**
* 获取所有 git 仓库
*
* @export
* @param {string} root 根路径
* @returns {array} 路径列表
* @example
* ```ts
* getAllGitRepo('/root/yang');
*
* [
* {
* root: '/root',
* origin: 'git@git.address',
* }
* ]
* ```
*/
function getAllGitRepo(root) {
var gitList = [];
getAllGitRoots(root, gitList);
var gitOriginList = getGitOrigin(gitList);
return gitOriginList;
}
function getAllGitRoots(root, gitList) {
var list = fs.readdirSync(root);
list.forEach(function (item) {
var filePath = "".concat(root, "/").concat(item);
if (isDirectory(filePath)) {
var gitPath = "".concat(filePath, "/.git");
if (fs.existsSync(gitPath)) {
gitList.push(filePath);
} else {
getAllGitRoots(filePath, gitList);
}
}
});
}
function getGitOrigin(gitList) {
var gitOriginList = gitList.map(function (item) {
var res = execCommand('git remote -v', item);
console.log('[getGitOrigin] res: ', res);
var origin = parseItemOrigin(res);
return {
root: item,
origin: origin
};
});
return gitOriginList;
}
function parseItemOrigin(result) {
if (result === void 0) {
result = '';
}
var reg = /\s+(git@.*\.git)/;
var httpReg = /\s+(http.*)\s*\(/;
var match = result.match(reg);
var httpMatch = result.match(httpReg);
return ((match === null || match === void 0 ? void 0 : match[1]) || (httpMatch === null || httpMatch === void 0 ? void 0 : httpMatch[1]) || '').trim();
}
export { getAllGitRepo };