diff --git a/scripts/harmony.patch.js b/scripts/harmony.patch.js new file mode 100644 index 0000000..f57ca62 --- /dev/null +++ b/scripts/harmony.patch.js @@ -0,0 +1,105 @@ +#!/usr/bin/env node +/** + * harmony.patch.js + * + * 该脚本用于鸿蒙元服务的构建后处理(仅在生产构建时由 vue.config.js 触发): + * 1. 拷贝 harmony-configs 配置文件到构建产物目录 + * 2. 拷贝 site.js 到构建产物目录 + * 3. 创建 ZIP 压缩包 + * + * 使用: + * node harmony.patch.js # 拷贝文件并创建 ZIP(默认 mode=production) + * node harmony.patch.js --no-zip # 只拷贝文件,不创建 ZIP + */ + +const fs = require('fs'); +const fsp = fs.promises; +const path = require('path'); + +const { openFileDirectory } = require('./open-directory'); +const { createZipWithSystemCommand } = require('./create-zip'); + +async function copyAssets(mode = 'production') { + try { + const cwd = path.join(__dirname, '..'); + const destDir = path.join(cwd, 'unpackage', 'dist', mode === 'production' ? 'build' : 'dev', '.mp-harmony'); + + await ensureDir(destDir); + + const copyPromises = []; + + if (await exists(path.join(cwd, 'harmony-configs'))) { + copyPromises.push(copyDirectory(path.join(cwd, 'harmony-configs'), path.join(destDir, 'harmony-configs'))); + } + + const srcSitePath = path.join(cwd, 'site.js'); + if (await exists(srcSitePath)) { + copyPromises.push(fsp.copyFile(srcSitePath, path.join(destDir, 'site.js'))); + } + + await Promise.all(copyPromises); + console.log('HarmonyOS 资源拷贝完成'); + } catch (err) { + console.error('发生错误:', err); + process.exitCode = 1; + } +} + +async function copyDirectory(src, dest) { + await ensureDir(dest); + const entries = await fsp.readdir(src, { withFileTypes: true }); + for (const entry of entries) { + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + if (entry.isDirectory()) { + await copyDirectory(srcPath, destPath); + } else { + await fsp.copyFile(srcPath, destPath); + } + } +} + +async function main() { + const argv = process.argv.slice(2); + const options = { + noZip: argv.includes('--no-zip'), + mode: 'production' + }; + + const modeIndex = argv.indexOf('--mode'); + if (modeIndex !== -1 && modeIndex + 1 < argv.length) { + options.mode = argv[modeIndex + 1]; + } + + await copyAssets(options.mode); + + if (!options.noZip && options.mode === 'production') { + const cwd = path.join(__dirname, '..'); + const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', '.mp-harmony'); + const destDir = path.join(cwd, 'unpackage', 'dist', 'build'); + const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir); + console.log(`ZIP 文件路径: ${zipFilePath}`); + await openFileDirectory(zipFilePath); + } else { + console.log('跳过创建 ZIP 文件和打开目录'); + } +} + +async function exists(p) { + try { + await fsp.access(p, fs.constants.F_OK); + return true; + } catch (e) { + return false; + } +} + +async function ensureDir(dirPath) { + try { + await fsp.mkdir(dirPath, { recursive: true }); + } catch (e) { + if (e.code !== 'EEXIST') throw e; + } +} + +main(); diff --git a/vue.config.js b/vue.config.js index d3a077f..4ecf850 100644 --- a/vue.config.js +++ b/vue.config.js @@ -182,6 +182,13 @@ class VersionInjectPlugin { } catch (e) { console.error('补丁脚本执行失败:', e.message); } + } else if (platform === 'mp-harmony') { + console.log('\n🚀 mp-harmony 生产构建完成,运行补丁脚本(配置拷贝 + ZIP 打包)...'); + try { + execSync(`"${process.execPath}" scripts/harmony.patch.js --mode production`, { cwd: __dirname, stdio: 'inherit' }); + } catch (e) { + console.error('补丁脚本执行失败:', e.message); + } } else if (platform === 'h5' && UNI_SCRIPT) { this._zipH5Build(compilation); }