uniapp-android-cli 0.1.0 → 0.1.3
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 +246 -450
- 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 +91 -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/assets.js +63 -32
- 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
package/src/core/process.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
+
import { StringDecoder } from 'node:string_decoder'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* @typedef {Object} RunOptions
|
|
@@ -7,9 +8,41 @@ import path from 'node:path'
|
|
|
7
8
|
* @property {Record<string, string | undefined>} [env]
|
|
8
9
|
* @property {boolean} [allowFailure]
|
|
9
10
|
* @property {(line: string) => void} [onLine]
|
|
11
|
+
* @property {(chunk: string, stream: 'stdout' | 'stderr') => void} [onChunk]
|
|
10
12
|
* @property {'inherit' | 'pipe'} [stdio]
|
|
11
13
|
*/
|
|
12
14
|
|
|
15
|
+
/**
|
|
16
|
+
* 子进程的 chunk 边界可能落在一行中间(尤其 Gradle 输出被 JVM 分块写出),
|
|
17
|
+
* 这里按 \n 缓冲残行,flush 时把最后一段不完整的行也交出去。
|
|
18
|
+
* @param {(line: string) => void} onLine
|
|
19
|
+
* @returns {{ push: (chunk: string) => void, flush: () => void }}
|
|
20
|
+
*/
|
|
21
|
+
export function createLineSplitter(onLine) {
|
|
22
|
+
let pending = ''
|
|
23
|
+
return {
|
|
24
|
+
push(chunk) {
|
|
25
|
+
pending += chunk
|
|
26
|
+
let index = pending.indexOf('\n')
|
|
27
|
+
while (index >= 0) {
|
|
28
|
+
const line = pending.slice(0, index).replace(/\r$/, '')
|
|
29
|
+
pending = pending.slice(index + 1)
|
|
30
|
+
if (line.trim()) {
|
|
31
|
+
onLine(line)
|
|
32
|
+
}
|
|
33
|
+
index = pending.indexOf('\n')
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
flush() {
|
|
37
|
+
const line = pending.replace(/\r$/, '')
|
|
38
|
+
pending = ''
|
|
39
|
+
if (line.trim()) {
|
|
40
|
+
onLine(line)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
13
46
|
/**
|
|
14
47
|
* @param {string} command
|
|
15
48
|
* @param {string[]} args
|
|
@@ -31,30 +64,46 @@ export function run(command, args, options = {}) {
|
|
|
31
64
|
|
|
32
65
|
let stdout = ''
|
|
33
66
|
let stderr = ''
|
|
67
|
+
const decoders = { stdout: new StringDecoder('utf8'), stderr: new StringDecoder('utf8') }
|
|
68
|
+
const splitters = {
|
|
69
|
+
stdout: options.onLine ? createLineSplitter(options.onLine) : null,
|
|
70
|
+
stderr: options.onLine ? createLineSplitter(options.onLine) : null,
|
|
71
|
+
}
|
|
34
72
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
73
|
+
/**
|
|
74
|
+
* @param {'stdout' | 'stderr'} stream
|
|
75
|
+
* @param {Buffer} chunk
|
|
76
|
+
*/
|
|
77
|
+
const consume = (stream, chunk) => {
|
|
78
|
+
const text = decoders[stream].write(chunk)
|
|
79
|
+
if (stream === 'stdout') {
|
|
80
|
+
stdout += text
|
|
81
|
+
} else {
|
|
82
|
+
stderr += text
|
|
40
83
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
options.onLine(line)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return text
|
|
84
|
+
options.onChunk?.(text, stream)
|
|
85
|
+
splitters[stream]?.push(text)
|
|
47
86
|
}
|
|
48
87
|
|
|
49
|
-
child.stdout?.on('data', (chunk) =>
|
|
50
|
-
|
|
51
|
-
})
|
|
52
|
-
child.stderr?.on('data', (chunk) => {
|
|
53
|
-
stderr += consume(chunk)
|
|
54
|
-
})
|
|
88
|
+
child.stdout?.on('data', (chunk) => consume('stdout', chunk))
|
|
89
|
+
child.stderr?.on('data', (chunk) => consume('stderr', chunk))
|
|
55
90
|
|
|
56
91
|
child.on('error', reject)
|
|
57
92
|
child.on('close', (code) => {
|
|
93
|
+
for (const stream of /** @type {const} */ (['stdout', 'stderr'])) {
|
|
94
|
+
const tail = decoders[stream].end()
|
|
95
|
+
if (tail) {
|
|
96
|
+
if (stream === 'stdout') {
|
|
97
|
+
stdout += tail
|
|
98
|
+
} else {
|
|
99
|
+
stderr += tail
|
|
100
|
+
}
|
|
101
|
+
options.onChunk?.(tail, stream)
|
|
102
|
+
splitters[stream]?.push(tail)
|
|
103
|
+
}
|
|
104
|
+
splitters[stream]?.flush()
|
|
105
|
+
}
|
|
106
|
+
|
|
58
107
|
const exitCode = code ?? 1
|
|
59
108
|
if (exitCode !== 0 && !options.allowFailure) {
|
|
60
109
|
const error = new Error(`${command} 退出码 ${exitCode}`)
|
package/src/core/project.js
CHANGED
|
@@ -161,6 +161,19 @@ function firstPresent(...values) {
|
|
|
161
161
|
return undefined
|
|
162
162
|
}
|
|
163
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
|
+
|
|
164
177
|
/**
|
|
165
178
|
* manifest 是唯一来源,fallbacks 只在该字段缺失时兜底。
|
|
166
179
|
* 配置里的同名字段可能是 init 时的快照,直接覆盖会让改过 manifest 的项目静默打旧版本。
|
|
@@ -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
|
+
}
|
package/src/core/sdk.js
CHANGED
|
@@ -241,8 +241,9 @@ export async function findSdkForVersion(config, version, extraDirs = []) {
|
|
|
241
241
|
|
|
242
242
|
/**
|
|
243
243
|
* 老版本 SDK 的 compileSdk/buildTools 与新版不同,优先沿用 SDK 自带的模板值。
|
|
244
|
+
* Kotlin 版本同样以模板声明为准;模板没写 kotlin-gradle-plugin 时返回空,由调用方兜底。
|
|
244
245
|
* @param {string} templateRoot
|
|
245
|
-
* @returns {Promise<{ compileSdkVersion?: number, buildToolsVersion?: string }>}
|
|
246
|
+
* @returns {Promise<{ compileSdkVersion?: number, buildToolsVersion?: string, kotlinVersion?: string }>}
|
|
246
247
|
*/
|
|
247
248
|
export async function readTemplateDefaults(templateRoot) {
|
|
248
249
|
const file = path.join(templateRoot, 'simpleDemo', 'build.gradle')
|
|
@@ -252,9 +253,13 @@ export async function readTemplateDefaults(templateRoot) {
|
|
|
252
253
|
const text = await readText(file)
|
|
253
254
|
const compileSdk = Number(/compileSdkVersion\s+(\d+)/.exec(text)?.[1])
|
|
254
255
|
const buildTools = /buildToolsVersion\s+'([^']+)'/.exec(text)?.[1]
|
|
256
|
+
const rootFile = path.join(templateRoot, 'build.gradle')
|
|
257
|
+
const rootText = (await exists(rootFile)) ? await readText(rootFile) : ''
|
|
258
|
+
const kotlinVersion = /kotlin-gradle-plugin:([0-9][\w.-]*)/.exec(rootText)?.[1]
|
|
255
259
|
return {
|
|
256
260
|
compileSdkVersion: Number.isFinite(compileSdk) ? compileSdk : undefined,
|
|
257
261
|
buildToolsVersion: buildTools || undefined,
|
|
262
|
+
kotlinVersion: kotlinVersion || undefined,
|
|
258
263
|
}
|
|
259
264
|
}
|
|
260
265
|
|
package/src/core/signing.js
CHANGED
|
@@ -18,17 +18,21 @@ import { exists, readText } from './fsx.js'
|
|
|
18
18
|
*/
|
|
19
19
|
export async function resolveSigning(project, options = {}) {
|
|
20
20
|
const signing = project.signing ?? {}
|
|
21
|
-
const keystore =
|
|
22
|
-
const
|
|
21
|
+
const keystore = resolveProjectPath(project.projectDir, signing.keystore)
|
|
22
|
+
const properties = await readProperties(signing.keystoreProperties, project.projectDir)
|
|
23
|
+
// 别名优先取配置;允许只给 keystore.properties(团队自定义布局时更省配置)
|
|
24
|
+
const keyAlias = String(signing.keyAlias ?? '') || String(properties.keyAlias ?? '')
|
|
23
25
|
if (!keystore || !(await exists(keystore))) {
|
|
24
|
-
throw new PackError(`找不到签名证书:${keystore || '(未配置)'}`, EXIT.SIGN
|
|
26
|
+
throw new PackError(`找不到签名证书:${keystore || '(未配置)'}`, EXIT.SIGN, {
|
|
27
|
+
hint: '检查 signing.keystore;相对路径按项目目录解析,也可以写绝对路径',
|
|
28
|
+
})
|
|
25
29
|
}
|
|
26
30
|
if (!keyAlias) {
|
|
27
|
-
throw new PackError('未配置签名别名 keyAlias', EXIT.SIGN
|
|
31
|
+
throw new PackError('未配置签名别名 keyAlias', EXIT.SIGN, {
|
|
32
|
+
hint: '在配置 signing.keyAlias 填写,或写进 keystoreProperties 的 keyAlias',
|
|
33
|
+
})
|
|
28
34
|
}
|
|
29
35
|
|
|
30
|
-
const properties = await readProperties(signing.keystoreProperties, project.projectDir)
|
|
31
|
-
|
|
32
36
|
const storePassword =
|
|
33
37
|
options.storePassword ??
|
|
34
38
|
envValue(signing.storePasswordEnv) ??
|
|
@@ -49,6 +53,20 @@ export async function resolveSigning(project, options = {}) {
|
|
|
49
53
|
return { keystore, keyAlias, storePassword, keyPassword }
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
/**
|
|
57
|
+
* 配置里的相对路径统一相对项目目录解析,避免从别的目录执行时找不到证书。
|
|
58
|
+
* @param {string | undefined} projectDir
|
|
59
|
+
* @param {unknown} value
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
function resolveProjectPath(projectDir, value) {
|
|
63
|
+
const text = String(value ?? '').trim()
|
|
64
|
+
if (!text) {
|
|
65
|
+
return ''
|
|
66
|
+
}
|
|
67
|
+
return path.isAbsolute(text) ? text : path.join(String(projectDir ?? ''), text)
|
|
68
|
+
}
|
|
69
|
+
|
|
52
70
|
/**
|
|
53
71
|
* @param {string | undefined} name
|
|
54
72
|
*/
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { rm, stat } from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { exists, readJson, writeJson } from './fsx.js'
|
|
5
|
+
|
|
6
|
+
export const TEMPLATE_STATE_FILE = '.uni-pack-template.json'
|
|
7
|
+
|
|
8
|
+
const FINGERPRINT_FILES = [
|
|
9
|
+
'build.gradle',
|
|
10
|
+
'settings.gradle',
|
|
11
|
+
'gradle/wrapper/gradle-wrapper.properties',
|
|
12
|
+
'simpleDemo/build.gradle',
|
|
13
|
+
'simpleDemo/src/main/AndroidManifest.xml',
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* SDK 换版本、模板被手工改过(mtime/大小变化)时都要重新复制模板。
|
|
18
|
+
* @param {{ root: string, templateRoot: string, version?: string | null }} sdk
|
|
19
|
+
* @returns {Promise<{ fingerprint: string, files: Array<{ file: string, size: number, mtimeMs: number }> }>}
|
|
20
|
+
*/
|
|
21
|
+
export async function templateFingerprint(sdk) {
|
|
22
|
+
/** @type {Array<{ file: string, size: number, mtimeMs: number }>} */
|
|
23
|
+
const files = []
|
|
24
|
+
for (const relative of FINGERPRINT_FILES) {
|
|
25
|
+
const file = path.join(sdk.templateRoot, relative)
|
|
26
|
+
try {
|
|
27
|
+
const info = await stat(file)
|
|
28
|
+
files.push({ file: relative, size: info.size, mtimeMs: Math.round(info.mtimeMs) })
|
|
29
|
+
} catch {
|
|
30
|
+
files.push({ file: relative, size: -1, mtimeMs: -1 })
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const payload = JSON.stringify({
|
|
34
|
+
root: sdk.root,
|
|
35
|
+
templateRoot: sdk.templateRoot,
|
|
36
|
+
version: String(sdk.version ?? ''),
|
|
37
|
+
files,
|
|
38
|
+
})
|
|
39
|
+
return { fingerprint: createHash('sha256').update(payload).digest('hex'), files }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} gradleRoot
|
|
44
|
+
* @returns {Promise<{ fingerprint?: string, templateRoot?: string, version?: string, builtAt?: string } | null>}
|
|
45
|
+
*/
|
|
46
|
+
export async function readTemplateState(gradleRoot) {
|
|
47
|
+
return readJson(path.join(gradleRoot, TEMPLATE_STATE_FILE)).catch(() => null)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} gradleRoot
|
|
52
|
+
* @param {Record<string, unknown>} state
|
|
53
|
+
*/
|
|
54
|
+
export async function writeTemplateState(gradleRoot, state) {
|
|
55
|
+
await writeJson(path.join(gradleRoot, TEMPLATE_STATE_FILE), state)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 增量构建前清掉会残留的输入:当前 appid 的 www、SDK 模板 assets 数据、上次的 AAR/so。
|
|
60
|
+
* build/、.gradle/、simpleDemo/build/ 保留,Gradle 才能命中 UP-TO-DATE。
|
|
61
|
+
* @param {{ gradleRoot: string, appid: string }} options
|
|
62
|
+
* @returns {Promise<string[]>} 已清理的路径
|
|
63
|
+
*/
|
|
64
|
+
export async function cleanIncrementalInputs(options) {
|
|
65
|
+
const relative = [
|
|
66
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'apps', options.appid),
|
|
67
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'data'),
|
|
68
|
+
path.join('simpleDemo', 'src', 'main', 'assets', 'dcloud_uniplugins.json'),
|
|
69
|
+
path.join('simpleDemo', 'libs'),
|
|
70
|
+
path.join('simpleDemo', 'src', 'main', 'jniLibs'),
|
|
71
|
+
]
|
|
72
|
+
const removed = []
|
|
73
|
+
for (const item of relative) {
|
|
74
|
+
const target = path.join(options.gradleRoot, item)
|
|
75
|
+
if (await exists(target)) {
|
|
76
|
+
await rm(target, { recursive: true, force: true })
|
|
77
|
+
removed.push(item)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return removed
|
|
81
|
+
}
|
package/src/core/webBuild.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { PackError, EXIT } from './errors.js'
|
|
3
|
-
import { exists, readJson } from './fsx.js'
|
|
3
|
+
import { ensureDir, exists, readJson } from './fsx.js'
|
|
4
4
|
import { runHbuilderxBuild } from './hbuilderx.js'
|
|
5
5
|
import { run } from './process.js'
|
|
6
|
+
import { buildTempEnv } from './workdir.js'
|
|
6
7
|
|
|
7
8
|
const REQUIRED = ['manifest.json', 'app-service.js', 'app-config.js', '__uniappview.html']
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @param {any} project
|
|
11
12
|
* @param {import('./logger.js').Logger} logger
|
|
12
|
-
* @param {{ hbuilderx?: { root: string, version: string }, vueVersion?: 2 | 3 }} [context]
|
|
13
|
+
* @param {{ hbuilderx?: { root: string, version: string }, vueVersion?: 2 | 3, tempDir?: string }} [context]
|
|
13
14
|
* @returns {Promise<{ dir: string, manifest: any }>}
|
|
14
15
|
*/
|
|
15
16
|
export async function runWebBuild(project, logger, context = {}) {
|
|
@@ -18,6 +19,11 @@ export async function runWebBuild(project, logger, context = {}) {
|
|
|
18
19
|
project.projectDir,
|
|
19
20
|
String(config.outputDir ?? (project.type === 'hbuilderx' ? 'unpackage/dist/build/app-plus' : 'dist/build/app')),
|
|
20
21
|
)
|
|
22
|
+
const tempDir = context.tempDir ? String(context.tempDir) : ''
|
|
23
|
+
if (tempDir) {
|
|
24
|
+
// 先建好目录,再把它作为子进程的 TEMP/TMP,避免系统 TEMP 的 ACL 问题
|
|
25
|
+
await ensureDir(tempDir)
|
|
26
|
+
}
|
|
21
27
|
|
|
22
28
|
if (project.type === 'hbuilderx') {
|
|
23
29
|
const hbuilderx = context.hbuilderx
|
|
@@ -32,6 +38,7 @@ export async function runWebBuild(project, logger, context = {}) {
|
|
|
32
38
|
inputDir: String(project.inputDir ?? project.projectDir),
|
|
33
39
|
outputDir: dir,
|
|
34
40
|
vueVersion: context.vueVersion ?? 3,
|
|
41
|
+
tempDir: tempDir || undefined,
|
|
35
42
|
logger,
|
|
36
43
|
})
|
|
37
44
|
} else {
|
|
@@ -42,6 +49,7 @@ export async function runWebBuild(project, logger, context = {}) {
|
|
|
42
49
|
try {
|
|
43
50
|
await run(command, args, {
|
|
44
51
|
cwd: project.projectDir,
|
|
52
|
+
env: tempDir ? buildTempEnv(tempDir) : undefined,
|
|
45
53
|
onLine: (line) => logger.detail(line),
|
|
46
54
|
})
|
|
47
55
|
} catch (error) {
|
package/src/core/wgt.js
CHANGED
|
@@ -1,18 +1,36 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto'
|
|
2
|
-
import { createReadStream, createWriteStream } from 'node:fs'
|
|
3
|
-
import { readdir, readFile } from 'node:fs/promises'
|
|
4
1
|
import path from 'node:path'
|
|
2
|
+
import { createWriteStream } from 'node:fs'
|
|
3
|
+
import { readdir, stat } from 'node:fs/promises'
|
|
5
4
|
import { pipeline } from 'node:stream/promises'
|
|
6
5
|
import yazl from 'yazl'
|
|
7
6
|
import { PackError, EXIT } from './errors.js'
|
|
8
|
-
import { ensureDir, exists, readJson } from './fsx.js'
|
|
7
|
+
import { ensureDir, exists, readJson, replaceHardLink, writeJson } from './fsx.js'
|
|
8
|
+
import { fileHash } from './hash.js'
|
|
9
9
|
|
|
10
10
|
const REQUIRED = ['manifest.json', 'app-service.js', '__uniappview.html']
|
|
11
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
|
+
|
|
12
26
|
/**
|
|
13
27
|
* @param {Object} input
|
|
14
28
|
* @param {string} input.resourcesDir
|
|
15
|
-
* @param {string} input.
|
|
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 }>}
|
|
16
34
|
*/
|
|
17
35
|
export async function writeWgt(input) {
|
|
18
36
|
const manifest = await readJson(path.join(input.resourcesDir, 'manifest.json'))
|
|
@@ -29,19 +47,36 @@ export async function writeWgt(input) {
|
|
|
29
47
|
throw new PackError('wgt 的 manifest.json 缺少 id', EXIT.WGT)
|
|
30
48
|
}
|
|
31
49
|
|
|
32
|
-
|
|
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)
|
|
33
56
|
const zip = new yazl.ZipFile()
|
|
34
57
|
await addDirectory(zip, input.resourcesDir, '')
|
|
35
58
|
zip.end()
|
|
36
|
-
await pipeline(zip.outputStream, createWriteStream(
|
|
59
|
+
await pipeline(zip.outputStream, createWriteStream(outputFile))
|
|
37
60
|
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
throw new PackError(`wgt 体积异常(${buffer.length} 字节),检查资源目录是否完整`, EXIT.WGT)
|
|
61
|
+
const size = (await stat(outputFile)).size
|
|
62
|
+
if (size < 1024 * 1024) {
|
|
63
|
+
throw new PackError(`wgt 体积异常(${size} 字节),检查资源目录是否完整`, EXIT.WGT)
|
|
42
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)
|
|
43
78
|
|
|
44
|
-
return { file:
|
|
79
|
+
return { file: outputFile, meta: metaFile, latest, sha256, size, manifest }
|
|
45
80
|
}
|
|
46
81
|
|
|
47
82
|
/**
|
|
@@ -61,16 +96,3 @@ async function addDirectory(zip, dir, prefix) {
|
|
|
61
96
|
}
|
|
62
97
|
}
|
|
63
98
|
}
|
|
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
|
-
}
|
|
@@ -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
|
+
}
|