import { isDirectory } from '../fs/fs.mjs';
import { execCommand } from '../node/node-command.mjs';
import { getFs } from '../nodejs/fs.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/os.mjs';
import '@babel/runtime/helpers/typeof';
import '../nodejs/child_process.mjs';

/**
 * 获取所有 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 = getFs().readdirSync(root);
  list.forEach(function (item) {
    var filePath = "".concat(root, "/").concat(item);
    if (isDirectory(filePath)) {
      var gitPath = "".concat(filePath, "/.git");
      if (getFs().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() {
  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();
}

export { getAllGitRepo };
