uniapp-android-cli 0.1.0 → 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/README.md +239 -452
- package/bin/uni-pack.js +0 -0
- package/package.json +1 -1
- package/src/cli.js +63 -5
- package/src/commands/build.js +4 -0
- package/src/commands/debug.js +103 -3
- package/src/commands/device.js +8 -2
- package/src/commands/doctor.js +10 -0
- package/src/commands/init.js +290 -79
- package/src/commands/projects.js +31 -0
- package/src/commands/wgt.js +51 -7
- package/src/core/adb.js +71 -13
- package/src/core/build.js +90 -26
- package/src/core/config.js +52 -1
- package/src/core/debugPreset.js +56 -0
- package/src/core/devicePairing.js +2 -1
- package/src/core/fsx.js +12 -1
- package/src/core/gradle.js +192 -46
- package/src/core/gradleLog.js +188 -0
- package/src/core/hash.js +18 -0
- package/src/core/hbuilderx.js +2 -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/output.js +123 -16
- package/src/core/patch/gradle.js +40 -2
- package/src/core/patch/template.js +28 -10
- package/src/core/patch/utsPlugins.js +1 -0
- package/src/core/process.js +66 -17
- package/src/core/project.js +13 -0
- package/src/core/resourceVersion.js +87 -0
- package/src/core/sdk.js +6 -1
- package/src/core/signing.js +24 -6
- package/src/core/templateCache.js +81 -0
- package/src/core/webBuild.js +10 -2
- package/src/core/wgt.js +47 -25
- 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
|
+
}
|
package/src/core/hbuilderx.js
CHANGED
|
@@ -201,6 +201,7 @@ export async function resolveHbuilderx(config, options = {}) {
|
|
|
201
201
|
* @param {string} options.inputDir
|
|
202
202
|
* @param {string} options.outputDir
|
|
203
203
|
* @param {number | string} [options.vueVersion] 2 或 3;默认 3
|
|
204
|
+
* @param {string} [options.tempDir] 子进程 TEMP/TMP 指向的可写临时目录
|
|
204
205
|
* @param {import('./logger.js').Logger} options.logger
|
|
205
206
|
*/
|
|
206
207
|
export async function runHbuilderxBuild(options) {
|
|
@@ -228,6 +229,7 @@ export async function runHbuilderxBuild(options) {
|
|
|
228
229
|
UNI_OUTPUT_DIR: outputDir,
|
|
229
230
|
UNI_PLATFORM: compiler.platform,
|
|
230
231
|
NODE_ENV: 'production',
|
|
232
|
+
...(options.tempDir ? { TEMP: options.tempDir, TMP: options.tempDir } : {}),
|
|
231
233
|
},
|
|
232
234
|
onLine: (line) => logger.detail(line),
|
|
233
235
|
})
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {{ ok: boolean, value?: string, message?: string }} Validation
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const PACKAGE_RE = /^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$/i
|
|
9
|
+
const APPKEY_RE = /^[0-9a-f]{32}$/i
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 空值表示「跳过」,交给构建期报错;交互时允许先不填。
|
|
13
|
+
* @param {string} value
|
|
14
|
+
* @returns {Validation}
|
|
15
|
+
*/
|
|
16
|
+
export function validatePackageName(value) {
|
|
17
|
+
if (!value) {
|
|
18
|
+
return { ok: true, value: '' }
|
|
19
|
+
}
|
|
20
|
+
if (!PACKAGE_RE.test(value)) {
|
|
21
|
+
return { ok: false, message: '包名不合法,示例:uni.app.UNIF1A8702' }
|
|
22
|
+
}
|
|
23
|
+
return { ok: true, value }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} value
|
|
28
|
+
* @returns {Validation}
|
|
29
|
+
*/
|
|
30
|
+
export function validateAppKey(value) {
|
|
31
|
+
if (!value) {
|
|
32
|
+
return { ok: true, value: '' }
|
|
33
|
+
}
|
|
34
|
+
if (!APPKEY_RE.test(value)) {
|
|
35
|
+
return { ok: false, message: 'AppKey 应为 32 位十六进制(0-9a-f)' }
|
|
36
|
+
}
|
|
37
|
+
return { ok: true, value: value.toLowerCase() }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 文件路径允许相对项目目录;不存在则重新询问。
|
|
42
|
+
* @param {string} projectDir
|
|
43
|
+
* @returns {(value: string) => Validation}
|
|
44
|
+
*/
|
|
45
|
+
export function makeFileValidator(projectDir) {
|
|
46
|
+
return (value) => {
|
|
47
|
+
if (!value) {
|
|
48
|
+
return { ok: true, value: '' }
|
|
49
|
+
}
|
|
50
|
+
const resolved = path.isAbsolute(value) ? value : path.resolve(projectDir, value)
|
|
51
|
+
if (!existsSync(resolved)) {
|
|
52
|
+
return { ok: false, message: `找不到文件:${resolved}` }
|
|
53
|
+
}
|
|
54
|
+
return { ok: true, value: resolved }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} value
|
|
60
|
+
* @returns {Validation}
|
|
61
|
+
*/
|
|
62
|
+
export function validateRequired(value) {
|
|
63
|
+
return value ? { ok: true, value } : { ok: false, message: '不能为空' }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} value
|
|
68
|
+
* @returns {Validation}
|
|
69
|
+
*/
|
|
70
|
+
export function validateConfirm(value) {
|
|
71
|
+
if (!value || /^y(es)?$/i.test(value) || /^n(o)?$/i.test(value)) {
|
|
72
|
+
return { ok: true, value: value.toLowerCase() }
|
|
73
|
+
}
|
|
74
|
+
return { ok: false, message: '输入 Y 或 n' }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 最终确认必须显式回答:回车不默认同意,避免手快直接写配置。
|
|
79
|
+
* @param {string} value
|
|
80
|
+
* @returns {Validation}
|
|
81
|
+
*/
|
|
82
|
+
export function validateExplicitConfirm(value) {
|
|
83
|
+
if (/^y(es)?$/i.test(value) || /^n(o)?$/i.test(value)) {
|
|
84
|
+
return { ok: true, value: value.toLowerCase() }
|
|
85
|
+
}
|
|
86
|
+
return { ok: false, message: '请输入 y 或 n(回车不会默认确认)' }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {string} value
|
|
91
|
+
* @returns {Validation}
|
|
92
|
+
*/
|
|
93
|
+
export function validateCustomType(value) {
|
|
94
|
+
const text = String(value ?? '').trim().toLowerCase()
|
|
95
|
+
if (text === 'local' || text === 'device') {
|
|
96
|
+
return { ok: true, value: text }
|
|
97
|
+
}
|
|
98
|
+
return { ok: false, message: '输入 local 或 device' }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {string} value
|
|
103
|
+
* @returns {Validation}
|
|
104
|
+
*/
|
|
105
|
+
export function validatePlayground(value) {
|
|
106
|
+
const text = String(value ?? '').trim().toLowerCase()
|
|
107
|
+
if (text === 'standard' || text === 'custom') {
|
|
108
|
+
return { ok: true, value: text }
|
|
109
|
+
}
|
|
110
|
+
return { ok: false, message: '输入 standard 或 custom' }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 项目内路径尽量写成相对路径,方便提交到仓库;项目外保留绝对路径。
|
|
115
|
+
* @param {string} projectDir
|
|
116
|
+
* @param {string} value
|
|
117
|
+
* @returns {string}
|
|
118
|
+
*/
|
|
119
|
+
export function toProjectRelative(projectDir, value) {
|
|
120
|
+
const text = String(value ?? '').trim()
|
|
121
|
+
if (!text) {
|
|
122
|
+
return ''
|
|
123
|
+
}
|
|
124
|
+
const absolute = path.isAbsolute(text) ? text : path.resolve(projectDir, text)
|
|
125
|
+
const relative = path.relative(projectDir, absolute)
|
|
126
|
+
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative)) {
|
|
127
|
+
return relative.split(path.sep).join('/')
|
|
128
|
+
}
|
|
129
|
+
return absolute
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const PROJECT_ONLY_USER_FIELDS = ['appKey', 'package', 'signing', 'webBuild', 'outputDir', 'outputPrefix', 'debug']
|
|
133
|
+
|
|
134
|
+
// 版本/包名等以 manifest 为准,不写进任何配置,避免旧快照覆盖项目改动
|
|
135
|
+
const SNAPSHOT_FIELDS = ['name', 'appid', 'versionName', 'versionCode', 'minSdkVersion', 'targetSdkVersion', 'abiFilters']
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 把 init 的结果拆成「项目级共享配置」与「本机用户配置」:
|
|
139
|
+
* - 项目配置:包名、AppKey、签名路径、输出目录、webBuild,可提交到仓库;
|
|
140
|
+
* - 用户配置:projectDir / type / sdkRoot / hbuilderxRoot 等本机路径;
|
|
141
|
+
* - 密码永远不写入任何配置。
|
|
142
|
+
* @param {Object} input
|
|
143
|
+
* @param {string} input.projectDir
|
|
144
|
+
* @param {'cli' | 'hbuilderx'} input.type
|
|
145
|
+
* @param {string} input.packageName
|
|
146
|
+
* @param {string} input.appKey
|
|
147
|
+
* @param {string} input.outputDir
|
|
148
|
+
* @param {string} [input.outputPrefix]
|
|
149
|
+
* @param {any} [input.webBuild]
|
|
150
|
+
* @param {{ keystore: string, keyAlias: string, keystoreProperties: string,
|
|
151
|
+
* storePasswordEnv?: string, keyPasswordEnv?: string }} input.signing
|
|
152
|
+
* @param {any} [input.existingProjectConfig]
|
|
153
|
+
* @param {any} [input.existingUserEntry]
|
|
154
|
+
* @param {Record<string, any>} [input.debug]
|
|
155
|
+
* @param {string} input.sdkRoot
|
|
156
|
+
* @param {string} [input.hbuilderxRoot]
|
|
157
|
+
* @returns {{ projectConfig: Record<string, any>, userEntry: Record<string, any> }}
|
|
158
|
+
*/
|
|
159
|
+
export function splitInitConfig(input) {
|
|
160
|
+
const existingProject = input.existingProjectConfig ?? {}
|
|
161
|
+
const existingUser = input.existingUserEntry ?? {}
|
|
162
|
+
/** @type {Record<string, any>} */
|
|
163
|
+
const projectConfig = { ...existingProject }
|
|
164
|
+
for (const field of ['appid', 'name', 'versionName', 'versionCode', 'minSdkVersion', 'targetSdkVersion', 'abiFilters']) {
|
|
165
|
+
delete projectConfig[field]
|
|
166
|
+
}
|
|
167
|
+
projectConfig.type = input.type
|
|
168
|
+
if (input.packageName) {
|
|
169
|
+
projectConfig.package = input.packageName
|
|
170
|
+
} else {
|
|
171
|
+
delete projectConfig.package
|
|
172
|
+
}
|
|
173
|
+
if (input.appKey) {
|
|
174
|
+
projectConfig.appKey = input.appKey
|
|
175
|
+
} else {
|
|
176
|
+
delete projectConfig.appKey
|
|
177
|
+
}
|
|
178
|
+
projectConfig.outputDir = toProjectRelative(input.projectDir, input.outputDir) || 'apk'
|
|
179
|
+
if (input.outputPrefix) {
|
|
180
|
+
projectConfig.outputPrefix = input.outputPrefix
|
|
181
|
+
}
|
|
182
|
+
if (input.webBuild) {
|
|
183
|
+
projectConfig.webBuild = input.webBuild
|
|
184
|
+
}
|
|
185
|
+
const debug = {
|
|
186
|
+
...(existingProject.debug ?? existingUser.debug ?? {}),
|
|
187
|
+
...(input.debug ?? {}),
|
|
188
|
+
}
|
|
189
|
+
if (Object.keys(debug).length > 0) {
|
|
190
|
+
projectConfig.debug = debug
|
|
191
|
+
}
|
|
192
|
+
projectConfig.signing = {
|
|
193
|
+
...(existingProject.signing ?? existingUser.signing ?? {}),
|
|
194
|
+
keystore: toProjectRelative(input.projectDir, input.signing.keystore),
|
|
195
|
+
keyAlias: String(input.signing.keyAlias ?? ''),
|
|
196
|
+
keystoreProperties: toProjectRelative(input.projectDir, input.signing.keystoreProperties),
|
|
197
|
+
storePasswordEnv: input.signing.storePasswordEnv || 'UNI_PACK_STORE_PASS',
|
|
198
|
+
keyPasswordEnv: input.signing.keyPasswordEnv || 'UNI_PACK_KEY_PASS',
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @type {Record<string, any>} */
|
|
202
|
+
const userEntry = {
|
|
203
|
+
...existingUser,
|
|
204
|
+
projectDir: input.projectDir,
|
|
205
|
+
type: input.type,
|
|
206
|
+
sdkRoot: input.sdkRoot,
|
|
207
|
+
hbuilderxRoot: input.hbuilderxRoot ?? existingUser.hbuilderxRoot ?? '',
|
|
208
|
+
}
|
|
209
|
+
for (const field of [...PROJECT_ONLY_USER_FIELDS, ...SNAPSHOT_FIELDS]) {
|
|
210
|
+
delete userEntry[field]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { projectConfig, userEntry }
|
|
214
|
+
}
|