feat(dev): dev:adb 多设备交互选择 + 记住上次设备
- 设备选择优先级:--serial= > 上次选择的设备(.dev-adb.local.json,不提交) > 单设备自动 > 多设备交互选择 - 非 TTY 环境(如某些任务脚本)给出 --serial 的明确提示,不再直接退出 - .gitignore 忽略 .dev-adb.local.json;README 补充设备选择说明
This commit is contained in:
parent
e93faeda0a
commit
6b3779ca6a
3
.gitignore
vendored
3
.gitignore
vendored
@ -21,6 +21,9 @@ yarn-error.log*
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# dev:adb 记住的目标设备(本地状态)
|
||||
.dev-adb.local.json
|
||||
|
||||
# VSCode
|
||||
.vscode/
|
||||
|
||||
|
||||
@ -117,6 +117,7 @@
|
||||
|
||||
- 注入方式:编译期命令行环境变量 `VUE_APP_API_BASE_OVERRIDE`(**不写入任何 .env 文件、不落盘**),在 `src/env.js` 中优先级最高。与工作台项目的 `VITE_API_BASE_OVERRIDE` 是同一套思路。
|
||||
- 测试包产物单独放 `release/android/dev/` 且文件名带 `_dev`,**不会覆盖发行包**,也不会触发发布上传(自动加 `-Dev -NoUpload`)。
|
||||
- 设备选择:命令行 `--serial=` > 上次选择的设备(记在 `.dev-adb.local.json`,不提交)> 只有一台时自动选中 > 多台时交互选择。
|
||||
- 两道校验方向相反:发行包**反向**校验(产物中不得出现 `192.168.x.x`/`127.0.0.1`);测试包**正向**校验(产物中必须出现注入的地址,否则中止,避免装成生产包却以为在测开发环境)。
|
||||
- 真机 JS 日志:**离线 release 包默认不会把 `console.log` 输出到 logcat**(HBuilderX「运行到手机」靠的是 debug 基座)。测试包由 `src/core/utils/dev-log.js` 用 `plus.android` 桥接 `android.util.Log`,把 console 转发到 logcat 的 `jsLog` tag(`dev:adb` 会自动注入开关并实时跟随打印):
|
||||
```powershell
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
const { spawn, spawnSync } = require('child_process')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const readline = require('readline')
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..')
|
||||
const APP_BUILD_DIR = path.join(ROOT, 'dist', 'build', 'app-plus')
|
||||
@ -126,6 +127,25 @@ function findDevApk() {
|
||||
}
|
||||
|
||||
// ---------- 设备 ----------
|
||||
// 上次选择的设备记在本文件里(不提交 git):多设备时自动选回它,避免每次都要选
|
||||
const STATE_FILE = path.join(ROOT, '.dev-adb.local.json')
|
||||
|
||||
function readSavedSerial() {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(STATE_FILE, 'utf-8')).serial || ''
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function saveSerial(serial) {
|
||||
try {
|
||||
fs.writeFileSync(STATE_FILE, `${JSON.stringify({ serial }, null, 2)}\n`)
|
||||
} catch (e) {
|
||||
// 写入失败不影响流程
|
||||
}
|
||||
}
|
||||
|
||||
function listDevices() {
|
||||
const res = spawnSync(ADB, ['devices', '-l'], { encoding: 'utf-8' })
|
||||
const devices = []
|
||||
@ -136,27 +156,71 @@ function listDevices() {
|
||||
return devices
|
||||
}
|
||||
|
||||
function pickDevice() {
|
||||
const wanted = argValue('--serial')
|
||||
function ask(question) {
|
||||
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
||||
return new Promise((resolve) => {
|
||||
rl.question(question, (answer) => {
|
||||
rl.close()
|
||||
resolve(String(answer || '').trim())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function printDevices(devices) {
|
||||
devices.forEach((d, i) => console.error(` [${i + 1}] ${d.serial} ${d.desc}`))
|
||||
}
|
||||
|
||||
async function pickDevice() {
|
||||
const devices = listDevices()
|
||||
if (!devices.length) {
|
||||
console.error('[dev:adb] 未检测到已连接设备(USB 未插好、未授权,或无线调试未 adb connect)')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// 1. 命令行指定优先
|
||||
const wanted = argValue('--serial')
|
||||
if (wanted) {
|
||||
const hit = devices.find((d) => d.serial === wanted)
|
||||
if (!hit) {
|
||||
console.error(`[dev:adb] 未找到设备 ${wanted},当前:${devices.map((d) => d.serial).join(', ')}`)
|
||||
process.exit(1)
|
||||
}
|
||||
saveSerial(hit.serial)
|
||||
return hit
|
||||
}
|
||||
if (devices.length > 1) {
|
||||
console.error('[dev:adb] 检测到多台设备,请用 --serial=<序列号> 指定:')
|
||||
devices.forEach((d) => console.error(` ${d.serial} ${d.desc}`))
|
||||
|
||||
// 2. 上次用过的设备仍然在线 → 直接用
|
||||
const saved = readSavedSerial()
|
||||
if (saved) {
|
||||
const hit = devices.find((d) => d.serial === saved)
|
||||
if (hit) {
|
||||
log(`使用上次选择的设备: ${hit.serial} ${hit.desc}(可用 --serial= 更换)`)
|
||||
return hit
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 只有一台 → 自动选中
|
||||
if (devices.length === 1) {
|
||||
saveSerial(devices[0].serial)
|
||||
return devices[0]
|
||||
}
|
||||
|
||||
// 4. 多台 → 交互选择(与工作台 dev:adb 一致)
|
||||
console.error('[dev:adb] 检测到多台设备:')
|
||||
printDevices(devices)
|
||||
if (!process.stdin.isTTY) {
|
||||
console.error('[dev:adb] 当前终端不能交互,请用 --serial=<序列号> 指定,例如:')
|
||||
console.error(` npm run dev:adb -- --serial=${devices[0].serial}`)
|
||||
process.exit(1)
|
||||
}
|
||||
return devices[0]
|
||||
const answer = await ask(`请选择设备序号 [1-${devices.length}],默认 1: `)
|
||||
const idx = answer ? Number(answer) - 1 : 0
|
||||
if (!Number.isInteger(idx) || idx < 0 || idx >= devices.length) {
|
||||
console.error(`[dev:adb] 无效序号: ${answer}`)
|
||||
process.exit(1)
|
||||
}
|
||||
saveSerial(devices[idx].serial)
|
||||
return devices[idx]
|
||||
}
|
||||
|
||||
// ---------- 真机日志跟随 ----------
|
||||
@ -339,10 +403,14 @@ function startWatch(device) {
|
||||
}
|
||||
|
||||
// ---------- 主流程 ----------
|
||||
const device = pickDevice()
|
||||
log(`目标设备: ${device.serial} ${device.desc}`)
|
||||
async function main() {
|
||||
const device = await pickDevice()
|
||||
log(`目标设备: ${device.serial} ${device.desc}`)
|
||||
|
||||
await triggerBuild(device, '首次')
|
||||
|
||||
log(`单独查看真机日志:adb -s ${device.serial} logcat jsLog:V UniConsole:V UniViewConsole:V *:S`)
|
||||
|
||||
triggerBuild(device, '首次').then(() => {
|
||||
if (ONCE) {
|
||||
if (NO_LOGCAT || NO_LAUNCH) {
|
||||
log('--once 指定,不进入监听,结束')
|
||||
@ -351,7 +419,8 @@ triggerBuild(device, '首次').then(() => {
|
||||
}
|
||||
return
|
||||
}
|
||||
startWatch(device)
|
||||
})
|
||||
|
||||
log(`单独查看真机日志:adb -s ${device.serial} logcat jsLog:V UniConsole:V UniViewConsole:V *:S`)
|
||||
startWatch(device)
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user