'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fs_fs = require('../fs/fs.js');
var node_nodeCommand = require('../node/node-command.js');
var nodejs_fs = require('../nodejs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/os.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');

/**
 * 获取所有 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 = nodejs_fs.getFs().readdirSync(root);
  list.forEach(function (item) {
    var filePath = "".concat(root, "/").concat(item);
    if (fs_fs.isDirectory(filePath)) {
      var gitPath = "".concat(filePath, "/.git");
      if (nodejs_fs.getFs().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() {
  var result = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  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;
