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
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gradle 日志里有两类「同类重复告警」会把真正的错误埋掉:
|
|
3
|
+
* - D8/R8 解析 DCloud AAR 的 Kotlin 2.2 元数据失败(每个 AAR/类一份几十行栈)
|
|
4
|
+
* - lint 报 Kotlin metadata 版本不兼容(每个 AAR 一行)
|
|
5
|
+
* 这里把它们折叠成「N 条同类告警 + 首个样例」,其余行原样保留。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const D8_PARSE = /^WARNING: D8: An error occurred when parsing kotlin metadata/i
|
|
9
|
+
const D8_REWRITE = /^WARNING: D8: Unexpected error during rewriting of Kotlin metadata/i
|
|
10
|
+
const KOTLIN_METADATA = /^e: file:.*Module was compiled with an incompatible version of Kotlin/i
|
|
11
|
+
|
|
12
|
+
const LABELS = {
|
|
13
|
+
'd8-parse': 'D8 解析 Kotlin metadata 失败(R8 版本低于 AAR 的 Kotlin 元数据版本)',
|
|
14
|
+
'kotlin-metadata': 'Kotlin 元数据版本不兼容(lint 无法分析高版本 Kotlin 编译的 AAR)',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const MAX_SAMPLE_CHARS = 240
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} value
|
|
21
|
+
* @param {number} [max]
|
|
22
|
+
*/
|
|
23
|
+
function truncate(value, max = MAX_SAMPLE_CHARS) {
|
|
24
|
+
return value.length > max ? `${value.slice(0, max)}…` : value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 流式折叠器:非重复行立即写出;重复块按 key 计数,结束写摘要。
|
|
29
|
+
*/
|
|
30
|
+
export class GradleLogAggregator {
|
|
31
|
+
/**
|
|
32
|
+
* @param {{ onLine: (line: string) => void, maxSampleLines?: number }} options
|
|
33
|
+
*/
|
|
34
|
+
constructor(options) {
|
|
35
|
+
this.write = options.onLine
|
|
36
|
+
this.maxSampleLines = options.maxSampleLines ?? 2
|
|
37
|
+
/** @type {Map<string, { label: string, count: number, sample: string[] }>} */
|
|
38
|
+
this.groups = new Map()
|
|
39
|
+
/** @type {{ kind: 'd8-rewrite', lines: string[] } | null} */
|
|
40
|
+
this.pending = null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @param {string} line */
|
|
44
|
+
push(line) {
|
|
45
|
+
if (this.pending) {
|
|
46
|
+
if (this.isContinuation(line)) {
|
|
47
|
+
this.pending.lines.push(line)
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
this.flushPending()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (D8_PARSE.test(line)) {
|
|
54
|
+
this.record('d8-parse', LABELS['d8-parse'], [line])
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
if (KOTLIN_METADATA.test(line)) {
|
|
58
|
+
this.record('kotlin-metadata', LABELS['kotlin-metadata'], [line])
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
if (D8_REWRITE.test(line)) {
|
|
62
|
+
this.pending = { kind: 'd8-rewrite', lines: [line] }
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
this.write(line)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {string} line
|
|
70
|
+
*/
|
|
71
|
+
isContinuation(line) {
|
|
72
|
+
if (!this.pending) {
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
if (this.pending.lines.length === 1) {
|
|
76
|
+
// 紧跟在 WARNING 后的异常头:com.android.tools.r8.internal.Os0: Should never be called
|
|
77
|
+
return /^[\w.$]+:\s+\S/.test(line) && !/^(WARNING|FAILURE|BUILD|e:|>)/.test(line)
|
|
78
|
+
}
|
|
79
|
+
// 栈帧缩进(制表符或空格)
|
|
80
|
+
return /^\s+\S/.test(line) && !/^\s+(WARNING|FAILURE|BUILD)/.test(line)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
flushPending() {
|
|
84
|
+
const pending = this.pending
|
|
85
|
+
if (!pending) {
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
this.pending = null
|
|
89
|
+
const header = pending.lines[1]
|
|
90
|
+
const message = header ? /^[\w.$]+:\s+(.+)$/.exec(header)?.[1] : undefined
|
|
91
|
+
const label = message ? `D8 重写 Kotlin metadata 失败(${message})` : 'D8 重写 Kotlin metadata 失败'
|
|
92
|
+
this.record(`d8-rewrite:${message ?? ''}`, label, pending.lines)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {string} key
|
|
97
|
+
* @param {string} label
|
|
98
|
+
* @param {string[]} lines
|
|
99
|
+
*/
|
|
100
|
+
record(key, label, lines) {
|
|
101
|
+
const group = this.groups.get(key) ?? { label, count: 0, sample: [] }
|
|
102
|
+
group.count += 1
|
|
103
|
+
if (group.sample.length === 0) {
|
|
104
|
+
group.sample = lines.slice(0, this.maxSampleLines).map((line) => truncate(line))
|
|
105
|
+
}
|
|
106
|
+
this.groups.set(key, group)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 结束并写出聚合摘要,返回分组(供控制台输出)。
|
|
111
|
+
* @returns {Array<{ label: string, count: number, sample: string[] }>}
|
|
112
|
+
*/
|
|
113
|
+
finish() {
|
|
114
|
+
this.flushPending()
|
|
115
|
+
if (this.groups.size === 0) {
|
|
116
|
+
return []
|
|
117
|
+
}
|
|
118
|
+
this.write('')
|
|
119
|
+
this.write('===== 以下重复告警已聚合(原始栈用 --verbose 重跑获取)=====')
|
|
120
|
+
for (const group of this.groups.values()) {
|
|
121
|
+
this.write(`${group.label} ×${group.count}`)
|
|
122
|
+
for (const sample of group.sample) {
|
|
123
|
+
this.write(` 样例:${sample}`)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return [...this.groups.values()]
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 失败时只保留 FAILURE / What went wrong 前后各若干行,避免把 1 万多行日志塞进终端。
|
|
132
|
+
* @param {string[]} lines
|
|
133
|
+
* @param {{ before?: number, after?: number }} [options]
|
|
134
|
+
* @returns {string}
|
|
135
|
+
*/
|
|
136
|
+
export function extractFailureContext(lines, options = {}) {
|
|
137
|
+
const before = options.before ?? 15
|
|
138
|
+
const after = options.after ?? 15
|
|
139
|
+
const patterns = [/^FAILURE:/, /What went wrong/i]
|
|
140
|
+
for (const pattern of patterns) {
|
|
141
|
+
const index = lines.findIndex((line) => pattern.test(line))
|
|
142
|
+
if (index >= 0) {
|
|
143
|
+
return lines.slice(Math.max(0, index - before), Math.min(lines.length, index + after + 1)).join('\n')
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const fallback = lines.findIndex((line) => /error:|Caused by:/i.test(line))
|
|
147
|
+
if (fallback >= 0) {
|
|
148
|
+
return lines.slice(Math.max(0, fallback - 5), Math.min(lines.length, fallback + after + 1)).join('\n')
|
|
149
|
+
}
|
|
150
|
+
return ''
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 已知问题直接给修复方向(与上下文一起展示)。
|
|
155
|
+
* @param {string} output
|
|
156
|
+
* @returns {string}
|
|
157
|
+
*/
|
|
158
|
+
export function knownFailureCause(output) {
|
|
159
|
+
if (/Could not resolve|Could not download|Could not GET/i.test(output)) {
|
|
160
|
+
return '依赖下载失败:检查网络,或先联网成功构建一次再使用 --offline'
|
|
161
|
+
}
|
|
162
|
+
if (/Manifest merger failed/i.test(output)) {
|
|
163
|
+
return 'AndroidManifest 合并失败:检查包名、权限与模板内容是否冲突'
|
|
164
|
+
}
|
|
165
|
+
if (/Duplicate class/i.test(output)) {
|
|
166
|
+
return 'AAR 重复:检查 libs 目录是否有多余的重复依赖'
|
|
167
|
+
}
|
|
168
|
+
if (/SDK location not found|sdk\.dir/i.test(output)) {
|
|
169
|
+
return 'Android SDK 路径不正确:检查配置里的 android.sdkDir 与 local.properties'
|
|
170
|
+
}
|
|
171
|
+
if (/Unsupported class file major version|Unsupported Java/i.test(output)) {
|
|
172
|
+
return 'JDK 版本不匹配:确认 JAVA_HOME 指向 JDK 17+'
|
|
173
|
+
}
|
|
174
|
+
return ''
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @param {{ lines: string[], logFile: string }} options
|
|
179
|
+
* @returns {string}
|
|
180
|
+
*/
|
|
181
|
+
export function formatFailureHint(options) {
|
|
182
|
+
const cause = knownFailureCause(options.lines.join('\n'))
|
|
183
|
+
let context = extractFailureContext(options.lines)
|
|
184
|
+
if (context.length > 5000) {
|
|
185
|
+
context = `${context.slice(0, 5000)}\n…(已截断)`
|
|
186
|
+
}
|
|
187
|
+
return [cause, context, `完整日志:${options.logFile}`].filter(Boolean).join('\n')
|
|
188
|
+
}
|
package/src/core/hash.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { createReadStream } from 'node:fs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 流式计算文件摘要,避免把几十 MB 的 APK/wgt 整个读进内存。
|
|
6
|
+
* @param {string} file
|
|
7
|
+
* @param {string} [algorithm]
|
|
8
|
+
* @returns {Promise<string>}
|
|
9
|
+
*/
|
|
10
|
+
export function fileHash(file, algorithm = 'sha256') {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const hash = createHash(algorithm)
|
|
13
|
+
createReadStream(file)
|
|
14
|
+
.on('data', (chunk) => hash.update(chunk))
|
|
15
|
+
.on('error', reject)
|
|
16
|
+
.on('end', () => resolve(hash.digest('hex')))
|
|
17
|
+
})
|
|
18
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { readdir } from 'node:fs/promises'
|
|
4
|
+
import { PackError, EXIT } from './errors.js'
|
|
5
|
+
import { exists, readJson } from './fsx.js'
|
|
6
|
+
import { run } from './process.js'
|
|
7
|
+
|
|
8
|
+
const VUE3_COMPILER = {
|
|
9
|
+
vueVersion: 3,
|
|
10
|
+
dir: path.join('plugins', 'uniapp-cli-vite'),
|
|
11
|
+
entry: path.join('node_modules', '@dcloudio', 'vite-plugin-uni', 'bin', 'uni.js'),
|
|
12
|
+
args: ['build', '-p', 'app'],
|
|
13
|
+
platform: 'app',
|
|
14
|
+
}
|
|
15
|
+
const VUE2_COMPILER = {
|
|
16
|
+
vueVersion: 2,
|
|
17
|
+
dir: path.join('plugins', 'uniapp-cli'),
|
|
18
|
+
entry: path.join('bin', 'uniapp-cli.js'),
|
|
19
|
+
args: [],
|
|
20
|
+
platform: 'app-plus',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Vue 2 与 Vue 3 的 HBuilderX 编译入口不同,按 manifest.vueVersion 选择。
|
|
25
|
+
* @param {number | string | undefined} vueVersion
|
|
26
|
+
*/
|
|
27
|
+
export function selectHbuilderxCompiler(vueVersion) {
|
|
28
|
+
return Number(vueVersion) === 2 ? VUE2_COMPILER : VUE3_COMPILER
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* about/package.json 的版本形如 5.34.2026102909;编译器写进 manifest 的是前两段。
|
|
33
|
+
* @param {string} version
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
export function normalizeHbuilderxVersion(version) {
|
|
37
|
+
const parts = String(version ?? '').split('.')
|
|
38
|
+
return parts.length >= 2 ? `${parts[0]}.${parts[1]}` : String(version ?? '')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} dir
|
|
43
|
+
* @returns {Promise<{ root: string, version: string } | null>}
|
|
44
|
+
*/
|
|
45
|
+
async function probeInstall(dir) {
|
|
46
|
+
const dirs = [dir, path.join(dir, 'HBuilderX')]
|
|
47
|
+
for (const candidate of dirs) {
|
|
48
|
+
const about = path.join(candidate, 'plugins', 'about', 'package.json')
|
|
49
|
+
const hasCompiler =
|
|
50
|
+
(await exists(path.join(candidate, VUE3_COMPILER.dir))) ||
|
|
51
|
+
(await exists(path.join(candidate, VUE2_COMPILER.dir)))
|
|
52
|
+
if (!(await exists(about)) || !hasCompiler) {
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
const pkg = await readJson(about).catch(() => ({}))
|
|
56
|
+
if (pkg.name !== 'about') {
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
return { root: candidate, version: normalizeHbuilderxVersion(pkg.version) }
|
|
60
|
+
}
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string} dir
|
|
66
|
+
* @returns {Promise<Array<{ root: string, version: string }>>}
|
|
67
|
+
*/
|
|
68
|
+
async function scanRoot(dir) {
|
|
69
|
+
/** @type {Array<{ root: string, version: string }>} */
|
|
70
|
+
const found = []
|
|
71
|
+
const direct = await probeInstall(dir)
|
|
72
|
+
if (direct) {
|
|
73
|
+
found.push(direct)
|
|
74
|
+
}
|
|
75
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
76
|
+
for (const entry of entries) {
|
|
77
|
+
if (!entry.isDirectory() || !/hbuilderx/i.test(entry.name)) {
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
const install = await probeInstall(path.join(dir, entry.name))
|
|
81
|
+
if (install) {
|
|
82
|
+
found.push(install)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return found
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @param {string[]} [extraDirs]
|
|
90
|
+
* @returns {Promise<Array<{ root: string, version: string }>>}
|
|
91
|
+
*/
|
|
92
|
+
export async function listHbuilderxInstalls(extraDirs = []) {
|
|
93
|
+
/** @type {string[]} */
|
|
94
|
+
const roots = [
|
|
95
|
+
...extraDirs,
|
|
96
|
+
process.env.HBUILDERX_HOME,
|
|
97
|
+
process.env.HX_APP_ROOT,
|
|
98
|
+
'C:/Program Files',
|
|
99
|
+
'C:/Program Files (x86)',
|
|
100
|
+
'C:/Soft',
|
|
101
|
+
'D:/',
|
|
102
|
+
'D:/Soft',
|
|
103
|
+
'D:/soft',
|
|
104
|
+
'D:/sort',
|
|
105
|
+
'E:/',
|
|
106
|
+
os.homedir(),
|
|
107
|
+
path.join(os.homedir(), 'AppData', 'Local'),
|
|
108
|
+
]
|
|
109
|
+
.filter((item) => typeof item === 'string' && item.trim() !== '')
|
|
110
|
+
.map((item) => String(item))
|
|
111
|
+
|
|
112
|
+
const seen = new Set()
|
|
113
|
+
/** @type {Array<{ root: string, version: string }>} */
|
|
114
|
+
const installs = []
|
|
115
|
+
for (const root of roots) {
|
|
116
|
+
for (const install of await scanRoot(path.resolve(String(root)))) {
|
|
117
|
+
const key = install.root.toLowerCase()
|
|
118
|
+
if (seen.has(key)) {
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
seen.add(key)
|
|
122
|
+
installs.push(install)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
installs.sort((a, b) => compareVersion(b.version, a.version))
|
|
126
|
+
return installs
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {string} a
|
|
131
|
+
* @param {string} b
|
|
132
|
+
*/
|
|
133
|
+
function compareVersion(a, b) {
|
|
134
|
+
const left = String(a).split('.').map(Number)
|
|
135
|
+
const right = String(b).split('.').map(Number)
|
|
136
|
+
for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
|
|
137
|
+
const diff = (left[index] || 0) - (right[index] || 0)
|
|
138
|
+
if (diff !== 0) {
|
|
139
|
+
return diff
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return 0
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 项目上次用 HBuilderX 编译时写进产物 manifest 的版本,用来挑对应版本的 HBuilderX。
|
|
147
|
+
* @param {string} projectDir
|
|
148
|
+
* @returns {Promise<string>}
|
|
149
|
+
*/
|
|
150
|
+
export async function readLastBuiltVersion(projectDir) {
|
|
151
|
+
const file = path.join(projectDir, 'unpackage/dist/build/app-plus/manifest.json')
|
|
152
|
+
if (!(await exists(file))) {
|
|
153
|
+
return ''
|
|
154
|
+
}
|
|
155
|
+
const manifest = await readJson(file).catch(() => ({}))
|
|
156
|
+
return String(manifest?.['app-plus']?.['uni-app']?.compilerVersion ?? '')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 解析顺序:显式指定 → 环境变量 → 上次构建版本对应的安装 → 最新安装。
|
|
161
|
+
* @param {any} config
|
|
162
|
+
* @param {{ project?: any, explicit?: string, projectDir?: string }} [options]
|
|
163
|
+
* @returns {Promise<{ root: string, version: string } | null>}
|
|
164
|
+
*/
|
|
165
|
+
export async function resolveHbuilderx(config, options = {}) {
|
|
166
|
+
const projectDir = String(options.projectDir ?? options.project?.projectDir ?? '')
|
|
167
|
+
const explicit = [
|
|
168
|
+
options.explicit,
|
|
169
|
+
options.project?.hbuilderxRoot,
|
|
170
|
+
config?.hbuilderx?.root,
|
|
171
|
+
process.env.HBUILDERX_HOME,
|
|
172
|
+
process.env.HX_APP_ROOT,
|
|
173
|
+
].filter((item) => typeof item === 'string').filter((item) => item.trim() !== '')
|
|
174
|
+
|
|
175
|
+
for (const candidate of explicit) {
|
|
176
|
+
const install = await probeInstall(path.resolve(candidate))
|
|
177
|
+
if (install) {
|
|
178
|
+
return install
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const installs = await listHbuilderxInstalls([...(config?.hbuilderx?.searchDirs ?? [])])
|
|
183
|
+
if (installs.length === 0) {
|
|
184
|
+
return null
|
|
185
|
+
}
|
|
186
|
+
if (projectDir) {
|
|
187
|
+
const lastVersion = await readLastBuiltVersion(projectDir)
|
|
188
|
+
const matched = installs.find((install) => install.version === lastVersion)
|
|
189
|
+
if (matched) {
|
|
190
|
+
return matched
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return installs[0]
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 用 HBuilderX 自带的 node + uni-app 编译器构建,产物结构与 HBuilderX 发行一致。
|
|
198
|
+
* @param {Object} options
|
|
199
|
+
* @param {string} options.root
|
|
200
|
+
* @param {string} options.version
|
|
201
|
+
* @param {string} options.inputDir
|
|
202
|
+
* @param {string} options.outputDir
|
|
203
|
+
* @param {number | string} [options.vueVersion] 2 或 3;默认 3
|
|
204
|
+
* @param {string} [options.tempDir] 子进程 TEMP/TMP 指向的可写临时目录
|
|
205
|
+
* @param {import('./logger.js').Logger} options.logger
|
|
206
|
+
*/
|
|
207
|
+
export async function runHbuilderxBuild(options) {
|
|
208
|
+
const { root, version, inputDir, outputDir, logger } = options
|
|
209
|
+
const compiler = selectHbuilderxCompiler(options.vueVersion ?? 3)
|
|
210
|
+
const pluginDir = path.join(root, compiler.dir)
|
|
211
|
+
const entry = path.join(pluginDir, compiler.entry)
|
|
212
|
+
if (!(await exists(entry))) {
|
|
213
|
+
throw new PackError(`HBuilderX 缺少 Vue ${compiler.vueVersion} 编译器:${entry}`, EXIT.WEB_BUILD, {
|
|
214
|
+
hint: `确认 manifest.json 的 vueVersion 是否正确,或升级 HBuilderX 后重试`,
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const node = await resolveNode(root)
|
|
219
|
+
logger.step(`构建前端资源:HBuilderX ${version}(Vue ${compiler.vueVersion})`)
|
|
220
|
+
try {
|
|
221
|
+
await run(node, [entry, ...compiler.args], {
|
|
222
|
+
cwd: pluginDir,
|
|
223
|
+
env: {
|
|
224
|
+
HX_Version: version,
|
|
225
|
+
RUN_BY_HBUILDERX: '1',
|
|
226
|
+
UNI_HBUILDERX_PLUGINS: path.join(root, 'plugins'),
|
|
227
|
+
UNI_CLI_CONTEXT: pluginDir,
|
|
228
|
+
UNI_INPUT_DIR: inputDir,
|
|
229
|
+
UNI_OUTPUT_DIR: outputDir,
|
|
230
|
+
UNI_PLATFORM: compiler.platform,
|
|
231
|
+
NODE_ENV: 'production',
|
|
232
|
+
...(options.tempDir ? { TEMP: options.tempDir, TMP: options.tempDir } : {}),
|
|
233
|
+
},
|
|
234
|
+
onLine: (line) => logger.detail(line),
|
|
235
|
+
})
|
|
236
|
+
} catch (error) {
|
|
237
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
238
|
+
throw new PackError('前端构建失败(HBuilderX 编译器)', EXIT.WEB_BUILD, {
|
|
239
|
+
hint: `${detail}\n可用 HBuilderX 打开项目手工编译一次,确认项目本身能编译通过`,
|
|
240
|
+
cause: error,
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @param {string} root
|
|
247
|
+
* @returns {Promise<string>}
|
|
248
|
+
*/
|
|
249
|
+
async function resolveNode(root) {
|
|
250
|
+
const candidates = [
|
|
251
|
+
path.join(root, 'plugins', 'node', process.platform === 'win32' ? 'node.exe' : 'bin/node'),
|
|
252
|
+
path.join(root, 'plugins', 'node', 'node.exe'),
|
|
253
|
+
]
|
|
254
|
+
for (const candidate of candidates) {
|
|
255
|
+
if (await exists(candidate)) {
|
|
256
|
+
return candidate
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return process.execPath
|
|
260
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { PackError, EXIT } from './errors.js'
|
|
4
|
+
import { run } from './process.js'
|
|
5
|
+
|
|
6
|
+
/** HBuilderX launch 命令要求 5.0+。 */
|
|
7
|
+
const MIN_MAJOR = 5
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{ major: number, minor: number, patch: number, raw: string }} CliVersion
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `cli ver` 可能带 ANSI 颜色,输出形如 5.34.2026102909。
|
|
15
|
+
* @param {string} output
|
|
16
|
+
* @returns {CliVersion | null}
|
|
17
|
+
*/
|
|
18
|
+
export function parseHbuilderxCliVersion(output) {
|
|
19
|
+
const text = String(output ?? '').replace(/\u001b\[[0-9;]*m/g, '')
|
|
20
|
+
const match = /(\d+)\.(\d+)\.(\d+)(?:\.\d+)?/.exec(text)
|
|
21
|
+
if (!match) {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
major: Number(match[1]),
|
|
26
|
+
minor: Number(match[2]),
|
|
27
|
+
patch: Number(match[3]),
|
|
28
|
+
raw: match[0],
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {CliVersion | null} version
|
|
34
|
+
* @returns {boolean}
|
|
35
|
+
*/
|
|
36
|
+
export function supportsLaunch(version) {
|
|
37
|
+
return Boolean(version && version.major >= MIN_MAJOR)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} root HBuilderX 安装目录
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
44
|
+
export function cliPath(root) {
|
|
45
|
+
return path.join(root, process.platform === 'win32' ? 'cli.exe' : 'cli')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* HBuilderX 本地自定义基座的约定位置。
|
|
50
|
+
* @param {string} projectDir
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
export function localCustomBasePath(projectDir) {
|
|
54
|
+
return path.join(projectDir, 'unpackage', 'debug', 'android_debug.apk')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {Object} options
|
|
59
|
+
* @param {string} options.projectDir
|
|
60
|
+
* @param {string} [options.deviceId]
|
|
61
|
+
* @param {'standard' | 'custom'} [options.playground]
|
|
62
|
+
* @param {'local' | 'device'} [options.customType]
|
|
63
|
+
* @param {string} [options.packageName]
|
|
64
|
+
* @param {boolean} [options.ui]
|
|
65
|
+
* @returns {string[]}
|
|
66
|
+
*/
|
|
67
|
+
export function buildLaunchArgs(options) {
|
|
68
|
+
const args = ['launch', 'app-android', '--project', options.projectDir]
|
|
69
|
+
if (options.deviceId) {
|
|
70
|
+
args.push('--deviceId', options.deviceId)
|
|
71
|
+
}
|
|
72
|
+
if (options.playground === 'custom') {
|
|
73
|
+
args.push('--playground', 'custom', '--customType', options.customType ?? 'local')
|
|
74
|
+
if (options.packageName) {
|
|
75
|
+
args.push('--packageName', options.packageName)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (options.ui) {
|
|
79
|
+
args.push('--ui', 'true')
|
|
80
|
+
}
|
|
81
|
+
return args
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* `cli ver` 能返回版本才认为主程序在运行;未运行时尝试 `cli open` 并等待。
|
|
86
|
+
* @param {Object} options
|
|
87
|
+
* @param {string} options.cli
|
|
88
|
+
* @param {import('./logger.js').Logger} options.logger
|
|
89
|
+
* @param {Record<string, string | undefined>} [options.env]
|
|
90
|
+
* @param {number} [options.timeoutMs]
|
|
91
|
+
* @returns {Promise<CliVersion>}
|
|
92
|
+
*/
|
|
93
|
+
export async function ensureHbuilderxRunning(options) {
|
|
94
|
+
const probe = async () => {
|
|
95
|
+
const { stdout, stderr } = await run(options.cli, ['ver'], { allowFailure: true, env: options.env })
|
|
96
|
+
return parseHbuilderxCliVersion(`${stdout}${stderr}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let version = await probe()
|
|
100
|
+
if (!version) {
|
|
101
|
+
options.logger.info('HBuilderX 未运行,执行 cli open 启动(首次可能需要几十秒)…')
|
|
102
|
+
await run(options.cli, ['open'], { allowFailure: true, env: options.env })
|
|
103
|
+
const deadline = Date.now() + (options.timeoutMs ?? 90000)
|
|
104
|
+
while (Date.now() < deadline) {
|
|
105
|
+
await sleep(2000)
|
|
106
|
+
version = await probe()
|
|
107
|
+
if (version) {
|
|
108
|
+
break
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!version) {
|
|
114
|
+
throw new PackError('无法连接 HBuilderX CLI', EXIT.DEVICE, {
|
|
115
|
+
hint: [
|
|
116
|
+
'确认 HBuilderX 5.0+ 已安装并能正常启动(uni-pack init 里的 hbuilderx.root)',
|
|
117
|
+
'手动打开 HBuilderX 后重试,或执行 cli open',
|
|
118
|
+
'不要以管理员身份运行 HBuilderX:普通权限的 cli 无法连接高权限进程(命名管道 EPERM)',
|
|
119
|
+
].join('\n'),
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
if (!supportsLaunch(version)) {
|
|
123
|
+
throw new PackError(`HBuilderX ${version.raw} 版本过低,launch 需要 5.0+`, EXIT.DEVICE, {
|
|
124
|
+
hint: '升级 HBuilderX 后重试',
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
return version
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 前台运行 `cli launch`,继承终端输出(HBuilderX 会把项目 console 日志打到终端)。
|
|
132
|
+
* @param {Object} options
|
|
133
|
+
* @param {string} options.cli
|
|
134
|
+
* @param {string[]} options.args
|
|
135
|
+
* @param {Record<string, string | undefined>} [options.env]
|
|
136
|
+
* @returns {Promise<number>}
|
|
137
|
+
*/
|
|
138
|
+
export function runLaunch(options) {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
const child = spawn(options.cli, options.args, {
|
|
141
|
+
stdio: 'inherit',
|
|
142
|
+
windowsHide: true,
|
|
143
|
+
env: { ...process.env, ...options.env },
|
|
144
|
+
})
|
|
145
|
+
const stop = () => {
|
|
146
|
+
child.kill()
|
|
147
|
+
}
|
|
148
|
+
process.once('SIGINT', stop)
|
|
149
|
+
process.once('SIGTERM', stop)
|
|
150
|
+
child.on('error', (error) => {
|
|
151
|
+
process.removeListener('SIGINT', stop)
|
|
152
|
+
process.removeListener('SIGTERM', stop)
|
|
153
|
+
reject(error)
|
|
154
|
+
})
|
|
155
|
+
child.on('close', (code) => {
|
|
156
|
+
process.removeListener('SIGINT', stop)
|
|
157
|
+
process.removeListener('SIGTERM', stop)
|
|
158
|
+
resolve(code ?? 1)
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {number} milliseconds
|
|
165
|
+
*/
|
|
166
|
+
function sleep(milliseconds) {
|
|
167
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds))
|
|
168
|
+
}
|