uniapp-android-cli 0.0.0-stage → 0.1.2
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 +382 -2
- package/bin/uni-pack.js +7 -0
- package/package.json +66 -4
- package/src/cli.js +242 -0
- package/src/commands/build.js +46 -0
- package/src/commands/debug.js +183 -0
- package/src/commands/device.js +309 -0
- package/src/commands/devices.js +23 -0
- package/src/commands/doctor.js +170 -0
- package/src/commands/init.js +488 -0
- package/src/commands/projects.js +31 -0
- package/src/commands/wgt.js +90 -0
- package/src/core/adb.js +292 -0
- package/src/core/build.js +438 -0
- package/src/core/config.js +208 -0
- package/src/core/debugPreset.js +56 -0
- package/src/core/devicePairing.js +153 -0
- package/src/core/errors.js +36 -0
- package/src/core/fsx.js +183 -0
- package/src/core/gradle.js +221 -0
- package/src/core/gradleLog.js +188 -0
- package/src/core/hash.js +18 -0
- package/src/core/hbuilderx.js +260 -0
- package/src/core/hbuilderxCli.js +168 -0
- package/src/core/initConfig.js +214 -0
- package/src/core/initEnv.js +140 -0
- package/src/core/interactive.js +93 -0
- package/src/core/logger.js +82 -0
- package/src/core/output.js +181 -0
- package/src/core/patch/assets.js +154 -0
- package/src/core/patch/gradle.js +166 -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 +75 -0
- package/src/core/patch/utsPlugins.js +425 -0
- package/src/core/process.js +120 -0
- package/src/core/project.js +263 -0
- package/src/core/qrcode.js +13 -0
- package/src/core/resourceVersion.js +87 -0
- package/src/core/sdk.js +403 -0
- package/src/core/signing.js +102 -0
- package/src/core/templateCache.js +81 -0
- package/src/core/version.js +128 -0
- package/src/core/webBuild.js +77 -0
- package/src/core/wgt.js +98 -0
- package/src/core/workdir.js +44 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
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
|
+
yes: { type: 'boolean', short: 'y' },
|
|
18
|
+
keystore: { type: 'string' },
|
|
19
|
+
'keystore-properties': { type: 'string' },
|
|
20
|
+
'key-alias': { type: 'string' },
|
|
21
|
+
package: { type: 'string' },
|
|
22
|
+
sdk: { type: 'string' },
|
|
23
|
+
hbuilderx: { type: 'string' },
|
|
24
|
+
'android-sdk': { type: 'string' },
|
|
25
|
+
jdk: { type: 'string' },
|
|
26
|
+
},
|
|
27
|
+
doctor: { ...COMMON },
|
|
28
|
+
projects: { ...COMMON },
|
|
29
|
+
devices: { ...COMMON },
|
|
30
|
+
device: {
|
|
31
|
+
...COMMON,
|
|
32
|
+
timeout: { type: 'string' },
|
|
33
|
+
service: { type: 'string' },
|
|
34
|
+
code: { type: 'string' },
|
|
35
|
+
port: { type: 'string' },
|
|
36
|
+
device: { type: 'string' },
|
|
37
|
+
connect: { type: 'boolean' },
|
|
38
|
+
},
|
|
39
|
+
build: {
|
|
40
|
+
...COMMON,
|
|
41
|
+
mode: { type: 'string' },
|
|
42
|
+
'skip-web': { type: 'boolean' },
|
|
43
|
+
'skip-resource-check': { type: 'boolean' },
|
|
44
|
+
out: { type: 'string' },
|
|
45
|
+
install: { type: 'boolean' },
|
|
46
|
+
device: { type: 'string' },
|
|
47
|
+
offline: { type: 'boolean' },
|
|
48
|
+
'no-daemon': { type: 'boolean' },
|
|
49
|
+
prune: { type: 'string' },
|
|
50
|
+
'keep-workdir': { type: 'boolean' },
|
|
51
|
+
'skip-version-check': { type: 'boolean' },
|
|
52
|
+
'store-password': { type: 'string' },
|
|
53
|
+
'key-password': { type: 'string' },
|
|
54
|
+
},
|
|
55
|
+
wgt: {
|
|
56
|
+
...COMMON,
|
|
57
|
+
'skip-web': { type: 'boolean' },
|
|
58
|
+
'skip-resource-check': { type: 'boolean' },
|
|
59
|
+
out: { type: 'string' },
|
|
60
|
+
},
|
|
61
|
+
debug: {
|
|
62
|
+
...COMMON,
|
|
63
|
+
install: { type: 'boolean' },
|
|
64
|
+
push: { type: 'boolean' },
|
|
65
|
+
logs: { type: 'boolean' },
|
|
66
|
+
device: { type: 'string' },
|
|
67
|
+
base: { type: 'string' },
|
|
68
|
+
build: { type: 'boolean' },
|
|
69
|
+
hbuilderx: { type: 'boolean' },
|
|
70
|
+
playground: { type: 'string' },
|
|
71
|
+
'custom-type': { type: 'string' },
|
|
72
|
+
ui: { type: 'boolean' },
|
|
73
|
+
'skip-web': { type: 'boolean' },
|
|
74
|
+
'skip-resource-check': { type: 'boolean' },
|
|
75
|
+
'no-daemon': { type: 'boolean' },
|
|
76
|
+
prune: { type: 'string' },
|
|
77
|
+
'store-password': { type: 'string' },
|
|
78
|
+
'key-password': { type: 'string' },
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {string[]} argv
|
|
84
|
+
*/
|
|
85
|
+
export async function main(argv) {
|
|
86
|
+
const [command, ...rest] = argv
|
|
87
|
+
if (command === 'version' || command === '--version' || command === '-v') {
|
|
88
|
+
console.log(VERSION)
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
if (!command || command === 'help' || command === '--help' || command === '-h') {
|
|
92
|
+
printHelp()
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
const spec = SPECS[command]
|
|
96
|
+
if (!spec) {
|
|
97
|
+
console.error(`未知命令:${command}`)
|
|
98
|
+
printHelp()
|
|
99
|
+
process.exitCode = 2
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let options
|
|
104
|
+
try {
|
|
105
|
+
const { values, positionals } = parseArgs({ args: rest, options: spec, allowPositionals: true })
|
|
106
|
+
options = normalize(values)
|
|
107
|
+
options.positionals = positionals
|
|
108
|
+
options.cwd = process.cwd()
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error(translateArgsError(error, rest, spec))
|
|
111
|
+
process.exitCode = 2
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
if (command === 'init') {
|
|
117
|
+
const { initCommand } = await import('./commands/init.js')
|
|
118
|
+
await initCommand(options)
|
|
119
|
+
} else if (command === 'doctor') {
|
|
120
|
+
const { doctorCommand } = await import('./commands/doctor.js')
|
|
121
|
+
await doctorCommand(options)
|
|
122
|
+
} else if (command === 'projects') {
|
|
123
|
+
const { projectsCommand } = await import('./commands/projects.js')
|
|
124
|
+
await projectsCommand(options)
|
|
125
|
+
} else if (command === 'devices') {
|
|
126
|
+
const { devicesCommand } = await import('./commands/devices.js')
|
|
127
|
+
await devicesCommand(options)
|
|
128
|
+
} else if (command === 'device') {
|
|
129
|
+
const { deviceCommand } = await import('./commands/device.js')
|
|
130
|
+
await deviceCommand(options)
|
|
131
|
+
} else if (command === 'build') {
|
|
132
|
+
const { buildCommand } = await import('./commands/build.js')
|
|
133
|
+
await buildCommand(options)
|
|
134
|
+
} else if (command === 'wgt') {
|
|
135
|
+
const { wgtCommand } = await import('./commands/wgt.js')
|
|
136
|
+
await wgtCommand(options)
|
|
137
|
+
} else if (command === 'debug') {
|
|
138
|
+
const { debugCommand } = await import('./commands/debug.js')
|
|
139
|
+
await debugCommand(options)
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
const failure = toFailure(error)
|
|
143
|
+
if (options.json) {
|
|
144
|
+
console.log(JSON.stringify({ ok: false, code: failure.code, message: failure.message, hint: failure.hint }, null, 2))
|
|
145
|
+
} else {
|
|
146
|
+
console.error(`\n✗ ${failure.message}`)
|
|
147
|
+
if (failure.hint) {
|
|
148
|
+
console.error(` ${failure.hint}`)
|
|
149
|
+
}
|
|
150
|
+
if (error instanceof PackError && error.cause instanceof Error) {
|
|
151
|
+
console.error(` 原因:${error.cause.message}`)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
process.exitCode = failure.code
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* node:util parseArgs 的报错是英文,且单横线长参数会被拆成短选项(-appkey → -a),
|
|
160
|
+
* 这里翻译成中文并提示正确写法,避免用户对着 "Unknown option '-a'" 猜。
|
|
161
|
+
* @param {unknown} error
|
|
162
|
+
* @param {string[]} args
|
|
163
|
+
* @param {Record<string, unknown>} spec
|
|
164
|
+
* @returns {string}
|
|
165
|
+
*/
|
|
166
|
+
export function translateArgsError(error, args, spec) {
|
|
167
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
168
|
+
const known = new Set(Object.keys(spec))
|
|
169
|
+
const mistaken = args.find((arg) => /^-[^-][\w-]+$/.test(arg) && known.has(arg.slice(1)))
|
|
170
|
+
if (mistaken) {
|
|
171
|
+
return `未知参数:${mistaken}\n长参数需要两个横线,改为:--${mistaken.slice(1)}`
|
|
172
|
+
}
|
|
173
|
+
const unknown = /Unknown option '(-[^']+)'/.exec(message)?.[1]
|
|
174
|
+
if (unknown) {
|
|
175
|
+
const flags = new Set()
|
|
176
|
+
for (const [key, value] of Object.entries(spec)) {
|
|
177
|
+
const short =
|
|
178
|
+
value && typeof value === 'object' && 'short' in value ? String(/** @type {any} */ (value).short ?? '') : ''
|
|
179
|
+
if (short) {
|
|
180
|
+
flags.add(`-${short}`)
|
|
181
|
+
}
|
|
182
|
+
flags.add(key.length === 1 ? `-${key}` : `--${key}`)
|
|
183
|
+
}
|
|
184
|
+
return `未知参数:${unknown}\n可用参数:${[...flags].sort().join(' ')}`
|
|
185
|
+
}
|
|
186
|
+
return message
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @param {Record<string, unknown>} values
|
|
191
|
+
*/
|
|
192
|
+
function normalize(values) {
|
|
193
|
+
/** @type {Record<string, unknown>} */
|
|
194
|
+
const result = {}
|
|
195
|
+
for (const [key, value] of Object.entries(values)) {
|
|
196
|
+
result[key.replace(/-([a-z])/g, (_, char) => char.toUpperCase())] = value
|
|
197
|
+
}
|
|
198
|
+
return result
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function printHelp() {
|
|
202
|
+
console.log(`uni-pack ${VERSION} — uni-app Android 离线打包
|
|
203
|
+
|
|
204
|
+
用法:
|
|
205
|
+
uni-pack init [-p <dir>] [--appkey <key>] [--package <包名>] [--sdk <dir>] [--hbuilderx <dir>]
|
|
206
|
+
[--keystore <文件>] [--key-alias <别名>] [--keystore-properties <文件>]
|
|
207
|
+
[--android-sdk <dir>] [--jdk <dir>] [--yes]
|
|
208
|
+
(问答式写入 <项目>/uni-pack.config.json;缺环境会引导填写;CI 用 --yes 跳过)
|
|
209
|
+
uni-pack doctor [-p <dir>] [--json] 环境自检
|
|
210
|
+
uni-pack projects 列出已登记项目(-p 可用项目名/目录名)
|
|
211
|
+
uni-pack build [-p <dir>] [--mode release|debug] [--install] [--device <serial>]
|
|
212
|
+
[--skip-web] [--skip-resource-check] [--out <dir>] [--offline]
|
|
213
|
+
[--prune <n>] [--no-daemon]
|
|
214
|
+
uni-pack wgt [-p <dir>] [--skip-web] [--skip-resource-check] [--out <dir>]
|
|
215
|
+
uni-pack debug [-p <dir>] [--push] [--install] [--build] [--logs] [--device <serial>]
|
|
216
|
+
[--hbuilderx [--playground standard|custom] [--custom-type local|device] [--ui]]
|
|
217
|
+
uni-pack devices 列出设备(同 device list)
|
|
218
|
+
uni-pack device [list] 列出 USB / 无线设备
|
|
219
|
+
uni-pack device pair [--timeout 120] [--service <名>] [--code <6位>]
|
|
220
|
+
终端显示二维码,手机扫码配对(Android 11+)
|
|
221
|
+
uni-pack device pair <ip:port> <配对码> 手动配对(手机显示配对码时用)
|
|
222
|
+
uni-pack device connect <ip[:port]> 连接无线设备(默认端口 5555)
|
|
223
|
+
uni-pack device disconnect [ip[:port]] 断开无线设备
|
|
224
|
+
uni-pack device tcpip [--port 5555] [--connect] USB 设备开启无线调试
|
|
225
|
+
uni-pack device mdns 列出 mDNS 发现的无线调试服务
|
|
226
|
+
uni-pack version 显示版本
|
|
227
|
+
|
|
228
|
+
支持两种项目:
|
|
229
|
+
CLI 项目 src/manifest.json,按项目脚本选 build:app-android / build:app-plus
|
|
230
|
+
HBuilderX 项目 根目录 manifest.json,用 HBuilderX 自带编译器(Vue 2 / Vue 3)
|
|
231
|
+
|
|
232
|
+
环境变量:
|
|
233
|
+
UNI_SDK_ROOT 含 HBuilder-Integrate-AS 的离线 SDK 目录
|
|
234
|
+
UNI_SDK_DIRS 额外扫描 Android-SDK@x.yy 的目录(多个用 ; 分隔)
|
|
235
|
+
HBUILDERX_HOME HBuilderX 安装目录
|
|
236
|
+
ANDROID_HOME Android SDK 根目录
|
|
237
|
+
JAVA_HOME JDK 17+ 根目录
|
|
238
|
+
UNI_PACK_TEMP 前端构建临时目录(默认 <workDir>/<项目>/tmp)
|
|
239
|
+
|
|
240
|
+
配置:~/.uniapp-android-cli/config.json(项目内 uni-pack.config.json 可覆盖项目级字段)
|
|
241
|
+
`)
|
|
242
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
apkRecord: result.apkRecord,
|
|
40
|
+
latestLinked: result.latestLinked,
|
|
41
|
+
resourceVersion: result.resourceVersion,
|
|
42
|
+
resourceVersionCode: result.resourceVersionCode,
|
|
43
|
+
workDir: path.join(result.workRoot, 'gradle'),
|
|
44
|
+
})
|
|
45
|
+
return result
|
|
46
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
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 { resolveDebugBase, resolveDebugDevice, resolveDebugLogs, resolveDebugStrategy } from '../core/debugPreset.js'
|
|
7
|
+
import { PackError, EXIT } from '../core/errors.js'
|
|
8
|
+
import { ensureDir, exists } from '../core/fsx.js'
|
|
9
|
+
import { resolveHbuilderx } from '../core/hbuilderx.js'
|
|
10
|
+
import { buildLaunchArgs, cliPath, ensureHbuilderxRunning, localCustomBasePath, runLaunch } from '../core/hbuilderxCli.js'
|
|
11
|
+
import { Logger } from '../core/logger.js'
|
|
12
|
+
import { apkBaseName, resolveApkPrefix } from '../core/output.js'
|
|
13
|
+
import { normalizeManifest, readProjectManifest } from '../core/project.js'
|
|
14
|
+
import { resolveAndroidTools } from '../core/sdk.js'
|
|
15
|
+
import { resolveBuildTempDir, resolveWorkRoot } from '../core/workdir.js'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {Record<string, any>} options
|
|
19
|
+
*/
|
|
20
|
+
export async function debugCommand(options) {
|
|
21
|
+
const logger = new Logger(options)
|
|
22
|
+
const config = await loadConfig()
|
|
23
|
+
const { project } = await resolveProject(config, options.project, options.cwd)
|
|
24
|
+
const preset = project.debug ?? {}
|
|
25
|
+
|
|
26
|
+
options.device = resolveDebugDevice(options, preset) || undefined
|
|
27
|
+
options.logs = resolveDebugLogs(options, preset)
|
|
28
|
+
|
|
29
|
+
if (resolveDebugStrategy(options, preset) === 'hbuilderx') {
|
|
30
|
+
await runHbuilderxDebug({ config, project, options, logger, preset })
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const tools = await resolveAndroidTools(config)
|
|
35
|
+
|
|
36
|
+
const devices = await listDevices(tools.adb)
|
|
37
|
+
const device = pickDevice(devices, options.device)
|
|
38
|
+
const activity = String(project.debug?.launchActivity ?? 'io.dcloud.PandoraEntry')
|
|
39
|
+
|
|
40
|
+
const { manifest } = await readProjectManifest(project.projectDir)
|
|
41
|
+
const resolved = normalizeManifest(manifest, {
|
|
42
|
+
appid: project.appid,
|
|
43
|
+
name: project.name,
|
|
44
|
+
package: project.package,
|
|
45
|
+
versionName: project.versionName,
|
|
46
|
+
versionCode: project.versionCode,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
let baseApk = options.base ? path.resolve(options.base) : ''
|
|
50
|
+
if (!baseApk) {
|
|
51
|
+
const baseName = apkBaseName(resolveApkPrefix(project, resolved), resolved.versionName, resolved.versionCode)
|
|
52
|
+
const candidate = path.join(path.resolve(project.outputDir), `${baseName}_debug_latest.apk`)
|
|
53
|
+
baseApk = (await exists(candidate)) ? candidate : ''
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// --push 表示「带上最新前端资源重建基座」,等价于 build + install;
|
|
57
|
+
// SDK 的 wgt 安装会让基座崩溃,这里不走 plus.runtime.install。
|
|
58
|
+
const rebuild = Boolean(options.push) || Boolean(options.build) || !baseApk
|
|
59
|
+
if (rebuild) {
|
|
60
|
+
logger.step('构建自定义基座')
|
|
61
|
+
const built = await buildProject({
|
|
62
|
+
config,
|
|
63
|
+
project,
|
|
64
|
+
mode: 'debug',
|
|
65
|
+
options: { ...options, install: false },
|
|
66
|
+
logger,
|
|
67
|
+
})
|
|
68
|
+
baseApk = built.apk
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (options.install || options.push || rebuild) {
|
|
72
|
+
logger.step(`安装到 ${device.model || device.serial}`)
|
|
73
|
+
await installApk(tools.adb, baseApk, device.serial)
|
|
74
|
+
await startApp(tools.adb, resolved.packageName, activity, device.serial)
|
|
75
|
+
logger.success('基座已安装并启动')
|
|
76
|
+
} else {
|
|
77
|
+
logger.info(`基座:${baseApk}`)
|
|
78
|
+
logger.info('重新构建并安装:uni-pack debug --push;只看日志:--logs')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (options.logs) {
|
|
82
|
+
await streamLogs(tools.adb, device.serial)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 用 HBuilderX CLI 真机运行:HBuilderX 负责编译、安装基座、差量同步与项目日志,
|
|
88
|
+
* uni-pack 只做环境检查和参数转发。
|
|
89
|
+
* @param {{ config: any, project: any, options: Record<string, any>, logger: Logger, preset: any }} input
|
|
90
|
+
*/
|
|
91
|
+
async function runHbuilderxDebug(input) {
|
|
92
|
+
const { config, project, options, logger, preset } = input
|
|
93
|
+
if (options.playground && !['standard', 'custom'].includes(String(options.playground).toLowerCase())) {
|
|
94
|
+
throw new PackError(`--playground 只支持 standard 或 custom,收到:${options.playground}`, EXIT.CONFIG)
|
|
95
|
+
}
|
|
96
|
+
if (options.customType && !['local', 'device'].includes(String(options.customType).toLowerCase())) {
|
|
97
|
+
throw new PackError(`--custom-type 只支持 local 或 device,收到:${options.customType}`, EXIT.CONFIG)
|
|
98
|
+
}
|
|
99
|
+
const { playground, customType } = resolveDebugBase(options, preset)
|
|
100
|
+
|
|
101
|
+
const tools = await resolveAndroidTools(config)
|
|
102
|
+
const devices = await listDevices(tools.adb)
|
|
103
|
+
const device = pickDevice(devices, options.device ?? preset.device)
|
|
104
|
+
options.device = device.serial
|
|
105
|
+
|
|
106
|
+
// 本地自定义基座:先自己 adb install -r。
|
|
107
|
+
// HBuilderX 只按包名+版本判断“基座已是最新”,同版本的正式包会让它误判并跳过安装。
|
|
108
|
+
if (playground === 'custom' && customType === 'local') {
|
|
109
|
+
const baseApk = localCustomBasePath(project.projectDir)
|
|
110
|
+
if (!(await exists(baseApk))) {
|
|
111
|
+
throw new PackError(`找不到本地自定义基座:${baseApk}`, EXIT.PREFLIGHT, {
|
|
112
|
+
hint: '先在 HBuilderX 制作自定义基座(生成 unpackage/debug/android_debug.apk),或改用 --custom-type device',
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
logger.info(`安装本地自定义基座:${baseApk}`)
|
|
116
|
+
await installApk(tools.adb, baseApk, device.serial)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const hbuilderx = await resolveHbuilderx(config, { project })
|
|
120
|
+
if (!hbuilderx) {
|
|
121
|
+
throw new PackError('找不到 HBuilderX,无法使用 --hbuilderx 运行', EXIT.PREFLIGHT, {
|
|
122
|
+
hint: '运行 uni-pack init 配置 hbuilderx.root,或设置 HBUILDERX_HOME',
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
const cli = cliPath(hbuilderx.root)
|
|
126
|
+
if (!(await exists(cli))) {
|
|
127
|
+
throw new PackError(`找不到 HBuilderX CLI:${cli}`, EXIT.PREFLIGHT, {
|
|
128
|
+
hint: '确认 hbuilderx.root 指向 HBuilderX 安装目录(与 cli.exe 同级)',
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const workRoot = resolveWorkRoot(config, project)
|
|
133
|
+
const temp = resolveBuildTempDir(workRoot)
|
|
134
|
+
await ensureDir(temp.tempDir)
|
|
135
|
+
const env = { TEMP: temp.tempDir, TMP: temp.tempDir }
|
|
136
|
+
|
|
137
|
+
logger.step(`HBuilderX 真机运行:${project.projectDir}`)
|
|
138
|
+
const version = await ensureHbuilderxRunning({ cli, logger, env })
|
|
139
|
+
logger.info(`HBuilderX ${version.raw}${options.logs ? '(项目日志会直接输出)' : ''}`)
|
|
140
|
+
|
|
141
|
+
let packageName = ''
|
|
142
|
+
if (playground === 'custom' && customType === 'device') {
|
|
143
|
+
const { manifest } = await readProjectManifest(project.projectDir)
|
|
144
|
+
packageName = normalizeManifest(manifest, { package: project.package }, { validate: false }).packageName
|
|
145
|
+
if (!packageName) {
|
|
146
|
+
throw new PackError('--custom-type device 需要知道设备上基座的包名', EXIT.CONFIG, {
|
|
147
|
+
hint: '在 manifest 的 app-plus.distribute.android.packagename 配好包名,或改用 --custom-type local',
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (options.push || options.base || options.install || options.build) {
|
|
153
|
+
logger.info('--hbuilderx 模式下由 HBuilderX 负责构建/安装/启动,--push/--base/--install 会忽略')
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const args = buildLaunchArgs({
|
|
157
|
+
projectDir: project.projectDir,
|
|
158
|
+
deviceId: options.device,
|
|
159
|
+
playground: /** @type {'standard' | 'custom'} */ (playground),
|
|
160
|
+
customType: /** @type {'local' | 'device'} */ (customType),
|
|
161
|
+
packageName,
|
|
162
|
+
ui: Boolean(options.ui),
|
|
163
|
+
})
|
|
164
|
+
logger.info(`保存代码后自动差量编译并同步;Ctrl+C 结束${options.ui ? '(界面模式)' : ''}`)
|
|
165
|
+
const code = await runLaunch({ cli, args, env })
|
|
166
|
+
if (code !== 0) {
|
|
167
|
+
throw new PackError(`HBuilderX 运行结束(退出码 ${code})`, EXIT.DEVICE)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @param {string} adb
|
|
173
|
+
* @param {string} serial
|
|
174
|
+
*/
|
|
175
|
+
async function streamLogs(adb, serial) {
|
|
176
|
+
const child = spawn(adb, ['-s', serial, 'logcat', '-v', 'time', '-s', 'console:V', 'uni-app:V', 'AndroidRuntime:E'], {
|
|
177
|
+
stdio: 'inherit',
|
|
178
|
+
windowsHide: true,
|
|
179
|
+
})
|
|
180
|
+
const stop = () => child.kill()
|
|
181
|
+
process.once('SIGINT', stop)
|
|
182
|
+
await new Promise((resolve) => child.on('close', resolve))
|
|
183
|
+
}
|