|
1 | | -import { cp, rm } from 'node:fs/promises' |
| 1 | +import { cp, readdir, rm } from 'node:fs/promises' |
2 | 2 | import { join } from 'node:path' |
3 | 3 | import type { PackageJson } from 'pkg-types' |
4 | 4 | import { App } from '../utils/app.js' |
5 | | -import { editJsonFile, exists } from '../utils/file.js' |
| 5 | +import { editJsonFile, exists, readJsonFile } from '../utils/file.js' |
6 | 6 | import Log from '../utils/log.js' |
| 7 | +import { getPackageDependencies } from '../utils/package.js' |
7 | 8 | import { HarmonyDir, TemplateDir } from '../utils/path.js' |
8 | 9 | import type { AppJson } from './templates/app.json5.js' |
| 10 | +import type { BuildProfile } from './templates/build-profile.js' |
9 | 11 | import type { ColorConfig } from './templates/element.json.js' |
10 | | -import { assetsAppsPath, copyWww } from './www.js' |
| 12 | + |
| 13 | +export const assetsAppsPath = join(HarmonyDir, 'entry/src/main/resources/resfile/apps/HBuilder/www') |
| 14 | + |
| 15 | +export const devDistPath = join(App.projectRoot, 'dist/dev/app-harmony') |
| 16 | + |
| 17 | +export const buildDistPath = join(App.projectRoot, 'dist/build/app-harmony') |
| 18 | + |
| 19 | +export async function copyWww(options?: { isBuild?: boolean }) { |
| 20 | + const buildDistDir = options?.isBuild ? buildDistPath : devDistPath |
| 21 | + const files = await readdir(buildDistDir) |
| 22 | + for (const file of files) { |
| 23 | + const filePath = join(buildDistDir, file) |
| 24 | + if (file === 'uni_modules') { |
| 25 | + const hmFiles = await readdir(filePath) |
| 26 | + for (const hmFile of hmFiles) { |
| 27 | + const hmFilePath = join(filePath, hmFile) |
| 28 | + if (hmFile === 'build-profile.json5') { |
| 29 | + const modules = readJsonFile<BuildProfile>(hmFilePath, true).modules || [] |
| 30 | + if (modules.length > 0) { |
| 31 | + await editJsonFile(join(HarmonyDir, hmFile), (data: BuildProfile) => { |
| 32 | + data.modules.push(...modules) |
| 33 | + }) |
| 34 | + } |
| 35 | + } else if (hmFile === 'index.generated.ets') { |
| 36 | + await cp(hmFilePath, join(HarmonyDir, `entry/src/main/ets/uni_modules/${hmFile}`), { recursive: true }) |
| 37 | + } else if (hmFile === 'oh-package.json5') { |
| 38 | + const dependencies = getPackageDependencies(readJsonFile<PackageJson>(hmFilePath, true) || {}) |
| 39 | + if (Object.keys(dependencies).length > 0) { |
| 40 | + await editJsonFile(join(HarmonyDir, hmFile), (data: PackageJson) => { |
| 41 | + if (!data.dependencies) data.dependencies = dependencies |
| 42 | + else { |
| 43 | + for (const key in dependencies) { |
| 44 | + if (data.dependencies[key]) { |
| 45 | + if (data.dependencies[key] === dependencies[key]) continue |
| 46 | + Log.debug(`uni_modules 依赖 \`${key}\` 版本不一致,使用版本 ${dependencies[key]}`) |
| 47 | + } |
| 48 | + data.dependencies[key] = dependencies[key] |
| 49 | + } |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } else { |
| 56 | + await cp(filePath, join(assetsAppsPath, file), { recursive: true }) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
11 | 60 |
|
12 | 61 | export async function prepare(options?: { isBuild?: boolean }) { |
13 | 62 | Log.debug('前端打包资源嵌入 Harmony 资源中') |
|
0 commit comments