0.0.1-1 • Published 3 years ago

@easystack-fe/switch-proxy-target v0.0.1-1

Weekly downloads
1
License
ISC
Repository
-
Last release
3 years ago

切换代理目标工具

简化前端开发多套接口环境切换步骤

通过环境变量控制代理地址切换

工具环境配置说明

  1. 安装npm install switch-proxy-target

  2. 初始化配置工具

❯ npx switch-proxy-target init  
init setting .sptrc.json  
  1. 配置工具参数,编辑.sptrc.json:
{
  "$schema": "node_modules/schema/setting.json",
  "targets": [
      {
          "name":"代理目标地址1",
          "hostname":"代理目标地址"
      }
   ],
  "syncHosts": [
      "选择目标需要同步的hosts域名,如:example.org"
   ]
}
  1. 修改项目proxy.conf.js配置中target:
{
    // ...
    target: `https://${process.env.PROXY_TARGET}`
    //...
}
  1. 增加package.jsonscripts
{
    "scripts":{
        //...
        "prestart":"switch-proxy-target select"
        //...
    }
}

切换环境

  • switch-proxy-target select
❯ switch-proxy-target select
.env.local false
? Please select the proxy target: 3节点安装环境:example.org   
----------Start detecting target accessability and fetch ip------------

正在 Ping example.org [192.168.1.1] 具有 32 字节的数据:

来自 192.168.1.1 的回复: 字节=32 时间=72ms TTL=62

192.168.1.1 的 Ping 统计信息:
    数据包: 已发送 = 1,已接收 = 1,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 72ms,最长 = 72ms,平均 = 72ms

ok !
----------Start setting proxy target------------
sync .env.local PROXY_TARGET
  • switch-proxy-target select --syncEtcHosts true切换代理并同步/etc/hosts(注:需要管理员权限)
❯ switch-proxy-target select --syncEtcHosts true
.env.local false
? Please select the proxy target: 3节点安装环境:example.org   
----------Start detecting target accessability and fetch ip------------

正在 Ping example.org [192.168.1.1] 具有 32 字节的数据:

来自 192.168.1.1 的回复: 字节=32 时间=72ms TTL=62

192.168.1.1 的 Ping 统计信息:
    数据包: 已发送 = 1,已接收 = 1,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 72ms,最长 = 72ms,平均 = 72ms

ok !
----------Start setting proxy target------------
sync .env.local PROXY_TARGET
sync hosts file example1.org ok!