usb-electron-det
v1.0.2
Published
USB 设备检测 - Electron + Vue 3 + Vite;适用linxu,windows
Downloads
21
Maintainers
Readme
usb-electron-det
USB 设备检测 - Electron + Vue 3 + Vite 示例应用;同时可打包为本地库供其他 Node.js / Electron 项目引用。
作为本地库使用(仅引入库,不引入整个项目)
其他项目可以只引用 USB 检测库,而不会引入本仓库的 Electron 应用、Vue 前端等。推荐使用打包后的 tgz 安装,这样目标项目的 node_modules 里只会出现库产物(dist-lib),不会包含本仓库源码。
1. 在本仓库中打包“仅库”的 tgz:
cd /path/to/usb-det
npm run pack:lib会先构建库并生成 usb-detection-1.0.0.tgz(版本号以 package.json 为准)。该 tgz 内仅含 dist-lib/ 与 package.json,不含 electron、src、vite 等。
2. 在其它项目中只安装该库:
cd /path/to/your-project
npm install /path/to/usb-det/usb-detection-1.0.0.tgz
# 或
yarn add /path/to/usb-det/usb-detection-1.0.0.tgz
# 或
pnpm add /path/to/usb-det/usb-detection-1.0.0.tgz安装后,node_modules/usb-detection 内只有库文件,不会带入整个本仓库。
3. 在代码中引用:
// ESM
import { UsbDetector, type UsbDeviceInfo, type UsbDriveInfo } from 'usb-detection'
const detector = new UsbDetector({ pollInterval: 2000, detectDrives: true })
detector.on('change', (e) => console.log('设备变化', e))
detector.onDriveChange((e) => console.log('U 盘变化', e))
detector.start()
// 一次性获取
const devices = await detector.getDevices()
const drives = await detector.getUsbDrives()导出 API: UsbDetector、UsbDeviceInfo、UsbDriveInfo、DeviceChangeEvent、UsbDriveChangeEvent、UsbDetectorOptions 等(见 lib/index.ts)。
可选(不推荐): 若使用 npm install file:../usb-det 或 npm link usb-detection,虽然运行时只会加载库入口,但 node_modules 中会包含本仓库完整目录。若希望仅引入库,请按上述步骤使用 npm run pack:lib 生成的 tgz 安装。
软件架构
electron/main/usb-detector.ts:USB 检测核心(无 Electron 依赖,可单独作为库)electron/main/usb-handler.ts:Electron IPC 与导出到 U 盘lib/index.ts:库入口,供外部项目引用
安装教程(运行本应用)
- 克隆仓库后执行
npm install - 开发:
npm run dev - 打包:
npm run build或npm run build:win/npm run build:linux
使用说明
- 运行应用后可在界面查看 USB 设备与 U 盘列表
- 支持热插拔检测与导出表格到 U 盘
参与贡献
- Fork 本仓库
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request
特技
- 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
- Gitee 官方博客 blog.gitee.com
- 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
- GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
- Gitee 官方提供的使用手册 https://gitee.com/help
- Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/
