
# https://juejin.cn/post/7054747440032776199

# shellcheck shell=sh disable=SC3043
# Reference: https://zhuanlan.zhihu.com/p/35856841

___x_cmd_mirror_pnpm(){
    [ "$#" -gt 0 ] ||   set -- --help

    local op="$1"; shift
    case "$op" in
        -h|--help)
            ___x_cmd help -m mirror pnpm         "$@"
            return   ;;
    esac

    ___x_cmd hascmd pnpm || N=mirror M="[command=pnpm] not found." log:ret:1
    case "$op" in
        ls|current|set|unset)  ___x_cmd_mirror_pnpm_"${op}"  "$@" ;;
        *)                     ___x_cmd_mirror___util_subcmd_invalid pnpm "$@" ;;
    esac
}

___x_cmd_mirror_pnpm_ls(){
    printf "%s\n" \
        "Code         Url                                               Name"                     \
        "official     https://registry.npmjs.org/                       官方源"                    \
        "npmmirror    https://registry.npmmirror.com/                   npmmirror 镜像/阿里云镜像"  \
        "tencent      http://mirrors.tencent.com/npm/                   腾讯云镜像"                \
        "huawei       https://mirrors.huaweicloud.com/repository/npm/   华为云镜像源"
}


___x_cmd_mirror_pnpm___url(){
    ___x_cmd_mirror_pnpm_ls | \
        ___x_cmd_mirror___util_get_url_by_name "$1"
}

___x_cmd_mirror_pnpm_get(){
    ___x_cmd_cmds pnpm config get registry
}

___x_cmd_mirror_pnpm_set() {
    [ "$#" -gt 0 ] ||   set -- npmmirror
    case "$1" in
        -h|--help)
            ___x_cmd help -m mirror pnpm set "$@"
            return   ;;
    esac

    local url
    url="$(___x_cmd_mirror_pnpm___url "$1")" || return $?
    mirror:info "Setting pnpm mirror $url"
    ___x_cmd_cmds pnpm config set registry "$url"
}

___x_cmd_mirror_pnpm_unset() {
        ___x_cmd_mirror_pnpm_set   official
}
