#!/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 syncSiteToStub(mode = 'production') { try { const cwd = path.join(__dirname, '..'); const srcSitePath = path.join(cwd, 'site.js'); const stubSitePath = path.join(cwd, 'stub-app-projects', 'mp-harmony', 'ascf', 'ascf_src', 'site.js'); if (await exists(srcSitePath)) { await fsp.copyFile(srcSitePath, stubSitePath); console.log('site.js 已同步到 stub 项目源码目录'); } } catch (err) { console.error('同步 site.js 到 stub 失败:', err); process.exitCode = 1; } } 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]; } // 同步 site.js 到 stub 项目源码目录,确保编译时能动态导入 await syncSiteToStub(options.mode); 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, 'mp-harmony'); 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();