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,263 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { exists, readJson, readJsonc, readText } from './fsx.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} ProjectManifest
|
|
7
|
+
* @property {string} appid
|
|
8
|
+
* @property {string} name
|
|
9
|
+
* @property {string} versionName
|
|
10
|
+
* @property {number} versionCode
|
|
11
|
+
* @property {string} packageName
|
|
12
|
+
* @property {string[]} abiFilters
|
|
13
|
+
* @property {number} minSdkVersion
|
|
14
|
+
* @property {number} targetSdkVersion
|
|
15
|
+
* @property {string[]} permissions
|
|
16
|
+
* @property {string[]} features
|
|
17
|
+
* @property {string[]} modules
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {Object} ProjectLayout
|
|
22
|
+
* @property {'cli' | 'hbuilderx' | 'unknown'} type
|
|
23
|
+
* @property {string} inputDir uni-app 源码目录(CLI 为 src,HBuilderX 为项目根)
|
|
24
|
+
* @property {string} manifestFile manifest.json 路径
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 两种项目的结构差异:
|
|
29
|
+
* - CLI(vite starter):src/manifest.json,产物 dist/build/app
|
|
30
|
+
* - HBuilderX:根目录 manifest.json(JSONC),产物 unpackage/dist/build/app-plus
|
|
31
|
+
* @param {string} projectDir
|
|
32
|
+
* @returns {Promise<ProjectLayout>}
|
|
33
|
+
*/
|
|
34
|
+
export async function detectProject(projectDir) {
|
|
35
|
+
const srcManifest = path.join(projectDir, 'src/manifest.json')
|
|
36
|
+
if (await exists(srcManifest)) {
|
|
37
|
+
return { type: 'cli', inputDir: path.join(projectDir, 'src'), manifestFile: srcManifest }
|
|
38
|
+
}
|
|
39
|
+
const rootManifest = path.join(projectDir, 'manifest.json')
|
|
40
|
+
if (await exists(rootManifest)) {
|
|
41
|
+
return { type: 'hbuilderx', inputDir: projectDir, manifestFile: rootManifest }
|
|
42
|
+
}
|
|
43
|
+
return { type: 'unknown', inputDir: projectDir, manifestFile: '' }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} projectDir
|
|
48
|
+
* @returns {Promise<boolean>}
|
|
49
|
+
*/
|
|
50
|
+
export async function isUniappProject(projectDir) {
|
|
51
|
+
return (await detectProject(projectDir)).type !== 'unknown'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 读取项目 manifest:优先生成后的 src/manifest.json,其次已构建产物,最后项目根目录。
|
|
56
|
+
* HBuilderX 的 manifest.json 含注释与末尾逗号,走 JSONC 解析。
|
|
57
|
+
* @param {string} projectDir
|
|
58
|
+
* @returns {Promise<{ manifest: any, file: string, type: ProjectLayout['type'], inputDir: string }>}
|
|
59
|
+
*/
|
|
60
|
+
export async function readProjectManifest(projectDir) {
|
|
61
|
+
const layout = await detectProject(projectDir)
|
|
62
|
+
if (layout.type !== 'unknown') {
|
|
63
|
+
return {
|
|
64
|
+
manifest: await readJsonc(layout.manifestFile),
|
|
65
|
+
file: layout.manifestFile,
|
|
66
|
+
type: layout.type,
|
|
67
|
+
inputDir: layout.inputDir,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const candidates = [
|
|
72
|
+
{ file: path.join(projectDir, 'dist/build/app/manifest.json'), type: 'cli', inputDir: path.join(projectDir, 'src') },
|
|
73
|
+
{
|
|
74
|
+
file: path.join(projectDir, 'unpackage/dist/build/app-plus/manifest.json'),
|
|
75
|
+
type: 'hbuilderx',
|
|
76
|
+
inputDir: projectDir,
|
|
77
|
+
},
|
|
78
|
+
{ file: path.join(projectDir, 'unpackage/dist/build/app/manifest.json'), type: 'hbuilderx', inputDir: projectDir },
|
|
79
|
+
]
|
|
80
|
+
for (const candidate of candidates) {
|
|
81
|
+
if (await exists(candidate.file)) {
|
|
82
|
+
return {
|
|
83
|
+
manifest: await readJson(candidate.file),
|
|
84
|
+
file: candidate.file,
|
|
85
|
+
type: /** @type {ProjectLayout['type']} */ (candidate.type),
|
|
86
|
+
inputDir: candidate.inputDir,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new PackError(`找不到 manifest.json:${projectDir}`, EXIT.CONFIG, {
|
|
92
|
+
hint: '确认目录是 uni-app 项目(CLI 项目有 src/manifest.json,HBuilderX 项目根目录有 manifest.json)',
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* HBuilderX 项目常常不在 manifest.json 里写包名(云打包时单独填),
|
|
98
|
+
* 项目根的 AndroidManifest.xml 是最后可用的兜底来源。
|
|
99
|
+
* HBuilderX 实际打包会把包名转小写,AppKey 又按包名绑定,这里保持一致。
|
|
100
|
+
* @param {string} projectDir
|
|
101
|
+
* @returns {Promise<string>}
|
|
102
|
+
*/
|
|
103
|
+
export async function readProjectPackage(projectDir) {
|
|
104
|
+
const file = path.join(projectDir, 'AndroidManifest.xml')
|
|
105
|
+
if (!(await exists(file))) {
|
|
106
|
+
return ''
|
|
107
|
+
}
|
|
108
|
+
const value = /package="([^"]+)"/.exec(await readText(file))?.[1] ?? ''
|
|
109
|
+
return value.toLowerCase()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* HBuilderX 的 Vue 3 项目会在 manifest 里写 "vueVersion": "3";没有该字段按 Vue 2 处理。
|
|
114
|
+
* @param {any} manifest
|
|
115
|
+
* @returns {2 | 3}
|
|
116
|
+
*/
|
|
117
|
+
export function readVueVersion(manifest) {
|
|
118
|
+
return String(manifest?.vueVersion ?? '').trim() === '3' ? 3 : 2
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* CLI 项目通常不在 manifest 写 vueVersion,改从 package.json 的 vue / @dcloudio 版本推断。
|
|
123
|
+
* @param {string} projectDir
|
|
124
|
+
* @param {any} manifest
|
|
125
|
+
* @param {'cli' | 'hbuilderx' | 'unknown'} type
|
|
126
|
+
* @returns {Promise<2 | 3>}
|
|
127
|
+
*/
|
|
128
|
+
export async function detectVueVersion(projectDir, manifest, type = 'cli') {
|
|
129
|
+
const declared = String(manifest?.vueVersion ?? '').trim()
|
|
130
|
+
if (declared === '2' || declared === '3') {
|
|
131
|
+
return declared === '3' ? 3 : 2
|
|
132
|
+
}
|
|
133
|
+
if (type === 'hbuilderx') {
|
|
134
|
+
return 2
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const packageFile = path.join(projectDir, 'package.json')
|
|
138
|
+
if (await exists(packageFile)) {
|
|
139
|
+
const pkg = await readJson(packageFile).catch(() => ({}))
|
|
140
|
+
const dependencies = { ...(pkg.dependencies ?? {}), ...(pkg.devDependencies ?? {}) }
|
|
141
|
+
const candidates = [dependencies.vue, dependencies['@dcloudio/uni-app']].map((value) => String(value ?? ''))
|
|
142
|
+
if (candidates.some((value) => /^2\./.test(value))) {
|
|
143
|
+
return 2
|
|
144
|
+
}
|
|
145
|
+
if (candidates.some((value) => /^3\./.test(value))) {
|
|
146
|
+
return 3
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return 3
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {...any} values
|
|
154
|
+
*/
|
|
155
|
+
function firstPresent(...values) {
|
|
156
|
+
for (const value of values) {
|
|
157
|
+
if (value !== undefined && value !== null && value !== '') {
|
|
158
|
+
return value
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* DCloud 运行时在安装后才会校验 AppKey,缺配置时只弹「未配置appkey或配置错误」,
|
|
166
|
+
* 在构建前提示可以避免白等一次 Gradle 和安装。
|
|
167
|
+
* @param {string | undefined} appKey
|
|
168
|
+
* @returns {string}
|
|
169
|
+
*/
|
|
170
|
+
export function missingAppKeyHint(appKey) {
|
|
171
|
+
if (String(appKey ?? '').trim()) {
|
|
172
|
+
return ''
|
|
173
|
+
}
|
|
174
|
+
return '未配置 DCloud AppKey:安装后应用会提示「未配置appkey或配置错误」,请重跑 init 并加 --appkey <key>'
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* manifest 是唯一来源,fallbacks 只在该字段缺失时兜底。
|
|
179
|
+
* 配置里的同名字段可能是 init 时的快照,直接覆盖会让改过 manifest 的项目静默打旧版本。
|
|
180
|
+
* @param {any} manifest
|
|
181
|
+
* @param {any} [fallbacks]
|
|
182
|
+
* @param {{ validate?: boolean }} [options]
|
|
183
|
+
* @returns {ProjectManifest}
|
|
184
|
+
*/
|
|
185
|
+
export function normalizeManifest(manifest, fallbacks = {}, options = {}) {
|
|
186
|
+
const distribute = manifest?.['app-plus']?.distribute ?? {}
|
|
187
|
+
const android = distribute.android ?? {}
|
|
188
|
+
const manifestAbi = Array.isArray(android.abiFilters) && android.abiFilters.length > 0 ? android.abiFilters : undefined
|
|
189
|
+
|
|
190
|
+
const permissions = extractPermissionNames(android.permissions ?? [])
|
|
191
|
+
const features = extractFeatureNames(android.permissions ?? [])
|
|
192
|
+
|
|
193
|
+
const result = {
|
|
194
|
+
appid: String(firstPresent(manifest?.appid, fallbacks.appid) ?? ''),
|
|
195
|
+
name: String(firstPresent(manifest?.name, fallbacks.name) ?? ''),
|
|
196
|
+
versionName: String(firstPresent(manifest?.versionName, fallbacks.versionName) ?? '1.0.0'),
|
|
197
|
+
versionCode: Number(firstPresent(manifest?.versionCode, manifest?.version?.code, fallbacks.versionCode) ?? 1),
|
|
198
|
+
packageName: String(firstPresent(android.packagename, fallbacks.package) ?? ''),
|
|
199
|
+
abiFilters: firstPresent(manifestAbi, fallbacks.abiFilters, ['armeabi-v7a', 'arm64-v8a']),
|
|
200
|
+
minSdkVersion: Number(firstPresent(android.minSdkVersion, fallbacks.minSdkVersion, 21)),
|
|
201
|
+
targetSdkVersion: Number(firstPresent(android.targetSdkVersion, fallbacks.targetSdkVersion, 30)),
|
|
202
|
+
permissions,
|
|
203
|
+
features,
|
|
204
|
+
modules: Object.keys(manifest?.['app-plus']?.modules ?? {}),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (options.validate !== false) {
|
|
208
|
+
validateManifest(result)
|
|
209
|
+
}
|
|
210
|
+
return result
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {ProjectManifest} manifest
|
|
215
|
+
*/
|
|
216
|
+
function validateManifest(manifest) {
|
|
217
|
+
if (!/^__UNI__[0-9A-F]+$/i.test(manifest.appid)) {
|
|
218
|
+
throw new PackError(`appid 不合法:${manifest.appid || '(空)'},应为 __UNI__XXXXXX`, EXIT.CONFIG)
|
|
219
|
+
}
|
|
220
|
+
if (!/^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$/i.test(manifest.packageName)) {
|
|
221
|
+
throw new PackError(`包名不合法:${manifest.packageName || '(空)'}`, EXIT.CONFIG, {
|
|
222
|
+
hint: '在 manifest.json 的 app-plus.distribute.android.packagename 填写,或 init 时用 --package 指定',
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
if (!Number.isInteger(manifest.versionCode) || manifest.versionCode <= 0) {
|
|
226
|
+
throw new PackError(`versionCode 不合法:${manifest.versionCode}`, EXIT.CONFIG)
|
|
227
|
+
}
|
|
228
|
+
if (!manifest.versionName) {
|
|
229
|
+
throw new PackError('versionName 为空', EXIT.CONFIG)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @param {string[]} entries
|
|
235
|
+
* @returns {string[]}
|
|
236
|
+
*/
|
|
237
|
+
export function extractPermissionNames(entries) {
|
|
238
|
+
/** @type {string[]} */
|
|
239
|
+
const names = []
|
|
240
|
+
for (const entry of entries) {
|
|
241
|
+
const match = /<uses-permission[^>]*android:name="([^"]+)"/.exec(entry)
|
|
242
|
+
if (match) {
|
|
243
|
+
names.push(match[1])
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return names
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param {string[]} entries
|
|
251
|
+
* @returns {string[]} 形如 "android.hardware.touchscreen|false"
|
|
252
|
+
*/
|
|
253
|
+
export function extractFeatureNames(entries) {
|
|
254
|
+
/** @type {string[]} */
|
|
255
|
+
const features = []
|
|
256
|
+
for (const entry of entries) {
|
|
257
|
+
const match = /<uses-feature[^>]*android:name="([^"]+)"[^>]*android:required="(true|false)"/.exec(entry)
|
|
258
|
+
if (match) {
|
|
259
|
+
features.push(`${match[1]}|${match[2]}`)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return features
|
|
263
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { readJson } from './fsx.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 前端产物 manifest 里的版本字段(构建产物里是 version.name / version.code)。
|
|
7
|
+
* @typedef {Object} ResourceVersion
|
|
8
|
+
* @property {string} versionName
|
|
9
|
+
* @property {number} versionCode
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {any} manifest 前端产物的 manifest.json
|
|
14
|
+
* @returns {ResourceVersion}
|
|
15
|
+
*/
|
|
16
|
+
export function readResourceVersion(manifest) {
|
|
17
|
+
const version = manifest?.version ?? {}
|
|
18
|
+
return {
|
|
19
|
+
versionName: String(version.name ?? ''),
|
|
20
|
+
versionCode: Number(version.code ?? 0),
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 前端产物只是 dist 里的一份静态资源:bump 版本后如果没重新编译(或用了 --skip-web),
|
|
26
|
+
* 会打出「新版本号 + 旧资源」,签名与体积检查都发现不了,只能靠版本一致性闸门。
|
|
27
|
+
* @param {ResourceVersion} resource
|
|
28
|
+
* @param {{ versionName: string, versionCode: number }} expected 项目 manifest 解析出的版本
|
|
29
|
+
* @returns {{ ok: boolean, message: string }}
|
|
30
|
+
*/
|
|
31
|
+
export function compareResourceVersion(resource, expected) {
|
|
32
|
+
const versionName = String(expected.versionName ?? '')
|
|
33
|
+
const versionCode = Number(expected.versionCode ?? 0)
|
|
34
|
+
const ok = resource.versionName === versionName && resource.versionCode === versionCode
|
|
35
|
+
return {
|
|
36
|
+
ok,
|
|
37
|
+
message: ok
|
|
38
|
+
? ''
|
|
39
|
+
: `前端产物版本 ${resource.versionName || '(空)'}(${resource.versionCode}) 与项目 manifest 版本 ${versionName}(${versionCode}) 不一致`,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 版本不一致时给出可执行的修复方向。
|
|
45
|
+
* @param {string} resourcesDir
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
48
|
+
function mismatchHint(resourcesDir) {
|
|
49
|
+
return [
|
|
50
|
+
'前端资源是旧版本,继续打包会得到「新版本号 + 旧资源」的 APK/wgt。',
|
|
51
|
+
`要么重新构建:pnpm build:app-android(产物写入 ${resourcesDir})`,
|
|
52
|
+
'要么确认 dist 已是最新后再用 --skip-web;确实要跳过校验加 --skip-resource-check。',
|
|
53
|
+
].join('\n')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 读取 <resourcesDir>/manifest.json 并断言版本与项目 manifest 一致。
|
|
58
|
+
* @param {Object} options
|
|
59
|
+
* @param {string} options.resourcesDir
|
|
60
|
+
* @param {{ versionName: string, versionCode: number }} options.expected
|
|
61
|
+
* @param {boolean} [options.skip]
|
|
62
|
+
* @param {import('./logger.js').Logger} options.logger
|
|
63
|
+
* @returns {Promise<ResourceVersion & { skipped: boolean }>}
|
|
64
|
+
*/
|
|
65
|
+
export async function assertResourceVersion(options) {
|
|
66
|
+
const manifestFile = path.join(options.resourcesDir, 'manifest.json')
|
|
67
|
+
let manifest
|
|
68
|
+
try {
|
|
69
|
+
manifest = await readJson(manifestFile)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new PackError(`无法读取前端产物 manifest:${manifestFile}`, EXIT.WEB_BUILD, {
|
|
72
|
+
hint: '确认前端产物目录完整,或去掉 --skip-web 重新构建',
|
|
73
|
+
cause: error,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const resource = readResourceVersion(manifest)
|
|
78
|
+
const result = compareResourceVersion(resource, options.expected)
|
|
79
|
+
if (result.ok) {
|
|
80
|
+
return { ...resource, skipped: false }
|
|
81
|
+
}
|
|
82
|
+
if (options.skip) {
|
|
83
|
+
options.logger.warn(`已跳过资源版本校验:${result.message}`)
|
|
84
|
+
return { ...resource, skipped: true }
|
|
85
|
+
}
|
|
86
|
+
throw new PackError(result.message, EXIT.WEB_BUILD, { hint: mismatchHint(options.resourcesDir) })
|
|
87
|
+
}
|