uniapp-android-cli 0.0.0-stage → 0.1.0
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 +595 -2
- package/bin/uni-pack.js +7 -0
- package/package.json +66 -4
- package/src/cli.js +184 -0
- package/src/commands/build.js +42 -0
- package/src/commands/debug.js +83 -0
- package/src/commands/device.js +303 -0
- package/src/commands/devices.js +23 -0
- package/src/commands/doctor.js +160 -0
- package/src/commands/init.js +277 -0
- package/src/commands/wgt.js +46 -0
- package/src/core/adb.js +234 -0
- package/src/core/build.js +374 -0
- package/src/core/config.js +157 -0
- package/src/core/devicePairing.js +152 -0
- package/src/core/errors.js +36 -0
- package/src/core/fsx.js +172 -0
- package/src/core/gradle.js +75 -0
- package/src/core/hbuilderx.js +258 -0
- package/src/core/logger.js +82 -0
- package/src/core/output.js +74 -0
- package/src/core/patch/assets.js +154 -0
- package/src/core/patch/gradle.js +128 -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 +57 -0
- package/src/core/patch/utsPlugins.js +424 -0
- package/src/core/process.js +71 -0
- package/src/core/project.js +250 -0
- package/src/core/qrcode.js +13 -0
- package/src/core/sdk.js +398 -0
- package/src/core/signing.js +84 -0
- package/src/core/version.js +128 -0
- package/src/core/webBuild.js +69 -0
- package/src/core/wgt.js +76 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { exists, readText } from './fsx.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Signing
|
|
7
|
+
* @property {string} keystore
|
|
8
|
+
* @property {string} keyAlias
|
|
9
|
+
* @property {string} storePassword
|
|
10
|
+
* @property {string} keyPassword
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 密码来源优先级:命令行 → 环境变量 → keystore.properties。
|
|
15
|
+
* @param {any} project
|
|
16
|
+
* @param {{ storePassword?: string, keyPassword?: string }} options
|
|
17
|
+
* @returns {Promise<Signing>}
|
|
18
|
+
*/
|
|
19
|
+
export async function resolveSigning(project, options = {}) {
|
|
20
|
+
const signing = project.signing ?? {}
|
|
21
|
+
const keystore = String(signing.keystore ?? '')
|
|
22
|
+
const keyAlias = String(signing.keyAlias ?? '')
|
|
23
|
+
if (!keystore || !(await exists(keystore))) {
|
|
24
|
+
throw new PackError(`找不到签名证书:${keystore || '(未配置)'}`, EXIT.SIGN)
|
|
25
|
+
}
|
|
26
|
+
if (!keyAlias) {
|
|
27
|
+
throw new PackError('未配置签名别名 keyAlias', EXIT.SIGN)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const properties = await readProperties(signing.keystoreProperties, project.projectDir)
|
|
31
|
+
|
|
32
|
+
const storePassword =
|
|
33
|
+
options.storePassword ??
|
|
34
|
+
envValue(signing.storePasswordEnv) ??
|
|
35
|
+
properties.storePassword ??
|
|
36
|
+
''
|
|
37
|
+
const keyPassword =
|
|
38
|
+
options.keyPassword ??
|
|
39
|
+
envValue(signing.keyPasswordEnv) ??
|
|
40
|
+
properties.keyPassword ??
|
|
41
|
+
storePassword
|
|
42
|
+
|
|
43
|
+
if (!storePassword || !keyPassword) {
|
|
44
|
+
throw new PackError('缺少签名密码', EXIT.SIGN, {
|
|
45
|
+
hint: '设置环境变量(如 PRODIS_STORE_PASS / PRODIS_KEY_PASS),或在配置里指定 keystoreProperties',
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { keystore, keyAlias, storePassword, keyPassword }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {string | undefined} name
|
|
54
|
+
*/
|
|
55
|
+
function envValue(name) {
|
|
56
|
+
if (!name) {
|
|
57
|
+
return undefined
|
|
58
|
+
}
|
|
59
|
+
return process.env[name] || undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {string | undefined} file
|
|
64
|
+
* @param {string} projectDir
|
|
65
|
+
*/
|
|
66
|
+
async function readProperties(file, projectDir) {
|
|
67
|
+
if (!file) {
|
|
68
|
+
return {}
|
|
69
|
+
}
|
|
70
|
+
const absolute = path.isAbsolute(file) ? file : path.join(projectDir, file)
|
|
71
|
+
if (!(await exists(absolute))) {
|
|
72
|
+
return {}
|
|
73
|
+
}
|
|
74
|
+
const text = await readText(absolute)
|
|
75
|
+
/** @type {Record<string, string>} */
|
|
76
|
+
const result = {}
|
|
77
|
+
for (const line of text.split(/\r?\n/)) {
|
|
78
|
+
const match = /^\s*([\w.]+)\s*=\s*(.*)\s*$/.exec(line)
|
|
79
|
+
if (match) {
|
|
80
|
+
result[match[1]] = match[2]
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return result
|
|
84
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { exists, readJson, readText } from './fsx.js'
|
|
4
|
+
import { normalizeHbuilderxVersion, readLastBuiltVersion } from './hbuilderx.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 把 uni-app npm 版本换算成 HBuilderX 版本号。
|
|
8
|
+
* 3.0.0-5030420261029002 -> 5.34(50304 -> 5.3.4)
|
|
9
|
+
* 3.0.0-4080520251106001 -> 4.85(40805 -> 4.8.5)
|
|
10
|
+
* @param {string} version
|
|
11
|
+
* @returns {string | null}
|
|
12
|
+
*/
|
|
13
|
+
export function decodeCompilerVersion(version) {
|
|
14
|
+
const match = /^\d+\.\d+\.\d+-(\d{5})\d+$/.exec(version)
|
|
15
|
+
if (!match) {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
const code = match[1]
|
|
19
|
+
const major = code[0]
|
|
20
|
+
const minor = Number(code.slice(1, 3))
|
|
21
|
+
const patch = Number(code.slice(3, 5))
|
|
22
|
+
return `${major}.${minor}${patch}`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} projectDir
|
|
27
|
+
* @returns {Promise<string | null>}
|
|
28
|
+
*/
|
|
29
|
+
export async function readProjectCompilerVersion(projectDir) {
|
|
30
|
+
const file = path.join(projectDir, 'node_modules/@dcloudio/uni-app/package.json')
|
|
31
|
+
if (!(await exists(file))) {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
const pkg = await readJson(file)
|
|
35
|
+
return decodeCompilerVersion(String(pkg.version ?? ''))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* CLI 项目跟随 @dcloudio 编译器;HBuilderX 项目跟随 HBuilderX 自身版本。
|
|
40
|
+
* @param {{ type?: string, projectDir: string }} project
|
|
41
|
+
* @param {{ hbuilderxRoot?: string }} [options]
|
|
42
|
+
* @returns {Promise<{ version: string | null, source: string }>}
|
|
43
|
+
*/
|
|
44
|
+
export async function readCompilerVersion(project, options = {}) {
|
|
45
|
+
if (project.type === 'hbuilderx') {
|
|
46
|
+
if (options.hbuilderxRoot) {
|
|
47
|
+
const about = path.join(options.hbuilderxRoot, 'plugins', 'about', 'package.json')
|
|
48
|
+
if (await exists(about)) {
|
|
49
|
+
const pkg = await readJson(about).catch(() => ({}))
|
|
50
|
+
return { version: normalizeHbuilderxVersion(pkg.version), source: `HBuilderX ${options.hbuilderxRoot}` }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const last = await readLastBuiltVersion(project.projectDir)
|
|
54
|
+
return { version: last || null, source: last ? '上次构建产物' : '未知(未找到 HBuilderX)' }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const version = await readProjectCompilerVersion(project.projectDir)
|
|
58
|
+
return {
|
|
59
|
+
version,
|
|
60
|
+
source: version ? '@dcloudio/uni-app' : '未知(缺少 node_modules/@dcloudio/uni-app)',
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* SDK 目录名形如 Android-SDK@5.34.82692_20261030。
|
|
66
|
+
* @param {string} sdkRoot
|
|
67
|
+
* @returns {Promise<string | null>}
|
|
68
|
+
*/
|
|
69
|
+
export async function readSdkVersion(sdkRoot) {
|
|
70
|
+
const match = /Android-SDK@(\d+\.\d+)/i.exec(path.basename(sdkRoot))
|
|
71
|
+
if (match) {
|
|
72
|
+
return match[1]
|
|
73
|
+
}
|
|
74
|
+
const readme = path.join(sdkRoot, 'readme/readme.md')
|
|
75
|
+
if (await exists(readme)) {
|
|
76
|
+
const text = await readText(readme)
|
|
77
|
+
const inline = /(\d+\.\d+)\.\d+/.exec(text)
|
|
78
|
+
if (inline) {
|
|
79
|
+
return inline[1]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 编译版本与离线 SDK 版本必须一致,否则运行时会提示版本不匹配甚至异常。
|
|
87
|
+
* @param {{ compiler: string | null, sdk: string | null }} versions
|
|
88
|
+
*/
|
|
89
|
+
export function assertVersionMatch(versions) {
|
|
90
|
+
if (!versions.compiler || !versions.sdk) {
|
|
91
|
+
return { ok: false, unknown: true }
|
|
92
|
+
}
|
|
93
|
+
return { ok: versions.compiler === versions.sdk, unknown: false }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {{ compiler: string | null, sdk: string | null }} versions
|
|
98
|
+
* @param {{ type?: string }} [options]
|
|
99
|
+
*/
|
|
100
|
+
export function versionMismatchHint(versions, options = {}) {
|
|
101
|
+
const lines =
|
|
102
|
+
options.type === 'hbuilderx'
|
|
103
|
+
? [
|
|
104
|
+
'HBuilderX 项目的版本由 HBuilderX 决定,两边必须一致,二选一:',
|
|
105
|
+
` 1) 安装与离线 SDK ${versions.sdk ?? '?'} 对应的 HBuilderX 版本`,
|
|
106
|
+
` 2) 把 sdk.root 指向与 HBuilderX ${versions.compiler ?? '?'} 对应的 Android-SDK@${versions.compiler ?? 'x.yy'}`,
|
|
107
|
+
'确认后重新执行 uni-pack doctor',
|
|
108
|
+
]
|
|
109
|
+
: [
|
|
110
|
+
'两边必须一致,二选一:',
|
|
111
|
+
` 1) 升级项目依赖到 SDK 对应版本:npx @dcloudio/uvm@latest`,
|
|
112
|
+
` 2) 下载与当前编译版本 ${versions.compiler ?? '?'} 匹配的 Android 离线 SDK,并在配置 sdk.root 指向它`,
|
|
113
|
+
'确认后重新执行 uni-pack doctor',
|
|
114
|
+
]
|
|
115
|
+
return lines.join('\n')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param {{ compiler: string | null, sdk: string | null }} versions
|
|
120
|
+
* @param {{ type?: string }} [options]
|
|
121
|
+
*/
|
|
122
|
+
export function versionMismatchError(versions, options = {}) {
|
|
123
|
+
return new PackError(
|
|
124
|
+
`uni-app 编译版本 ${versions.compiler ?? '未知'} 与离线 SDK ${versions.sdk ?? '未知'} 不一致`,
|
|
125
|
+
EXIT.PREFLIGHT,
|
|
126
|
+
{ hint: versionMismatchHint(versions, options) },
|
|
127
|
+
)
|
|
128
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { exists, readJson } from './fsx.js'
|
|
4
|
+
import { runHbuilderxBuild } from './hbuilderx.js'
|
|
5
|
+
import { run } from './process.js'
|
|
6
|
+
|
|
7
|
+
const REQUIRED = ['manifest.json', 'app-service.js', 'app-config.js', '__uniappview.html']
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {any} project
|
|
11
|
+
* @param {import('./logger.js').Logger} logger
|
|
12
|
+
* @param {{ hbuilderx?: { root: string, version: string }, vueVersion?: 2 | 3 }} [context]
|
|
13
|
+
* @returns {Promise<{ dir: string, manifest: any }>}
|
|
14
|
+
*/
|
|
15
|
+
export async function runWebBuild(project, logger, context = {}) {
|
|
16
|
+
const config = project.webBuild ?? {}
|
|
17
|
+
const dir = path.resolve(
|
|
18
|
+
project.projectDir,
|
|
19
|
+
String(config.outputDir ?? (project.type === 'hbuilderx' ? 'unpackage/dist/build/app-plus' : 'dist/build/app')),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
if (project.type === 'hbuilderx') {
|
|
23
|
+
const hbuilderx = context.hbuilderx
|
|
24
|
+
if (!hbuilderx) {
|
|
25
|
+
throw new PackError('找不到 HBuilderX,无法编译 HBuilderX 项目', EXIT.WEB_BUILD, {
|
|
26
|
+
hint: '运行 uni-pack init --hbuilderx <HBuilderX 目录>,或在配置 hbuilderx.root 里填写安装路径',
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
await runHbuilderxBuild({
|
|
30
|
+
root: hbuilderx.root,
|
|
31
|
+
version: hbuilderx.version,
|
|
32
|
+
inputDir: String(project.inputDir ?? project.projectDir),
|
|
33
|
+
outputDir: dir,
|
|
34
|
+
vueVersion: context.vueVersion ?? 3,
|
|
35
|
+
logger,
|
|
36
|
+
})
|
|
37
|
+
} else {
|
|
38
|
+
const command = String(config.command ?? 'pnpm')
|
|
39
|
+
const args = Array.isArray(config.args) ? config.args : ['run', 'build:app-android']
|
|
40
|
+
|
|
41
|
+
logger.step(`构建前端资源:${command} ${args.join(' ')}`)
|
|
42
|
+
try {
|
|
43
|
+
await run(command, args, {
|
|
44
|
+
cwd: project.projectDir,
|
|
45
|
+
onLine: (line) => logger.detail(line),
|
|
46
|
+
})
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new PackError('前端构建失败', EXIT.WEB_BUILD, { cause: error })
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!(await exists(dir))) {
|
|
53
|
+
throw new PackError(`前端产物目录不存在:${dir}`, EXIT.WEB_BUILD)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const missing = []
|
|
57
|
+
for (const file of REQUIRED) {
|
|
58
|
+
if (!(await exists(path.join(dir, file)))) {
|
|
59
|
+
missing.push(file)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (missing.length > 0) {
|
|
63
|
+
throw new PackError(`前端产物不完整,缺少:${missing.join(', ')}`, EXIT.WEB_BUILD)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const manifest = await readJson(path.join(dir, 'manifest.json'))
|
|
67
|
+
logger.success(`前端资源就绪:${manifest.id} ${manifest.version?.name ?? ''}`)
|
|
68
|
+
return { dir, manifest }
|
|
69
|
+
}
|
package/src/core/wgt.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { createReadStream, createWriteStream } from 'node:fs'
|
|
3
|
+
import { readdir, readFile } from 'node:fs/promises'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { pipeline } from 'node:stream/promises'
|
|
6
|
+
import yazl from 'yazl'
|
|
7
|
+
import { PackError, EXIT } from './errors.js'
|
|
8
|
+
import { ensureDir, exists, readJson } from './fsx.js'
|
|
9
|
+
|
|
10
|
+
const REQUIRED = ['manifest.json', 'app-service.js', '__uniappview.html']
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {Object} input
|
|
14
|
+
* @param {string} input.resourcesDir
|
|
15
|
+
* @param {string} input.outputFile
|
|
16
|
+
*/
|
|
17
|
+
export async function writeWgt(input) {
|
|
18
|
+
const manifest = await readJson(path.join(input.resourcesDir, 'manifest.json'))
|
|
19
|
+
const missing = []
|
|
20
|
+
for (const file of REQUIRED) {
|
|
21
|
+
if (!(await exists(path.join(input.resourcesDir, file)))) {
|
|
22
|
+
missing.push(file)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (missing.length > 0) {
|
|
26
|
+
throw new PackError(`wgt 缺少文件:${missing.join(', ')}`, EXIT.WGT)
|
|
27
|
+
}
|
|
28
|
+
if (!manifest?.id) {
|
|
29
|
+
throw new PackError('wgt 的 manifest.json 缺少 id', EXIT.WGT)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await ensureDir(path.dirname(input.outputFile))
|
|
33
|
+
const zip = new yazl.ZipFile()
|
|
34
|
+
await addDirectory(zip, input.resourcesDir, '')
|
|
35
|
+
zip.end()
|
|
36
|
+
await pipeline(zip.outputStream, createWriteStream(input.outputFile))
|
|
37
|
+
|
|
38
|
+
const buffer = await readFile(input.outputFile)
|
|
39
|
+
const sha256 = createHash('sha256').update(buffer).digest('hex')
|
|
40
|
+
if (buffer.length < 1024 * 1024) {
|
|
41
|
+
throw new PackError(`wgt 体积异常(${buffer.length} 字节),检查资源目录是否完整`, EXIT.WGT)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { file: input.outputFile, sha256, size: buffer.length, manifest }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {yazl.ZipFile} zip
|
|
49
|
+
* @param {string} dir
|
|
50
|
+
* @param {string} prefix
|
|
51
|
+
*/
|
|
52
|
+
async function addDirectory(zip, dir, prefix) {
|
|
53
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
54
|
+
for (const entry of entries) {
|
|
55
|
+
const absolute = path.join(dir, entry.name)
|
|
56
|
+
const relative = prefix ? `${prefix}/${entry.name}` : entry.name
|
|
57
|
+
if (entry.isDirectory()) {
|
|
58
|
+
await addDirectory(zip, absolute, relative)
|
|
59
|
+
} else {
|
|
60
|
+
zip.addFile(absolute, relative)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} file
|
|
67
|
+
*/
|
|
68
|
+
export function fileHash(file) {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
const sha = createHash('sha256')
|
|
71
|
+
createReadStream(file)
|
|
72
|
+
.on('data', (chunk) => sha.update(chunk))
|
|
73
|
+
.on('error', reject)
|
|
74
|
+
.on('end', () => resolve(sha.digest('hex')))
|
|
75
|
+
})
|
|
76
|
+
}
|