t-comm
Version:
专业、稳定、纯粹的工具库
92 lines (85 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
var fs_fs = require('../fs/fs.js');
var node_nodeCommand = require('../node/node-command.js');
require('@babel/runtime/helpers/typeof');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
/**
* 获取所有 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__namespace.readdirSync(root);
list.forEach(function (item) {
var filePath = "".concat(root, "/").concat(item);
if (fs_fs.isDirectory(filePath)) {
var gitPath = "".concat(filePath, "/.git");
if (fs__namespace.existsSync(gitPath)) {
gitList.push(filePath);
} else {
getAllGitRoots(filePath, gitList);
}
}
});
}
function getGitOrigin(gitList) {
var gitOriginList = gitList.map(function (item) {
var res = node_nodeCommand.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();
}
exports.getAllGitRepo = getAllGitRepo;