rabbit-harmony/products/app/hvigorfile.ts

39 lines
1.4 KiB
TypeScript

import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import fs from 'fs';
import path from 'path';
function updateArtifactName() {
const filePath = path.resolve(__dirname, 'build-profile.json5');
const appConfigPath = path.resolve(__dirname, '..', '..', 'AppScope', 'app.json5');
if (fs.existsSync(filePath)) {
let versionName = '1.0.0';
if (fs.existsSync(appConfigPath)) {
const appContent = fs.readFileSync(appConfigPath, 'utf8');
const vMatch = appContent.match(/"versionName"\s*:\s*"([^"]+)"/);
if (vMatch) versionName = vMatch[1];
}
const now = new Date();
const timeStr = `${now.getFullYear().toString().slice(-2)}${now.getMonth() + 1}${now.getDate()}${now.getHours()}${now.getMinutes()}`;
// const newArtifactName = `rabbit_harmony_${timeStr}`;
// 格式示例: rabbit_harmony_v1.0.0_2605062215
const newArtifactName = `rabbit_harmony_v${versionName}_${timeStr}`;
let content = fs.readFileSync(filePath, 'utf8');
// 使用正则表达式精准匹配 BUILD_TIME 后的值并替换,匹配模式: "BUILD_TIME": "任意内容"
const newContent = content.replace(/("artifactName"\s*:\s*")[^"]*(")/g, `$1${newArtifactName}$2`);
if (content !== newContent) {
fs.writeFileSync(filePath, newContent, 'utf8');
}
}
}
updateArtifactName();
export default {
system: hapTasks,
plugins: []
}