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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +382 -2
  3. package/bin/uni-pack.js +7 -0
  4. package/package.json +66 -4
  5. package/src/cli.js +242 -0
  6. package/src/commands/build.js +46 -0
  7. package/src/commands/debug.js +183 -0
  8. package/src/commands/device.js +309 -0
  9. package/src/commands/devices.js +23 -0
  10. package/src/commands/doctor.js +170 -0
  11. package/src/commands/init.js +488 -0
  12. package/src/commands/projects.js +31 -0
  13. package/src/commands/wgt.js +90 -0
  14. package/src/core/adb.js +292 -0
  15. package/src/core/build.js +438 -0
  16. package/src/core/config.js +208 -0
  17. package/src/core/debugPreset.js +56 -0
  18. package/src/core/devicePairing.js +153 -0
  19. package/src/core/errors.js +36 -0
  20. package/src/core/fsx.js +183 -0
  21. package/src/core/gradle.js +221 -0
  22. package/src/core/gradleLog.js +188 -0
  23. package/src/core/hash.js +18 -0
  24. package/src/core/hbuilderx.js +260 -0
  25. package/src/core/hbuilderxCli.js +168 -0
  26. package/src/core/initConfig.js +214 -0
  27. package/src/core/initEnv.js +140 -0
  28. package/src/core/interactive.js +93 -0
  29. package/src/core/logger.js +82 -0
  30. package/src/core/output.js +181 -0
  31. package/src/core/patch/assets.js +154 -0
  32. package/src/core/patch/gradle.js +166 -0
  33. package/src/core/patch/manifest.js +81 -0
  34. package/src/core/patch/nativePlugins.js +137 -0
  35. package/src/core/patch/projectManifest.js +66 -0
  36. package/src/core/patch/template.js +75 -0
  37. package/src/core/patch/utsPlugins.js +425 -0
  38. package/src/core/process.js +120 -0
  39. package/src/core/project.js +263 -0
  40. package/src/core/qrcode.js +13 -0
  41. package/src/core/resourceVersion.js +87 -0
  42. package/src/core/sdk.js +403 -0
  43. package/src/core/signing.js +102 -0
  44. package/src/core/templateCache.js +81 -0
  45. package/src/core/version.js +128 -0
  46. package/src/core/webBuild.js +77 -0
  47. package/src/core/wgt.js +98 -0
  48. package/src/core/workdir.js +44 -0
@@ -0,0 +1,292 @@
1
+ import { PackError, EXIT } from './errors.js'
2
+ import { run } from './process.js'
3
+
4
+ /**
5
+ * @typedef {{ serial: string, state: string, model: string }} Device
6
+ * @typedef {{ name: string, type: 'pairing' | 'connect' | 'other', host: string, port: number, address: string }} MdnsService
7
+ */
8
+
9
+ /**
10
+ * @param {string} adb
11
+ * @returns {Promise<boolean>}
12
+ */
13
+ export async function mdnsSupported(adb) {
14
+ const { code, stdout, stderr } = await run(adb, ['mdns', 'check'], { allowFailure: true })
15
+ // 失败时也常带 "mdns"(如 ERROR: mdns daemon unavailable),只认版本行
16
+ return code === 0 && /mdns daemon version/i.test(`${stdout}${stderr}`)
17
+ }
18
+
19
+ /**
20
+ * 解析 `adb mdns services` 输出,兼容两种格式:
21
+ * - 新版 3 列:<实例名> <服务类型> <host>:<port>
22
+ * - 老版 2 列:<实例名含服务类型> <host>:<port>
23
+ * 同时兼容 IPv4 与 [IPv6]:port。
24
+ * @param {string} stdout
25
+ * @returns {MdnsService[]}
26
+ */
27
+ export function parseMdnsServices(stdout) {
28
+ /** @type {MdnsService[]} */
29
+ const services = []
30
+ for (const rawLine of stdout.split(/\r?\n/)) {
31
+ const line = rawLine.trim()
32
+ if (!line || /^List of discovered/i.test(line)) {
33
+ continue
34
+ }
35
+ const fields = line.split(/\s+/)
36
+ if (fields.length < 2) {
37
+ continue
38
+ }
39
+ const addressToken = fields[fields.length - 1]
40
+ const match = /^(\[[^\]]+\]|[^\s:]+):(\d+)$/.exec(addressToken)
41
+ if (!match) {
42
+ continue
43
+ }
44
+ const name = fields[0]
45
+ const typeText = fields.length > 2 ? fields.slice(1, -1).join(' ') : name
46
+ const host = match[1].replace(/^\[|\]$/g, '')
47
+ const type = typeText.includes('_adb-tls-pairing._tcp')
48
+ ? 'pairing'
49
+ : typeText.includes('_adb-tls-connect._tcp')
50
+ ? 'connect'
51
+ : 'other'
52
+ services.push({ name, type, host, port: Number(match[2]), address: `${host}:${match[2]}` })
53
+ }
54
+ return services
55
+ }
56
+
57
+ /**
58
+ * @param {string} adb
59
+ * @returns {Promise<MdnsService[]>}
60
+ */
61
+ export async function listMdnsServices(adb) {
62
+ const { stdout } = await run(adb, ['mdns', 'services'], { allowFailure: true })
63
+ return parseMdnsServices(stdout)
64
+ }
65
+
66
+ /**
67
+ * @param {string} adb
68
+ * @param {string} address
69
+ * @param {string} code
70
+ */
71
+ export async function pairDevice(adb, address, code) {
72
+ const { code: exitCode, stdout, stderr } = await run(adb, ['pair', address, code], { allowFailure: true })
73
+ const output = `${stdout}\n${stderr}`.trim()
74
+ if (exitCode !== 0 || /failed|error|cannot/i.test(output)) {
75
+ throw new PackError(`配对失败:${output.split(/\r?\n/).slice(-3).join('\n') || 'adb pair 无输出'}`, EXIT.DEVICE, {
76
+ hint: '确认手机与电脑在同一 Wi-Fi、二维码未过期;如有 VPN 先关闭再重试',
77
+ })
78
+ }
79
+ return output
80
+ }
81
+
82
+ /**
83
+ * @param {string} adb
84
+ * @param {string} address
85
+ */
86
+ export async function connectDevice(adb, address) {
87
+ const { code, stdout, stderr } = await run(adb, ['connect', address], { allowFailure: true })
88
+ const output = `${stdout}\n${stderr}`.trim()
89
+ if (code !== 0 || /failed|cannot/i.test(output)) {
90
+ throw new PackError(`连接失败:${output.split(/\r?\n/).slice(-3).join('\n') || address}`, EXIT.DEVICE, {
91
+ hint: '确认手机无线调试已开启、与电脑同网段,并已配对',
92
+ })
93
+ }
94
+ return output
95
+ }
96
+
97
+ /**
98
+ * @param {string} adb
99
+ * @param {string} [address]
100
+ */
101
+ export async function disconnectDevice(adb, address) {
102
+ const args = address ? ['disconnect', address] : ['disconnect']
103
+ const { stdout, stderr } = await run(adb, args, { allowFailure: true })
104
+ return `${stdout}\n${stderr}`.trim()
105
+ }
106
+
107
+ /**
108
+ * @param {string} adb
109
+ * @param {string} serial
110
+ * @param {number} port
111
+ */
112
+ export async function enableTcpip(adb, serial, port) {
113
+ const { code, stdout, stderr } = await run(adb, ['-s', serial, 'tcpip', String(port)], { allowFailure: true })
114
+ if (code !== 0) {
115
+ throw new PackError(`开启无线调试失败:${(stderr || stdout).trim()}`, EXIT.DEVICE)
116
+ }
117
+ }
118
+
119
+ /**
120
+ * @param {string} adb
121
+ * @param {string} serial
122
+ * @returns {Promise<string>}
123
+ */
124
+ export async function deviceWifiIp(adb, serial) {
125
+ const { stdout } = await run(adb, ['-s', serial, 'shell', 'ip', '-f', 'inet', 'addr', 'show', 'wlan0'], {
126
+ allowFailure: true,
127
+ })
128
+ const match = /inet\s+(\d+\.\d+\.\d+\.\d+)/.exec(stdout)
129
+ return match?.[1] ?? ''
130
+ }
131
+
132
+ /**
133
+ * @param {string} adb
134
+ * @param {string} address
135
+ * @param {number} timeoutMs
136
+ */
137
+ export async function waitForDevice(adb, address, timeoutMs) {
138
+ const deadline = Date.now() + timeoutMs
139
+ while (Date.now() < deadline) {
140
+ const devices = await listDevices(adb).catch(() => [])
141
+ const match = devices.find((device) => device.serial === address && device.state === 'device')
142
+ if (match) {
143
+ return match
144
+ }
145
+ await new Promise((resolve) => setTimeout(resolve, 1000))
146
+ }
147
+ return null
148
+ }
149
+
150
+ /**
151
+ * @param {string} adb
152
+ * @returns {Promise<Device[]>}
153
+ */
154
+ export async function listDevices(adb) {
155
+ const { stdout } = await run(adb, ['devices', '-l'], { allowFailure: false })
156
+ return stdout
157
+ .split(/\r?\n/)
158
+ .slice(1)
159
+ .filter((line) => line.trim().length > 0 && !line.startsWith('*'))
160
+ .map((line) => {
161
+ const [serial, state, ...rest] = line.trim().split(/\s+/)
162
+ const model = rest.find((item) => item.startsWith('model:'))?.slice('model:'.length) ?? ''
163
+ return { serial, state, model }
164
+ })
165
+ }
166
+
167
+ /**
168
+ * 按完整序列号、型号或唯一子串找设备。多设备时避免手抄长序列号。
169
+ * @param {Device[]} devices
170
+ * @param {string | undefined} input
171
+ * @returns {Device | null}
172
+ */
173
+ export function matchDevice(devices, input) {
174
+ const query = String(input ?? '').trim()
175
+ if (!query) {
176
+ return null
177
+ }
178
+ const exact = devices.find((device) => device.serial === query)
179
+ if (exact) {
180
+ return exact
181
+ }
182
+ const lower = query.toLowerCase()
183
+ const byModel = devices.filter((device) => device.model && device.model.toLowerCase() === lower)
184
+ if (byModel.length === 1) {
185
+ return byModel[0]
186
+ }
187
+ const matches = devices.filter(
188
+ (device) =>
189
+ device.serial.toLowerCase().includes(lower) || (device.model && device.model.toLowerCase().includes(lower)),
190
+ )
191
+ if (matches.length === 1) {
192
+ return matches[0]
193
+ }
194
+ if (matches.length > 1) {
195
+ throw new PackError(`设备不唯一:${query}`, EXIT.DEVICE, {
196
+ hint: `候选:${matches.map(formatDevice).join(';')}`,
197
+ })
198
+ }
199
+ return null
200
+ }
201
+
202
+ /**
203
+ * @param {Device} device
204
+ * @returns {string}
205
+ */
206
+ function formatDevice(device) {
207
+ return `${device.serial}${device.model ? `(${device.model})` : ''}`
208
+ }
209
+
210
+ /**
211
+ * @param {Device[]} devices
212
+ * @param {string} [input] 完整序列号、型号或唯一子串
213
+ */
214
+ export function pickDevice(devices, input) {
215
+ if (input) {
216
+ const match = matchDevice(devices, input)
217
+ if (!match) {
218
+ throw new PackError(`找不到设备:${input}`, EXIT.DEVICE, {
219
+ hint: `可用设备:${devices.map(formatDevice).join(';') || '(无)'};支持序列号后缀或型号`,
220
+ })
221
+ }
222
+ return match
223
+ }
224
+ const online = devices.filter((device) => device.state === 'device')
225
+ if (online.length === 0) {
226
+ throw new PackError('没有可用设备,请连接设备或启动模拟器后重试', EXIT.DEVICE)
227
+ }
228
+ return online[0]
229
+ }
230
+
231
+ /**
232
+ * @param {string} adb
233
+ * @param {string} apk
234
+ * @param {string} serial
235
+ */
236
+ export async function installApk(adb, apk, serial) {
237
+ const { code, stdout, stderr } = await run(adb, ['-s', serial, 'install', '-r', apk], { allowFailure: true })
238
+ if (code !== 0 || /Failure/i.test(stdout + stderr)) {
239
+ throw new PackError(`安装失败:${(stdout + stderr).trim().split('\n').slice(-3).join('\n')}`, EXIT.DEVICE)
240
+ }
241
+ }
242
+
243
+ /**
244
+ * @param {string} adb
245
+ * @param {string} serial
246
+ * @param {string} packageName
247
+ */
248
+ export async function isInstalled(adb, serial, packageName) {
249
+ const { stdout } = await run(adb, ['-s', serial, 'shell', 'pm', 'list', 'packages', packageName], {
250
+ allowFailure: true,
251
+ })
252
+ return stdout.split(/\r?\n/).some((line) => line.trim() === `package:${packageName}`)
253
+ }
254
+
255
+ /**
256
+ * @param {string} adb
257
+ * @param {string} packageName
258
+ * @param {string} activity
259
+ * @param {string} serial
260
+ */
261
+ export async function startApp(adb, packageName, activity, serial) {
262
+ await run(adb, ['-s', serial, 'shell', 'am', 'force-stop', packageName], { allowFailure: true })
263
+ const { code, stdout, stderr } = await run(
264
+ adb,
265
+ ['-s', serial, 'shell', 'am', 'start', '-n', `${packageName}/${activity}`],
266
+ { allowFailure: true },
267
+ )
268
+ if (code !== 0) {
269
+ throw new PackError(`启动应用失败:${(stderr || stdout).trim()}`, EXIT.DEVICE, {
270
+ hint: '基座未安装?先执行 uni-pack debug --install',
271
+ })
272
+ }
273
+ }
274
+
275
+ /**
276
+ * @param {string} adb
277
+ * @param {string} serial
278
+ * @param {number} port
279
+ */
280
+ export async function reversePort(adb, serial, port) {
281
+ await run(adb, ['-s', serial, 'reverse', `tcp:${port}`, `tcp:${port}`], { allowFailure: false })
282
+ }
283
+
284
+ /**
285
+ * @param {string} adb
286
+ * @param {string} serial
287
+ * @returns {Promise<string>}
288
+ */
289
+ export async function packagePid(adb, serial, packageName) {
290
+ const { stdout } = await run(adb, ['-s', serial, 'shell', 'pidof', packageName], { allowFailure: true })
291
+ return stdout.trim()
292
+ }