npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rafa.li/switch-host

v1.0.2

Published

A CLI tool to manage local hosts file and flush DNS

Readme

switch-host

一个管理本地 hosts 文件和刷新 DNS 的 CLI 工具。

命令别名

  • switch-host(完整命令)
  • shost(简短别名)

功能特性

  • 📋 列出所有 hosts 条目(区分 fetch 的和其他的)
  • ➕ 添加新的 host 条目
  • ❌ 删除 host 条目(支持交互式选择和模糊匹配)
  • 🗑️ 清除所有 fetch 的条目
  • 🔄 从远程 URL 获取 hosts 并更新
  • ⚙️ 配置管理(自定义远程 URL)
  • 🔄 刷新 DNS 缓存
  • 🎨 友好的命令行界面
  • 🔒 跨平台支持(Windows, macOS, Linux)

安装

从 npm 安装

npm install -g switch-host

本地开发

git clone <repository>
cd switch-host
npm install
npm run build
npm link

使用方法

基本命令

switch-host <command> [options]
# 或使用简短别名
shost <command> [options]

命令说明

list / ls

列出所有 hosts 条目。

switch-host list
# 或
switch-host ls

输出示例:

=== Hosts List ===

Fetched entries (40):
IP                      Domain
--------------------------------------------------
140.82.112.26           alive.github.com
20.205.243.168          api.github.com

Other entries (1):
IP                      Domain
--------------------------------------------------
127.0.0.1               localhost

Total: 41 entries (40 fetched, 1 other)

add

添加新的 host 条目。

switch-host add <ip> <domain> [comment]

示例:

switch-host add 127.0.0.1 example.local
switch-host add 127.0.0.1 mysite.local "local dev site"

remove / rm

删除 host 条目。

方式一:交互式选择

switch-host remove
# 或
switch-host rm

显示所有条目,输入数字选择要删除的条目:

  • 输入单个数字:1
  • 输入多个数字(空格分隔):1 3 5
  • 按 Enter 或 Esc 取消

方式二:模糊匹配

switch-host remove <domain>

示例:

switch-host remove github

会显示所有匹配的条目并询问是否删除。

clear

清除所有 fetch 的条目。

switch-host clear

fetch

从远程 URL 获取 hosts 并更新。

switch-host fetch [url]

示例:

# 使用配置的 URL
switch-host fetch

# 使用指定的 URL
switch-host fetch https://example.com/hosts.json

更新摘要:

  • 🟢 绿色 - 新增的条目
  • 🟡 黄色 - 更新的条目
  • 🔴 红色 - 删除的条目

config

查看或设置配置。

# 查看所有配置
switch-host config

# 查看特定配置
switch-host config hostsUrl

# 设置配置
switch-host config hostsUrl https://example.com/hosts.json

配置项:

  • hostsUrl - 远程 hosts 数据的 URL(默认:https://raw.hellogithub.com/hosts.json

flush

刷新 DNS 缓存。

switch-host flush

注意: Windows 上可能需要管理员权限。

help

显示帮助信息。

switch-host help

配置

配置文件保存在项目根目录的 .switch-host-config.json

默认配置:

{
  "hostsUrl": "https://raw.hellogithub.com/hosts.json"
}

远程数据格式

支持以下数据格式:

格式 1:二维数组

[
  ["192.0.66.2", "github.blog"],
  ["20.205.243.166", "github.com"]
]

格式 2:对象数组

{
  "hosts": [
    { "ip": "192.0.66.2", "host": "github.blog" },
    { "ip": "20.205.243.166", "host": "github.com" }
  ]
}

格式 3:键值对

{
  "github.blog": "192.0.66.2",
  "github.com": "20.205.243.166"
}

项目结构

switch-host/
├── index.js                    # 主入口文件
├── build.js                    # esbuild 构建配置
├── package.json                # 项目配置
├── .switch-host-config.json    # 用户配置(可选)
└── src/
    ├── utils.js                # 工具函数和常量
    ├── config.js               # 配置管理
    ├── hosts.js                # hosts 文件管理
    └── commands.js             # 命令实现

开发

构建项目

npm run build

输出文件:dist/switch-host.js(压缩混淆后的单文件)

本地测试

npm start -- help
npm start -- list

发布到 npm

  1. 更新 package.json 中的版本号
  2. 登录 npm:
    npm login
  3. 发布:
    npm publish

许可证

MIT

贡献

欢迎提交 Issue 和 Pull Request!