import { harTasks } from '@ohos/hvigor-ohos-plugin';
import fs from 'fs'
interface OhPackage {
name: string;
version: number;
description: string;
author: string;
}
export function renameHarTask(str?: string) {
return {
pluginId: 'RenameHarTaskID',
apply(pluginContext) {
pluginContext.registerTask({
// 编写自定义任务
name: 'renameHarTask',
run: (taskContext) => {
//读取oh-package.json5,解析出version
const packageFile = taskContext.modulePath+'\oh-package.json5';
console.log('file: ', packageFile);
let fileContent = fs.readFileSync(packageFile, 'utf8');
console.log(fileContent);
const content: OhPackage = JSON.parse(fileContent);
const version = content.version;
const author = content.author;
console.log('renameHarTask: ', taskContext.moduleName, taskContext.modulePath);
const sourceFile = taskContext.modulePath + '\\build\\default\\outputs\\default\\' + taskContext.moduleName + '.har';
const targetFile = taskContext.modulePath + '\\build\\default\\outputs\\default\\'
+ taskContext.moduleName + '-' + version + '-' + author +'.har';
console.log('renameHarTask: sourceFile: ', sourceFile);
console.log('renameHarTask: targetFile: ', targetFile);
// 修改产物名
fs.rename(sourceFile, targetFile, (err)=> {
console.log('err: ' + err);
});
},
// 确认自定义任务插入位置
dependencies: ['default@PackageHar'],
postDependencies: ['assembleHar']
})
}
}
}
export default {
system: harTasks,
plugins:[renameHarTask()]
}