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');
  if (fs.existsSync(filePath)) {
    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}`;

    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: []
}
