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,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,77 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { PackError, EXIT } from './errors.js'
|
|
3
|
+
import { ensureDir, exists, readJson } from './fsx.js'
|
|
4
|
+
import { runHbuilderxBuild } from './hbuilderx.js'
|
|
5
|
+
import { run } from './process.js'
|
|
6
|
+
import { buildTempEnv } from './workdir.js'
|
|
7
|
+
|
|
8
|
+
const REQUIRED = ['manifest.json', 'app-service.js', 'app-config.js', '__uniappview.html']
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {any} project
|
|
12
|
+
* @param {import('./logger.js').Logger} logger
|
|
13
|
+
* @param {{ hbuilderx?: { root: string, version: string }, vueVersion?: 2 | 3, tempDir?: string }} [context]
|
|
14
|
+
* @returns {Promise<{ dir: string, manifest: any }>}
|
|
15
|
+
*/
|
|
16
|
+
export async function runWebBuild(project, logger, context = {}) {
|
|
17
|
+
const config = project.webBuild ?? {}
|
|
18
|
+
const dir = path.resolve(
|
|
19
|
+
project.projectDir,
|
|
20
|
+
String(config.outputDir ?? (project.type === 'hbuilderx' ? 'unpackage/dist/build/app-plus' : 'dist/build/app')),
|
|
21
|
+
)
|
|
22
|
+
const tempDir = context.tempDir ? String(context.tempDir) : ''
|
|
23
|
+
if (tempDir) {
|
|
24
|
+
// 先建好目录,再把它作为子进程的 TEMP/TMP,避免系统 TEMP 的 ACL 问题
|
|
25
|
+
await ensureDir(tempDir)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (project.type === 'hbuilderx') {
|
|
29
|
+
const hbuilderx = context.hbuilderx
|
|
30
|
+
if (!hbuilderx) {
|
|
31
|
+
throw new PackError('找不到 HBuilderX,无法编译 HBuilderX 项目', EXIT.WEB_BUILD, {
|
|
32
|
+
hint: '运行 uni-pack init --hbuilderx <HBuilderX 目录>,或在配置 hbuilderx.root 里填写安装路径',
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
await runHbuilderxBuild({
|
|
36
|
+
root: hbuilderx.root,
|
|
37
|
+
version: hbuilderx.version,
|
|
38
|
+
inputDir: String(project.inputDir ?? project.projectDir),
|
|
39
|
+
outputDir: dir,
|
|
40
|
+
vueVersion: context.vueVersion ?? 3,
|
|
41
|
+
tempDir: tempDir || undefined,
|
|
42
|
+
logger,
|
|
43
|
+
})
|
|
44
|
+
} else {
|
|
45
|
+
const command = String(config.command ?? 'pnpm')
|
|
46
|
+
const args = Array.isArray(config.args) ? config.args : ['run', 'build:app-android']
|
|
47
|
+
|
|
48
|
+
logger.step(`构建前端资源:${command} ${args.join(' ')}`)
|
|
49
|
+
try {
|
|
50
|
+
await run(command, args, {
|
|
51
|
+
cwd: project.projectDir,
|
|
52
|
+
env: tempDir ? buildTempEnv(tempDir) : undefined,
|
|
53
|
+
onLine: (line) => logger.detail(line),
|
|
54
|
+
})
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new PackError('前端构建失败', EXIT.WEB_BUILD, { cause: error })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!(await exists(dir))) {
|
|
61
|
+
throw new PackError(`前端产物目录不存在:${dir}`, EXIT.WEB_BUILD)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const missing = []
|
|
65
|
+
for (const file of REQUIRED) {
|
|
66
|
+
if (!(await exists(path.join(dir, file)))) {
|
|
67
|
+
missing.push(file)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (missing.length > 0) {
|
|
71
|
+
throw new PackError(`前端产物不完整,缺少:${missing.join(', ')}`, EXIT.WEB_BUILD)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const manifest = await readJson(path.join(dir, 'manifest.json'))
|
|
75
|
+
logger.success(`前端资源就绪:${manifest.id} ${manifest.version?.name ?? ''}`)
|
|
76
|
+
return { dir, manifest }
|
|
77
|
+
}
|
package/src/core/wgt.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { createWriteStream } from 'node:fs'
|
|
3
|
+
import { readdir, stat } from 'node:fs/promises'
|
|
4
|
+
import { pipeline } from 'node:stream/promises'
|
|
5
|
+
import yazl from 'yazl'
|
|
6
|
+
import { PackError, EXIT } from './errors.js'
|
|
7
|
+
import { ensureDir, exists, readJson, replaceHardLink, writeJson } from './fsx.js'
|
|
8
|
+
import { fileHash } from './hash.js'
|
|
9
|
+
|
|
10
|
+
const REQUIRED = ['manifest.json', 'app-service.js', '__uniappview.html']
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 产物基名:<appid>-<versionName>。版本名里可能有斜杠等字符,统一替换成下划线。
|
|
14
|
+
* @param {{ appid: string, versionName: string }} options
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
export function wgtBaseName(options) {
|
|
18
|
+
const appid = String(options.appid ?? '').trim()
|
|
19
|
+
const version = String(options.versionName ?? '')
|
|
20
|
+
.trim()
|
|
21
|
+
.replace(/[^\w.-]+/g, '_')
|
|
22
|
+
.replace(/^_+|_+$/g, '')
|
|
23
|
+
return version ? `${appid}-${version}` : appid
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {Object} input
|
|
28
|
+
* @param {string} input.resourcesDir
|
|
29
|
+
* @param {string} input.outputDir
|
|
30
|
+
* @param {string} input.appid
|
|
31
|
+
* @param {string} input.versionName
|
|
32
|
+
* @param {number} input.versionCode
|
|
33
|
+
* @returns {Promise<{ file: string, meta: string, latest: string, sha256: string, size: number, manifest: any }>}
|
|
34
|
+
*/
|
|
35
|
+
export async function writeWgt(input) {
|
|
36
|
+
const manifest = await readJson(path.join(input.resourcesDir, 'manifest.json'))
|
|
37
|
+
const missing = []
|
|
38
|
+
for (const file of REQUIRED) {
|
|
39
|
+
if (!(await exists(path.join(input.resourcesDir, file)))) {
|
|
40
|
+
missing.push(file)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (missing.length > 0) {
|
|
44
|
+
throw new PackError(`wgt 缺少文件:${missing.join(', ')}`, EXIT.WGT)
|
|
45
|
+
}
|
|
46
|
+
if (!manifest?.id) {
|
|
47
|
+
throw new PackError('wgt 的 manifest.json 缺少 id', EXIT.WGT)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const baseName = wgtBaseName({ appid: input.appid, versionName: input.versionName })
|
|
51
|
+
const outputFile = path.join(input.outputDir, `${baseName}.wgt`)
|
|
52
|
+
const metaFile = path.join(input.outputDir, `${baseName}.json`)
|
|
53
|
+
const latest = path.join(input.outputDir, `${input.appid}.wgt`)
|
|
54
|
+
|
|
55
|
+
await ensureDir(input.outputDir)
|
|
56
|
+
const zip = new yazl.ZipFile()
|
|
57
|
+
await addDirectory(zip, input.resourcesDir, '')
|
|
58
|
+
zip.end()
|
|
59
|
+
await pipeline(zip.outputStream, createWriteStream(outputFile))
|
|
60
|
+
|
|
61
|
+
const size = (await stat(outputFile)).size
|
|
62
|
+
if (size < 1024 * 1024) {
|
|
63
|
+
throw new PackError(`wgt 体积异常(${size} 字节),检查资源目录是否完整`, EXIT.WGT)
|
|
64
|
+
}
|
|
65
|
+
const sha256 = await fileHash(outputFile)
|
|
66
|
+
|
|
67
|
+
await writeJson(metaFile, {
|
|
68
|
+
appid: input.appid,
|
|
69
|
+
versionName: input.versionName,
|
|
70
|
+
versionCode: input.versionCode,
|
|
71
|
+
file: path.basename(outputFile),
|
|
72
|
+
size,
|
|
73
|
+
sha256,
|
|
74
|
+
builtAt: new Date().toISOString(),
|
|
75
|
+
})
|
|
76
|
+
// 固定名保持旧路径可用:硬链到最新版本,零复制且不会被覆盖
|
|
77
|
+
await replaceHardLink(outputFile, latest)
|
|
78
|
+
|
|
79
|
+
return { file: outputFile, meta: metaFile, latest, sha256, size, manifest }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {yazl.ZipFile} zip
|
|
84
|
+
* @param {string} dir
|
|
85
|
+
* @param {string} prefix
|
|
86
|
+
*/
|
|
87
|
+
async function addDirectory(zip, dir, prefix) {
|
|
88
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
89
|
+
for (const entry of entries) {
|
|
90
|
+
const absolute = path.join(dir, entry.name)
|
|
91
|
+
const relative = prefix ? `${prefix}/${entry.name}` : entry.name
|
|
92
|
+
if (entry.isDirectory()) {
|
|
93
|
+
await addDirectory(zip, absolute, relative)
|
|
94
|
+
} else {
|
|
95
|
+
zip.addFile(absolute, relative)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 项目名可能是中文或带空格,工作目录用清洗后的名字,和旧版本保持一致。
|
|
5
|
+
* @param {unknown} value
|
|
6
|
+
* @returns {string}
|
|
7
|
+
*/
|
|
8
|
+
export function sanitizeWorkName(value) {
|
|
9
|
+
return String(value ?? '').replace(/[^\w.-]+/g, '-').replace(/^-+|-+$/g, '') || 'project'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 每个项目一个稳定的构建工作目录:<workDir>/<项目名>。
|
|
14
|
+
* @param {any} config
|
|
15
|
+
* @param {any} project
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
export function resolveWorkRoot(config, project) {
|
|
19
|
+
return path.join(String(config?.workDir ?? ''), sanitizeWorkName(project?.name ?? 'project'))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 前端构建(pnpm / HBuilderX 编译器)的临时目录。
|
|
24
|
+
* 默认 <workRoot>/tmp:某些机器的系统 TEMP 有 ACL 问题,esbuild 删除临时文件会被拒绝。
|
|
25
|
+
* 用户显式设置 UNI_PACK_TEMP 时不覆盖。
|
|
26
|
+
* @param {string} workRoot
|
|
27
|
+
* @param {Record<string, string | undefined>} [env]
|
|
28
|
+
* @returns {{ tempDir: string, explicit: boolean }}
|
|
29
|
+
*/
|
|
30
|
+
export function resolveBuildTempDir(workRoot, env = process.env) {
|
|
31
|
+
const explicit = String(env.UNI_PACK_TEMP ?? '').trim()
|
|
32
|
+
if (explicit) {
|
|
33
|
+
return { tempDir: explicit, explicit: true }
|
|
34
|
+
}
|
|
35
|
+
return { tempDir: path.join(workRoot, 'tmp'), explicit: false }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} tempDir
|
|
40
|
+
* @returns {{ TEMP: string, TMP: string }}
|
|
41
|
+
*/
|
|
42
|
+
export function buildTempEnv(tempDir) {
|
|
43
|
+
return { TEMP: tempDir, TMP: tempDir }
|
|
44
|
+
}
|