106 lines
3.0 KiB
JavaScript
106 lines
3.0 KiB
JavaScript
#!/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();
|