- scripts/dev-adb.cjs:编译期注入 VUE_APP_API_BASE_OVERRIDE(默认取 .env.development.local),打包到 release/android/dev 并可选装机启动 - pack.ps1 新增 -Dev:产物 _dev 后缀 + 单独目录,不覆盖发行包、不触发发布上传 - package.json 新增 dev:adb / build:app-plus:dev - README 补充两种模式的接口地址来源、正反向校验与真机日志查看方式
206 lines
8.0 KiB
JavaScript
206 lines
8.0 KiB
JavaScript
/**
|
||
* 真机调试包(测试模式):编译时注入指定接口地址 → 打包 → 安装到设备 → 启动。
|
||
*
|
||
* 与 `npm run build:apk` 的区别:
|
||
* - build:apk → 发行包。接口地址固定为 src/env.js 的 PROD_API_BASE,构建后强制校验产物不含内网地址。
|
||
* - dev:adb → 测试包。接口地址由本脚本在编译期注入(默认取 .env.development.local 的
|
||
* VUE_APP_API_BASE_URL),产物输出到 release/android/dev/*_dev.apk,绝不覆盖发行包。
|
||
*
|
||
* 注入方式(与工作台 wms_worker_station 一致):命令行环境变量 VUE_APP_API_BASE_OVERRIDE,
|
||
* 不写入任何 .env 文件、不落盘;在 src/env.js 中优先级高于生产常量,详见该文件顶部说明。
|
||
*
|
||
* 用法:
|
||
* npm run dev:adb # 默认注入 .env.development.local 的地址
|
||
* npm run dev:adb -- --prod-api # 不注入,产出与发行包一致的包(排查线上问题时用)
|
||
* npm run dev:adb -- --api=http://192.168.1.10:6060
|
||
* npm run dev:adb -- --no-install # 只编译 + 打包,不装机
|
||
* npm run dev:adb -- --no-launch # 装机后不自动启动
|
||
* npm run dev:adb -- --serial=4235737 # 多设备时指定目标设备
|
||
*/
|
||
const { spawnSync } = require('child_process')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const ROOT = path.resolve(__dirname, '..')
|
||
const APP_BUILD_DIR = path.join(ROOT, 'dist', 'build', 'app-plus')
|
||
const DEV_APK_DIR = path.join(ROOT, 'release', 'android', 'dev')
|
||
const PACKAGE_NAME = 'com.gztbyc.pda_wms'
|
||
const LAUNCH_COMPONENT = `${PACKAGE_NAME}/io.dcloud.PandoraEntry`
|
||
|
||
const log = (msg) => console.log(`[dev:adb] ${msg}`)
|
||
const argv = process.argv.slice(2)
|
||
const hasFlag = (name) => argv.includes(name)
|
||
const argValue = (name) => {
|
||
const hit = argv.find((a) => a.startsWith(`${name}=`))
|
||
return hit ? hit.slice(name.length + 1) : ''
|
||
}
|
||
|
||
// ---------- adb 探测 ----------
|
||
function detectAdb() {
|
||
const candidates = []
|
||
const home = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT || ''
|
||
if (home) candidates.push(path.join(home, 'platform-tools', process.platform === 'win32' ? 'adb.exe' : 'adb'))
|
||
if (process.env.LOCALAPPDATA) candidates.push(path.join(process.env.LOCALAPPDATA, 'Android', 'Sdk', 'platform-tools', 'adb.exe'))
|
||
candidates.push(process.platform === 'win32' ? 'adb.exe' : 'adb')
|
||
for (const c of candidates) {
|
||
const res = spawnSync(c, ['version'], { encoding: 'utf-8' })
|
||
if (res.status === 0 && /Android Debug Bridge/.test(res.stdout || '')) return c
|
||
}
|
||
return 'adb'
|
||
}
|
||
const ADB = detectAdb()
|
||
|
||
// ---------- 接口地址解析 ----------
|
||
function readEnvValue(file, key) {
|
||
try {
|
||
const text = fs.readFileSync(path.join(ROOT, file), 'utf-8')
|
||
for (const line of text.split(/\r?\n/)) {
|
||
const m = line.match(/^\s*([A-Za-z0-9_]+)\s*=\s*(.*)$/)
|
||
if (m && m[1] === key) return m[2].trim().replace(/^["']|["']$/g, '')
|
||
}
|
||
} catch (e) {
|
||
// 文件不存在视为未配置
|
||
}
|
||
return ''
|
||
}
|
||
|
||
function resolveApiTarget() {
|
||
if (hasFlag('--prod-api')) return { base: '', note: '--prod-api 指定,与发行包一致(生产地址)' }
|
||
const argApi = argValue('--api')
|
||
if (argApi) return { base: argApi, note: '命令行 --api 指定' }
|
||
const fromEnv = readEnvValue('.env.development.local', 'VUE_APP_API_BASE_URL') || readEnvValue('.env.local', 'VUE_APP_API_BASE_URL')
|
||
if (fromEnv) return { base: fromEnv, note: '.env.development.local 的 VUE_APP_API_BASE_URL' }
|
||
return { base: '', note: '未配置开发地址,沿用构建默认(生产地址)' }
|
||
}
|
||
|
||
// ---------- 工具 ----------
|
||
function run(cmd, args, env) {
|
||
const res = spawnSync(cmd, args, {
|
||
cwd: ROOT,
|
||
stdio: 'inherit',
|
||
env: { ...process.env, ...(env || {}) },
|
||
shell: process.platform === 'win32',
|
||
})
|
||
if (res.error) {
|
||
console.error(`[dev:adb] 执行失败 ${cmd}: ${res.error.message}`)
|
||
return false
|
||
}
|
||
return res.status === 0
|
||
}
|
||
|
||
function collectFiles(dir, out = []) {
|
||
if (!fs.existsSync(dir)) return out
|
||
for (const name of fs.readdirSync(dir)) {
|
||
const full = path.join(dir, name)
|
||
if (fs.statSync(full).isDirectory()) collectFiles(full, out)
|
||
else if (/\.(js|html|json)$/i.test(name)) out.push(full)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// ---------- 设备 ----------
|
||
function listDevices() {
|
||
const res = spawnSync(ADB, ['devices', '-l'], { encoding: 'utf-8' })
|
||
const devices = []
|
||
for (const line of (res.stdout || '').split(/\r?\n/)) {
|
||
const m = line.trim().match(/^(\S+)\s+device\b(.*)$/)
|
||
if (m) devices.push({ serial: m[1], desc: m[2].trim() })
|
||
}
|
||
return devices
|
||
}
|
||
|
||
function pickDevice() {
|
||
const wanted = argValue('--serial') || argValue('-s')
|
||
const devices = listDevices()
|
||
if (!devices.length) {
|
||
console.error('[dev:adb] 未检测到已连接设备(USB 未插好、未授权,或无线调试未 adb connect)')
|
||
process.exit(1)
|
||
}
|
||
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)
|
||
}
|
||
return hit
|
||
}
|
||
if (devices.length > 1) {
|
||
console.error('[dev:adb] 检测到多台设备,请用 --serial=<序列号> 指定:')
|
||
devices.forEach((d) => console.error(` ${d.serial} ${d.desc}`))
|
||
process.exit(1)
|
||
}
|
||
log(`目标设备: ${devices[0].serial} ${devices[0].desc}`)
|
||
return devices[0]
|
||
}
|
||
|
||
// ---------- 主流程 ----------
|
||
const apiTarget = resolveApiTarget()
|
||
const device = pickDevice()
|
||
|
||
log(`接口地址 = ${apiTarget.base || 'src/env.js 的 PROD_API_BASE(生产)'} 〔${apiTarget.note}〕`)
|
||
|
||
// 1. 编译(生产模式编译,但注入 override 指向测试环境)
|
||
log('step 1/4: 编译 App 资源 ...')
|
||
const buildEnv = apiTarget.base ? { VUE_APP_API_BASE_OVERRIDE: apiTarget.base } : {}
|
||
if (!run('npm', ['run', 'build:app-plus:dev'], buildEnv)) {
|
||
console.error('[dev:adb] ✗ 编译失败')
|
||
process.exit(1)
|
||
}
|
||
|
||
// 2. 校验注入结果(正向校验:期望地址必须出现在产物里)
|
||
log('step 2/4: 校验接口地址注入 ...')
|
||
const files = collectFiles(APP_BUILD_DIR)
|
||
if (!files.length) {
|
||
console.error(`[dev:adb] ✗ 未找到构建产物: ${APP_BUILD_DIR}`)
|
||
process.exit(1)
|
||
}
|
||
if (apiTarget.base) {
|
||
const host = apiTarget.base.replace(/^https?:\/\//, '').replace(/\/.*$/, '')
|
||
const hit = files.find((f) => fs.readFileSync(f, 'utf-8').includes(host))
|
||
if (!hit) {
|
||
console.error(`[dev:adb] ✗ 产物中未找到注入的地址 ${apiTarget.base},注入可能未生效,已中止`)
|
||
console.error(' 排查:src/env.js 是否仍以 VUE_APP_API_BASE_OVERRIDE 为最高优先级')
|
||
process.exit(1)
|
||
}
|
||
log(`✓ 已确认产物使用 ${apiTarget.base}`)
|
||
} else {
|
||
if (!run('node', ['scripts/check-build-env.cjs', 'dist/build/app-plus'])) {
|
||
console.error('[dev:adb] ✗ 产物地址校验未通过')
|
||
process.exit(1)
|
||
}
|
||
}
|
||
|
||
// 3. 打包(-Dev:产物进 release/android/dev 且带 _dev 后缀,不触发发布上传)
|
||
log('step 3/4: 离线打包 ...')
|
||
if (!run('npm', ['run', 'android:pack:raw', '--', '-Dev', '-NoUpload'])) {
|
||
console.error('[dev:adb] ✗ 打包失败')
|
||
process.exit(1)
|
||
}
|
||
const apks = fs.existsSync(DEV_APK_DIR)
|
||
? fs.readdirSync(DEV_APK_DIR).filter((f) => f.endsWith('.apk')).map((f) => ({ f, t: fs.statSync(path.join(DEV_APK_DIR, f)).mtimeMs }))
|
||
: []
|
||
apks.sort((a, b) => b.t - a.t)
|
||
if (!apks.length) {
|
||
console.error(`[dev:adb] ✗ 未找到 APK: ${DEV_APK_DIR}`)
|
||
process.exit(1)
|
||
}
|
||
const apkPath = path.join(DEV_APK_DIR, apks[0].f)
|
||
log(`✓ 测试包: ${path.relative(ROOT, apkPath)}`)
|
||
|
||
if (hasFlag('--no-install')) {
|
||
log('--no-install 指定,结束(未安装)')
|
||
process.exit(0)
|
||
}
|
||
|
||
// 4. 安装 + 启动
|
||
log('step 4/4: 安装并启动 ...')
|
||
if (!run(ADB, ['-s', device.serial, 'install', '-r', apkPath])) {
|
||
console.error('[dev:adb] ✗ 安装失败')
|
||
process.exit(1)
|
||
}
|
||
if (!hasFlag('--no-launch')) {
|
||
run(ADB, ['-s', device.serial, 'shell', 'am', 'start', '-n', LAUNCH_COMPONENT])
|
||
}
|
||
log('完成。查看应用日志(release 包里 JS console 走 UniConsole/UniViewConsole 两个 tag):')
|
||
log(` & "$env:ANDROID_HOME\\platform-tools\\adb.exe" -s ${device.serial} logcat UniConsole:V UniViewConsole:V *:S`)
|