uniapp-android-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +595 -2
- package/bin/uni-pack.js +7 -0
- package/package.json +66 -4
- package/src/cli.js +184 -0
- package/src/commands/build.js +42 -0
- package/src/commands/debug.js +83 -0
- package/src/commands/device.js +303 -0
- package/src/commands/devices.js +23 -0
- package/src/commands/doctor.js +160 -0
- package/src/commands/init.js +277 -0
- package/src/commands/wgt.js +46 -0
- package/src/core/adb.js +234 -0
- package/src/core/build.js +374 -0
- package/src/core/config.js +157 -0
- package/src/core/devicePairing.js +152 -0
- package/src/core/errors.js +36 -0
- package/src/core/fsx.js +172 -0
- package/src/core/gradle.js +75 -0
- package/src/core/hbuilderx.js +258 -0
- package/src/core/logger.js +82 -0
- package/src/core/output.js +74 -0
- package/src/core/patch/assets.js +154 -0
- package/src/core/patch/gradle.js +128 -0
- package/src/core/patch/manifest.js +81 -0
- package/src/core/patch/nativePlugins.js +137 -0
- package/src/core/patch/projectManifest.js +66 -0
- package/src/core/patch/template.js +57 -0
- package/src/core/patch/utsPlugins.js +424 -0
- package/src/core/process.js +71 -0
- package/src/core/project.js +250 -0
- package/src/core/qrcode.js +13 -0
- package/src/core/sdk.js +398 -0
- package/src/core/signing.js +84 -0
- package/src/core/version.js +128 -0
- package/src/core/webBuild.js +69 -0
- package/src/core/wgt.js +76 -0
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uniapp-android-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "离线打包 uni-app Android 应用:正式包、自定义基座、wgt、设备调试",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"uni-app",
|
|
8
|
+
"uniapp",
|
|
9
|
+
"android",
|
|
10
|
+
"apk",
|
|
11
|
+
"offline",
|
|
12
|
+
"dcloud",
|
|
13
|
+
"hbuilderx",
|
|
14
|
+
"wgt",
|
|
15
|
+
"cli"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "liudejin <liudejin@kis2.cn>",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/liu-dejin/uniapp-android-cli.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/liu-dejin/uniapp-android-cli/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/liu-dejin/uniapp-android-cli#readme",
|
|
27
|
+
"bin": {
|
|
28
|
+
"uni-pack": "bin/uni-pack.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"bin",
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
36
|
+
},
|
|
37
|
+
"packageManager": "npm@11.21.1",
|
|
38
|
+
"devEngines": {
|
|
39
|
+
"runtime": {
|
|
40
|
+
"name": "node",
|
|
41
|
+
"version": "^20.19.0 || >=22.12.0",
|
|
42
|
+
"onFail": "error"
|
|
43
|
+
},
|
|
44
|
+
"packageManager": {
|
|
45
|
+
"name": "npm",
|
|
46
|
+
"version": ">=11.15.0",
|
|
47
|
+
"onFail": "warn"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "node --test",
|
|
56
|
+
"link": "npm link",
|
|
57
|
+
"pack:cli": "npm pack",
|
|
58
|
+
"prepublishOnly": "npm run typecheck && npm test"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"qrcode-terminal": "^0.12.0",
|
|
62
|
+
"yazl": "^3.3.1"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@types/node": "^22.10.0",
|
|
66
|
+
"typescript": "^5.7.0"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { parseArgs } from 'node:util'
|
|
3
|
+
import { PackError, toFailure } from './core/errors.js'
|
|
4
|
+
|
|
5
|
+
const VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version
|
|
6
|
+
|
|
7
|
+
const COMMON = {
|
|
8
|
+
project: { type: 'string', short: 'p' },
|
|
9
|
+
verbose: { type: 'boolean' },
|
|
10
|
+
json: { type: 'boolean' },
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const SPECS = {
|
|
14
|
+
init: {
|
|
15
|
+
...COMMON,
|
|
16
|
+
appkey: { type: 'string' },
|
|
17
|
+
keystore: { type: 'string' },
|
|
18
|
+
'key-alias': { type: 'string' },
|
|
19
|
+
package: { type: 'string' },
|
|
20
|
+
sdk: { type: 'string' },
|
|
21
|
+
hbuilderx: { type: 'string' },
|
|
22
|
+
'android-sdk': { type: 'string' },
|
|
23
|
+
jdk: { type: 'string' },
|
|
24
|
+
},
|
|
25
|
+
doctor: { ...COMMON },
|
|
26
|
+
devices: { ...COMMON },
|
|
27
|
+
device: {
|
|
28
|
+
...COMMON,
|
|
29
|
+
timeout: { type: 'string' },
|
|
30
|
+
service: { type: 'string' },
|
|
31
|
+
code: { type: 'string' },
|
|
32
|
+
port: { type: 'string' },
|
|
33
|
+
device: { type: 'string' },
|
|
34
|
+
connect: { type: 'boolean' },
|
|
35
|
+
},
|
|
36
|
+
build: {
|
|
37
|
+
...COMMON,
|
|
38
|
+
mode: { type: 'string' },
|
|
39
|
+
'skip-web': { type: 'boolean' },
|
|
40
|
+
out: { type: 'string' },
|
|
41
|
+
install: { type: 'boolean' },
|
|
42
|
+
device: { type: 'string' },
|
|
43
|
+
offline: { type: 'boolean' },
|
|
44
|
+
'keep-workdir': { type: 'boolean' },
|
|
45
|
+
'skip-version-check': { type: 'boolean' },
|
|
46
|
+
'store-password': { type: 'string' },
|
|
47
|
+
'key-password': { type: 'string' },
|
|
48
|
+
},
|
|
49
|
+
wgt: { ...COMMON, 'skip-web': { type: 'boolean' }, out: { type: 'string' } },
|
|
50
|
+
debug: {
|
|
51
|
+
...COMMON,
|
|
52
|
+
install: { type: 'boolean' },
|
|
53
|
+
push: { type: 'boolean' },
|
|
54
|
+
logs: { type: 'boolean' },
|
|
55
|
+
device: { type: 'string' },
|
|
56
|
+
base: { type: 'string' },
|
|
57
|
+
build: { type: 'boolean' },
|
|
58
|
+
'skip-web': { type: 'boolean' },
|
|
59
|
+
'store-password': { type: 'string' },
|
|
60
|
+
'key-password': { type: 'string' },
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string[]} argv
|
|
66
|
+
*/
|
|
67
|
+
export async function main(argv) {
|
|
68
|
+
const [command, ...rest] = argv
|
|
69
|
+
if (command === 'version' || command === '--version' || command === '-v') {
|
|
70
|
+
console.log(VERSION)
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
if (!command || command === 'help' || command === '--help' || command === '-h') {
|
|
74
|
+
printHelp()
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
const spec = SPECS[command]
|
|
78
|
+
if (!spec) {
|
|
79
|
+
console.error(`未知命令:${command}`)
|
|
80
|
+
printHelp()
|
|
81
|
+
process.exitCode = 2
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let options
|
|
86
|
+
try {
|
|
87
|
+
const { values, positionals } = parseArgs({ args: rest, options: spec, allowPositionals: true })
|
|
88
|
+
options = normalize(values)
|
|
89
|
+
options.positionals = positionals
|
|
90
|
+
options.cwd = process.cwd()
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error(error instanceof Error ? error.message : String(error))
|
|
93
|
+
process.exitCode = 2
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
if (command === 'init') {
|
|
99
|
+
const { initCommand } = await import('./commands/init.js')
|
|
100
|
+
await initCommand(options)
|
|
101
|
+
} else if (command === 'doctor') {
|
|
102
|
+
const { doctorCommand } = await import('./commands/doctor.js')
|
|
103
|
+
await doctorCommand(options)
|
|
104
|
+
} else if (command === 'devices') {
|
|
105
|
+
const { devicesCommand } = await import('./commands/devices.js')
|
|
106
|
+
await devicesCommand(options)
|
|
107
|
+
} else if (command === 'device') {
|
|
108
|
+
const { deviceCommand } = await import('./commands/device.js')
|
|
109
|
+
await deviceCommand(options)
|
|
110
|
+
} else if (command === 'build') {
|
|
111
|
+
const { buildCommand } = await import('./commands/build.js')
|
|
112
|
+
await buildCommand(options)
|
|
113
|
+
} else if (command === 'wgt') {
|
|
114
|
+
const { wgtCommand } = await import('./commands/wgt.js')
|
|
115
|
+
await wgtCommand(options)
|
|
116
|
+
} else if (command === 'debug') {
|
|
117
|
+
const { debugCommand } = await import('./commands/debug.js')
|
|
118
|
+
await debugCommand(options)
|
|
119
|
+
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
const failure = toFailure(error)
|
|
122
|
+
if (options.json) {
|
|
123
|
+
console.log(JSON.stringify({ ok: false, code: failure.code, message: failure.message, hint: failure.hint }, null, 2))
|
|
124
|
+
} else {
|
|
125
|
+
console.error(`\n✗ ${failure.message}`)
|
|
126
|
+
if (failure.hint) {
|
|
127
|
+
console.error(` ${failure.hint}`)
|
|
128
|
+
}
|
|
129
|
+
if (error instanceof PackError && error.cause instanceof Error) {
|
|
130
|
+
console.error(` 原因:${error.cause.message}`)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
process.exitCode = failure.code
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {Record<string, unknown>} values
|
|
139
|
+
*/
|
|
140
|
+
function normalize(values) {
|
|
141
|
+
/** @type {Record<string, unknown>} */
|
|
142
|
+
const result = {}
|
|
143
|
+
for (const [key, value] of Object.entries(values)) {
|
|
144
|
+
result[key.replace(/-([a-z])/g, (_, char) => char.toUpperCase())] = value
|
|
145
|
+
}
|
|
146
|
+
return result
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function printHelp() {
|
|
150
|
+
console.log(`uni-pack ${VERSION} — uni-app Android 离线打包
|
|
151
|
+
|
|
152
|
+
用法:
|
|
153
|
+
uni-pack init [-p <dir>] [--appkey <key>] [--package <包名>] [--sdk <dir>] [--hbuilderx <dir>]
|
|
154
|
+
[--android-sdk <dir>] [--jdk <dir>]
|
|
155
|
+
uni-pack doctor [-p <dir>] [--json] 环境自检
|
|
156
|
+
uni-pack build [-p <dir>] [--mode release|debug] [--install] [--device <serial>]
|
|
157
|
+
[--skip-web] [--out <dir>] [--offline]
|
|
158
|
+
uni-pack wgt [-p <dir>] [--skip-web] [--out <dir>]
|
|
159
|
+
uni-pack debug [-p <dir>] [--push] [--install] [--build] [--logs] [--device <serial>]
|
|
160
|
+
uni-pack devices 列出设备(同 device list)
|
|
161
|
+
uni-pack device [list] 列出 USB / 无线设备
|
|
162
|
+
uni-pack device pair [--timeout 120] [--service <名>] [--code <6位>]
|
|
163
|
+
终端显示二维码,手机扫码配对(Android 11+)
|
|
164
|
+
uni-pack device pair <ip:port> <配对码> 手动配对(手机显示配对码时用)
|
|
165
|
+
uni-pack device connect <ip[:port]> 连接无线设备(默认端口 5555)
|
|
166
|
+
uni-pack device disconnect [ip[:port]] 断开无线设备
|
|
167
|
+
uni-pack device tcpip [--port 5555] [--connect] USB 设备开启无线调试
|
|
168
|
+
uni-pack device mdns 列出 mDNS 发现的无线调试服务
|
|
169
|
+
uni-pack version 显示版本
|
|
170
|
+
|
|
171
|
+
支持两种项目:
|
|
172
|
+
CLI 项目 src/manifest.json,按项目脚本选 build:app-android / build:app-plus
|
|
173
|
+
HBuilderX 项目 根目录 manifest.json,用 HBuilderX 自带编译器(Vue 2 / Vue 3)
|
|
174
|
+
|
|
175
|
+
环境变量:
|
|
176
|
+
UNI_SDK_ROOT 含 HBuilder-Integrate-AS 的离线 SDK 目录
|
|
177
|
+
UNI_SDK_DIRS 额外扫描 Android-SDK@x.yy 的目录(多个用 ; 分隔)
|
|
178
|
+
HBUILDERX_HOME HBuilderX 安装目录
|
|
179
|
+
ANDROID_HOME Android SDK 根目录
|
|
180
|
+
JAVA_HOME JDK 17+ 根目录
|
|
181
|
+
|
|
182
|
+
配置:~/.uniapp-android-cli/config.json(项目内 uni-pack.config.json 可覆盖项目级字段)
|
|
183
|
+
`)
|
|
184
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { buildProject } from '../core/build.js'
|
|
3
|
+
import { installApk } from '../core/adb.js'
|
|
4
|
+
import { loadConfig, resolveProject } from '../core/config.js'
|
|
5
|
+
import { Logger } from '../core/logger.js'
|
|
6
|
+
import { resolveAndroidTools } from '../core/sdk.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {Record<string, any>} options
|
|
10
|
+
*/
|
|
11
|
+
export async function buildCommand(options) {
|
|
12
|
+
const logger = new Logger(options)
|
|
13
|
+
const config = await loadConfig()
|
|
14
|
+
const { project } = await resolveProject(config, options.project, options.cwd)
|
|
15
|
+
|
|
16
|
+
const result = await buildProject({
|
|
17
|
+
config,
|
|
18
|
+
project,
|
|
19
|
+
mode: options.mode === 'debug' ? 'debug' : 'release',
|
|
20
|
+
options,
|
|
21
|
+
logger,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
if (options.install) {
|
|
25
|
+
const tools = await resolveAndroidTools(config)
|
|
26
|
+
const { listDevices, pickDevice } = await import('../core/adb.js')
|
|
27
|
+
const devices = await listDevices(tools.adb)
|
|
28
|
+
const device = pickDevice(devices, options.device)
|
|
29
|
+
logger.step(`安装到 ${device.model || device.serial}`)
|
|
30
|
+
await installApk(tools.adb, result.apk, device.serial)
|
|
31
|
+
logger.success('安装完成')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
logger.output('build', {
|
|
35
|
+
ok: true,
|
|
36
|
+
apk: result.apk,
|
|
37
|
+
latest: result.latest,
|
|
38
|
+
sha256: result.sha256,
|
|
39
|
+
workDir: path.join(result.workRoot, 'gradle'),
|
|
40
|
+
})
|
|
41
|
+
return result
|
|
42
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { spawn } from 'node:child_process'
|
|
3
|
+
import { buildProject } from '../core/build.js'
|
|
4
|
+
import { installApk, listDevices, pickDevice, startApp } from '../core/adb.js'
|
|
5
|
+
import { loadConfig, resolveProject } from '../core/config.js'
|
|
6
|
+
import { exists } from '../core/fsx.js'
|
|
7
|
+
import { Logger } from '../core/logger.js'
|
|
8
|
+
import { apkBaseName } from '../core/output.js'
|
|
9
|
+
import { normalizeManifest, readProjectManifest } from '../core/project.js'
|
|
10
|
+
import { resolveAndroidTools } from '../core/sdk.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {Record<string, any>} options
|
|
14
|
+
*/
|
|
15
|
+
export async function debugCommand(options) {
|
|
16
|
+
const logger = new Logger(options)
|
|
17
|
+
const config = await loadConfig()
|
|
18
|
+
const { project } = await resolveProject(config, options.project, options.cwd)
|
|
19
|
+
const tools = await resolveAndroidTools(config)
|
|
20
|
+
|
|
21
|
+
const devices = await listDevices(tools.adb)
|
|
22
|
+
const device = pickDevice(devices, options.device)
|
|
23
|
+
const activity = String(project.debug?.launchActivity ?? 'io.dcloud.PandoraEntry')
|
|
24
|
+
|
|
25
|
+
const { manifest } = await readProjectManifest(project.projectDir)
|
|
26
|
+
const resolved = normalizeManifest(manifest, {
|
|
27
|
+
appid: project.appid,
|
|
28
|
+
name: project.name,
|
|
29
|
+
package: project.package,
|
|
30
|
+
versionName: project.versionName,
|
|
31
|
+
versionCode: project.versionCode,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
let baseApk = options.base ? path.resolve(options.base) : ''
|
|
35
|
+
if (!baseApk) {
|
|
36
|
+
const baseName = apkBaseName(project.name ?? resolved.name, resolved.versionName, resolved.versionCode)
|
|
37
|
+
const candidate = path.join(path.resolve(project.outputDir), `${baseName}_debug_latest.apk`)
|
|
38
|
+
baseApk = (await exists(candidate)) ? candidate : ''
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// --push 表示「带上最新前端资源重建基座」,等价于 build + install;
|
|
42
|
+
// SDK 的 wgt 安装会让基座崩溃,这里不走 plus.runtime.install。
|
|
43
|
+
const rebuild = Boolean(options.push) || Boolean(options.build) || !baseApk
|
|
44
|
+
if (rebuild) {
|
|
45
|
+
logger.step('构建自定义基座')
|
|
46
|
+
const built = await buildProject({
|
|
47
|
+
config,
|
|
48
|
+
project,
|
|
49
|
+
mode: 'debug',
|
|
50
|
+
options: { ...options, install: false },
|
|
51
|
+
logger,
|
|
52
|
+
})
|
|
53
|
+
baseApk = built.apk
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (options.install || options.push || rebuild) {
|
|
57
|
+
logger.step(`安装到 ${device.model || device.serial}`)
|
|
58
|
+
await installApk(tools.adb, baseApk, device.serial)
|
|
59
|
+
await startApp(tools.adb, resolved.packageName, activity, device.serial)
|
|
60
|
+
logger.success('基座已安装并启动')
|
|
61
|
+
} else {
|
|
62
|
+
logger.info(`基座:${baseApk}`)
|
|
63
|
+
logger.info('重新构建并安装:uni-pack debug --push;只看日志:--logs')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (options.logs) {
|
|
67
|
+
await streamLogs(tools.adb, device.serial)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {string} adb
|
|
73
|
+
* @param {string} serial
|
|
74
|
+
*/
|
|
75
|
+
async function streamLogs(adb, serial) {
|
|
76
|
+
const child = spawn(adb, ['-s', serial, 'logcat', '-v', 'time', '-s', 'console:V', 'uni-app:V', 'AndroidRuntime:E'], {
|
|
77
|
+
stdio: 'inherit',
|
|
78
|
+
windowsHide: true,
|
|
79
|
+
})
|
|
80
|
+
const stop = () => child.kill()
|
|
81
|
+
process.once('SIGINT', stop)
|
|
82
|
+
await new Promise((resolve) => child.on('close', resolve))
|
|
83
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import {
|
|
2
|
+
connectDevice,
|
|
3
|
+
deviceWifiIp,
|
|
4
|
+
disconnectDevice,
|
|
5
|
+
enableTcpip,
|
|
6
|
+
listDevices,
|
|
7
|
+
listMdnsServices,
|
|
8
|
+
mdnsSupported,
|
|
9
|
+
pairDevice,
|
|
10
|
+
pickDevice,
|
|
11
|
+
waitForDevice,
|
|
12
|
+
} from '../core/adb.js'
|
|
13
|
+
import { loadConfig } from '../core/config.js'
|
|
14
|
+
import {
|
|
15
|
+
hostOf,
|
|
16
|
+
normalizeAddress,
|
|
17
|
+
normalizePairingAddress,
|
|
18
|
+
pairingPayload,
|
|
19
|
+
selectPairingService,
|
|
20
|
+
validatePairingCode,
|
|
21
|
+
validateServiceName,
|
|
22
|
+
} from '../core/devicePairing.js'
|
|
23
|
+
import { PackError, EXIT } from '../core/errors.js'
|
|
24
|
+
import { Logger } from '../core/logger.js'
|
|
25
|
+
import { renderQr } from '../core/qrcode.js'
|
|
26
|
+
import { resolveAdbPath } from '../core/sdk.js'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {Record<string, any>} options
|
|
30
|
+
*/
|
|
31
|
+
export async function deviceCommand(options) {
|
|
32
|
+
const logger = new Logger(options)
|
|
33
|
+
const config = await loadConfig()
|
|
34
|
+
const adb = await resolveAdbPath(config)
|
|
35
|
+
const [action = 'list', ...rest] = options.positionals ?? []
|
|
36
|
+
|
|
37
|
+
if (action === 'list') {
|
|
38
|
+
await list(adb, logger)
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
if (action === 'pair') {
|
|
42
|
+
if (rest.length >= 2) {
|
|
43
|
+
await pairByCode(adb, rest[0], rest[1], logger)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
await pairByQr(adb, options, logger)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
if (action === 'connect') {
|
|
50
|
+
await connect(adb, rest[0], logger)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
if (action === 'disconnect') {
|
|
54
|
+
await disconnect(adb, rest[0], logger)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
if (action === 'tcpip') {
|
|
58
|
+
await tcpip(adb, options, logger)
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
if (action === 'mdns') {
|
|
62
|
+
await mdns(adb, logger)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
throw new PackError(`未知的 device 子命令:${action}`, EXIT.CONFIG, {
|
|
66
|
+
hint: '可用:list / pair / connect / disconnect / tcpip / mdns',
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {string} adb
|
|
72
|
+
* @param {Logger} logger
|
|
73
|
+
*/
|
|
74
|
+
async function list(adb, logger) {
|
|
75
|
+
const devices = await listDevices(adb)
|
|
76
|
+
if (devices.length === 0) {
|
|
77
|
+
logger.warn('没有发现设备。USB 连接请确认已开启 USB 调试;无线连接用 uni-pack device pair')
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
for (const device of devices) {
|
|
81
|
+
const kind = device.serial.includes(':') ? '无线' : 'USB'
|
|
82
|
+
logger.info(`${device.serial} ${device.state}${device.model ? ` ${device.model}` : ''} [${kind}]`)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {string} adb
|
|
88
|
+
* @param {Record<string, any>} options
|
|
89
|
+
* @param {Logger} logger
|
|
90
|
+
*/
|
|
91
|
+
async function pairByQr(adb, options, logger) {
|
|
92
|
+
if (!(await mdnsSupported(adb))) {
|
|
93
|
+
throw new PackError('当前 adb 不支持 mDNS 发现,无法使用二维码配对', EXIT.DEVICE, {
|
|
94
|
+
hint: '升级 Android SDK platform-tools(adb mdns check 应返回 mdns daemon 版本)',
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const service = validateServiceName(options.service)
|
|
99
|
+
const code = validatePairingCode(options.code)
|
|
100
|
+
const timeout = clamp(Number(options.timeout) || 120, 15, 600)
|
|
101
|
+
const payload = pairingPayload(service, code)
|
|
102
|
+
|
|
103
|
+
logger.step('手机扫码配对')
|
|
104
|
+
process.stdout.write(`${renderQr(payload)}\n`)
|
|
105
|
+
logger.info('手机:设置 → 开发者选项 → 无线调试 → 使用二维码配对设备')
|
|
106
|
+
logger.info(`服务名:${service} 配对码:${code}`)
|
|
107
|
+
logger.info(`等待手机注册配对服务(最多 ${timeout} 秒)…`)
|
|
108
|
+
|
|
109
|
+
const before = await listMdnsServices(adb)
|
|
110
|
+
const pairing = await waitForPairingService(adb, service, before, timeout)
|
|
111
|
+
if (!pairing) {
|
|
112
|
+
throw new PackError(`等待超时:没有发现配对服务 ${service}`, EXIT.DEVICE, {
|
|
113
|
+
hint: '确认手机与电脑在同一 Wi-Fi,且已选择“使用二维码配对设备”;关闭 VPN/代理后重试',
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
logger.step(`发现配对服务:${pairing.address}`)
|
|
118
|
+
await pairDevice(adb, pairing.address, code)
|
|
119
|
+
logger.success('配对成功')
|
|
120
|
+
|
|
121
|
+
const connectService = await waitForConnectService(adb, pairing.host, before, 20)
|
|
122
|
+
if (!connectService) {
|
|
123
|
+
logger.info('暂未发现连接服务,可稍后在手机上查看 IP 并执行:uni-pack device connect <ip:port>')
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
await connectDevice(adb, connectService.address)
|
|
127
|
+
const device = await waitForDevice(adb, connectService.address, 10000)
|
|
128
|
+
if (device) {
|
|
129
|
+
logger.success(`已连接:${device.serial}${device.model ? `(${device.model})` : ''}`)
|
|
130
|
+
} else {
|
|
131
|
+
logger.warn(`已发起连接,但设备状态未就绪:${connectService.address}`)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {string} adb
|
|
137
|
+
* @param {string} addressValue
|
|
138
|
+
* @param {string} codeValue
|
|
139
|
+
* @param {Logger} logger
|
|
140
|
+
*/
|
|
141
|
+
async function pairByCode(adb, addressValue, codeValue, logger) {
|
|
142
|
+
const address = normalizePairingAddress(addressValue)
|
|
143
|
+
const code = validatePairingCode(codeValue)
|
|
144
|
+
const before = await listMdnsServices(adb)
|
|
145
|
+
logger.step(`配对 ${address}`)
|
|
146
|
+
await pairDevice(adb, address, code)
|
|
147
|
+
logger.success('配对成功')
|
|
148
|
+
|
|
149
|
+
const host = hostOf(address)
|
|
150
|
+
const connectService = await waitForConnectService(adb, host, before, 10)
|
|
151
|
+
if (!connectService) {
|
|
152
|
+
logger.info(`连接:uni-pack device connect ${host}:5555`)
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
await connect(adb, connectService.address, logger)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {string} adb
|
|
160
|
+
* @param {string} service
|
|
161
|
+
* @param {Array<{ address: string }>} before
|
|
162
|
+
* @param {number} timeoutSeconds
|
|
163
|
+
*/
|
|
164
|
+
async function waitForPairingService(adb, service, before, timeoutSeconds) {
|
|
165
|
+
const previous = new Set(before.map((item) => item.address))
|
|
166
|
+
const deadline = Date.now() + timeoutSeconds * 1000
|
|
167
|
+
while (Date.now() < deadline) {
|
|
168
|
+
const services = await listMdnsServices(adb)
|
|
169
|
+
const match = selectPairingService(services, previous, service)
|
|
170
|
+
if (match) {
|
|
171
|
+
return match
|
|
172
|
+
}
|
|
173
|
+
await sleep(1000)
|
|
174
|
+
}
|
|
175
|
+
return null
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param {string} adb
|
|
180
|
+
* @param {string} host
|
|
181
|
+
* @param {Array<{ address: string }>} before
|
|
182
|
+
* @param {number} timeoutSeconds
|
|
183
|
+
*/
|
|
184
|
+
async function waitForConnectService(adb, host, before, timeoutSeconds) {
|
|
185
|
+
const previous = new Set(before.map((item) => item.address))
|
|
186
|
+
const deadline = Date.now() + timeoutSeconds * 1000
|
|
187
|
+
while (Date.now() < deadline) {
|
|
188
|
+
const services = await listMdnsServices(adb)
|
|
189
|
+
const candidates = services.filter((item) => item.type === 'connect' && item.host === host)
|
|
190
|
+
const fresh = candidates.find((item) => !previous.has(item.address))
|
|
191
|
+
if (fresh) {
|
|
192
|
+
return fresh
|
|
193
|
+
}
|
|
194
|
+
if (candidates.length === 1) {
|
|
195
|
+
return candidates[0]
|
|
196
|
+
}
|
|
197
|
+
await sleep(1000)
|
|
198
|
+
}
|
|
199
|
+
return null
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {string} adb
|
|
204
|
+
* @param {string | undefined} value
|
|
205
|
+
* @param {Logger} logger
|
|
206
|
+
*/
|
|
207
|
+
async function connect(adb, value, logger) {
|
|
208
|
+
if (!value) {
|
|
209
|
+
throw new PackError('缺少设备地址', EXIT.DEVICE, { hint: 'uni-pack device connect <ip[:port]>,默认端口 5555' })
|
|
210
|
+
}
|
|
211
|
+
const address = normalizeAddress(value)
|
|
212
|
+
logger.step(`连接 ${address}`)
|
|
213
|
+
await connectDevice(adb, address)
|
|
214
|
+
const device = await waitForDevice(adb, address, 10000)
|
|
215
|
+
if (device) {
|
|
216
|
+
logger.success(`已连接:${device.serial}${device.model ? `(${device.model})` : ''}`)
|
|
217
|
+
} else {
|
|
218
|
+
logger.warn(`已发起连接,但设备状态未就绪:${address}`)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {string} adb
|
|
224
|
+
* @param {string | undefined} value
|
|
225
|
+
* @param {Logger} logger
|
|
226
|
+
*/
|
|
227
|
+
async function disconnect(adb, value, logger) {
|
|
228
|
+
const address = value ? normalizeAddress(value) : ''
|
|
229
|
+
const output = await disconnectDevice(adb, address || undefined)
|
|
230
|
+
logger.success(output || (address ? `已断开 ${address}` : '已断开所有无线设备'))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @param {string} adb
|
|
235
|
+
* @param {Record<string, any>} options
|
|
236
|
+
* @param {Logger} logger
|
|
237
|
+
*/
|
|
238
|
+
async function tcpip(adb, options, logger) {
|
|
239
|
+
const devices = await listDevices(adb)
|
|
240
|
+
const device = pickDevice(devices, options.device)
|
|
241
|
+
if (device.serial.includes(':')) {
|
|
242
|
+
throw new PackError(`设备已经是无线连接:${device.serial}`, EXIT.DEVICE, {
|
|
243
|
+
hint: 'USB 转无线请先用数据线连接设备',
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
const port = Number(options.port ?? 5555)
|
|
247
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
248
|
+
throw new PackError(`端口不合法:${options.port}`, EXIT.CONFIG, { hint: '端口范围 1–65535,无线调试常用 5555' })
|
|
249
|
+
}
|
|
250
|
+
const ip = await deviceWifiIp(adb, device.serial)
|
|
251
|
+
if (!ip) {
|
|
252
|
+
throw new PackError('无法读取设备的 Wi-Fi IP', EXIT.DEVICE, {
|
|
253
|
+
hint: '确认手机已连接 Wi-Fi;也可以手动执行 uni-pack device connect <手机IP>:5555',
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
logger.step(`开启无线调试:${device.serial}`)
|
|
258
|
+
await enableTcpip(adb, device.serial, port)
|
|
259
|
+
const address = `${ip}:${port}`
|
|
260
|
+
logger.success(`设备已监听 ${address}`)
|
|
261
|
+
|
|
262
|
+
if (options.connect) {
|
|
263
|
+
await connect(adb, address, logger)
|
|
264
|
+
return
|
|
265
|
+
}
|
|
266
|
+
logger.info(`连接:uni-pack device connect ${address}`)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @param {string} adb
|
|
271
|
+
* @param {Logger} logger
|
|
272
|
+
*/
|
|
273
|
+
async function mdns(adb, logger) {
|
|
274
|
+
if (!(await mdnsSupported(adb))) {
|
|
275
|
+
throw new PackError('当前 adb 不支持 mDNS 发现', EXIT.DEVICE, {
|
|
276
|
+
hint: '升级 Android SDK platform-tools 后重试',
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
const services = await listMdnsServices(adb)
|
|
280
|
+
if (services.length === 0) {
|
|
281
|
+
logger.warn('没有发现 mDNS 服务(无线调试是否已开启?手机与电脑是否在同一 Wi-Fi?)')
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
for (const service of services) {
|
|
285
|
+
logger.info(`${service.name} ${service.address} [${service.type}]`)
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @param {number} value
|
|
291
|
+
* @param {number} min
|
|
292
|
+
* @param {number} max
|
|
293
|
+
*/
|
|
294
|
+
function clamp(value, min, max) {
|
|
295
|
+
return Math.min(max, Math.max(min, value))
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* @param {number} milliseconds
|
|
300
|
+
*/
|
|
301
|
+
function sleep(milliseconds) {
|
|
302
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
303
|
+
}
|