import { execCommand } from '../node/node-command.mjs';
import { getPath } from '../nodejs/path.mjs';
import { transformGitToSSH } from './ssh.mjs';
import '@babel/runtime/helpers/typeof';
import '../nodejs/child_process.mjs';

/**
 * 根据配置表，重新 clone 仓库
 * @param {Array<item>} list 列表
 * @param {string} item.root 路径
 * @param {string} item.origin origin
 * @example
 * ```ts
 * reCloneGitRemote([
 *   {
 *     root: '/Users/foo/Documents/git/t-comm',
 *     origin: 'https://github.com/novlan1/t-comm',
 *   },
 *   {
 *     root: '/Users/foo/Documents/git/api',
 *     origin: 'git@git.woa.com:pmd-mobile/pmd-h5/api.git',
 *   },
 * ]);
 * // 会先 rm -rf 目标目录，再用 ssh 协议重新 clone
 * ```
 */
function reCloneGitRemote() {
  var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  list.forEach(function (item) {
    var root = item.root,
      origin = item.origin;
    var realOrigin = transformGitToSSH(origin.trim());
    console.log('[reCloneGitRemote] root: ', root);
    console.log('[reCloneGitRemote] origin: ', realOrigin);
    execCommand("rm -rf ".concat(root));
    var dir = getPath().dirname(root);
    console.log('[reCloneGitRemote] dir: ', dir);
    try {
      execCommand("git clone ".concat(realOrigin), dir, 'inherit');
    } catch (err) {
      console.log('[reCloneGitRemote] err: ', err);
    }
  });
}

export { reCloneGitRemote };
