Skip to content

Commit 086867d

Browse files
committed
fix(cli): 优化USB数据文件路径处理逻辑
- 添加isProd变量判断当前环境是否为生产环境 - 根据isProd决定USB数据文件路径指向node_modules或根目录 - 修复USB数据文件路径拼接方式,确保文件读取正确 - 增加缺失的路径存在性检查日志警告提示 - 优化代码结构提升可读性和维护性
1 parent e2a62d2 commit 086867d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/cli.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ async function startWebServer(port = 3000): Promise<void> {
143143
try {
144144
const root = process.cwd()
145145
let distDir = path.join(root, 'node_modules', 'usb.ids', 'dist', 'ui')
146-
146+
let isProd = true
147147
if (!fs.existsSync(distDir)) {
148148
logger.warn('node_modules/usb-ids/dist/ui directory does not exist, trying to use dist/ui instead')
149149
distDir = path.join(root, 'dist', 'ui')
150+
isProd = false
150151
}
151152

152153
// 检查dist/ui目录是否存在
@@ -183,7 +184,10 @@ async function startWebServer(port = 3000): Promise<void> {
183184
// 处理 USB 数据文件
184185
if (filePath.includes(USB_IDS_JSON_FILE) || filePath.includes(USB_IDS_VERSION_JSON_FILE)) {
185186
// USB 数据文件与 dist 同级目录
186-
filePath = path.join(root, req.url!.replace(UI_LOCAL_BASE_URL, ''))
187+
filePath
188+
= isProd
189+
? path.join(root, 'node_modules', 'usb.ids', req.url!.replace(UI_LOCAL_BASE_URL, ''))
190+
: path.join(root, req.url!.replace(UI_LOCAL_BASE_URL, ''))
187191
console.log('json file path', filePath)
188192
}
189193

0 commit comments

Comments
 (0)