目录

How do I obtain the packaging time of the HAP file through code?

This is implemented through the hvigor build script. During packaging, the time is written into a JSON file and saved to the rawfile directory. Then the file content can be directly read in the application. Content of the hvigorfile.ts file:

import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
import * as fs from 'fs';
import * as path from 'path';

// Callback function after node evaluation
hvigor.afterNodeEvaluate((hvigorNode) => {
  // Ensure this directory exists
  const resourcesDir = path.join(__dirname, 'entry/src/main/resources/rawfile');
  if (!fs.existsSync(resourcesDir)) {
    fs.mkdirSync(resourcesDir, { recursive: true });
  }

  // Write the build time into the JSON file
  const now = new Date();
  const buildTime = now.getFullYear() + '-'
    + String(now.getMonth() + 1).padStart(2, '0') + '-'
    + String(now.getDate()).padStart(2, '0') + ' '
    + String(now.getHours()).padStart(2, '0') + ':'
    + String(now.getMinutes()).padStart(2, '0') + ':'
    + String(now.getSeconds()).padStart(2, '0');
  const buildInfo = { 'buildTime': buildTime };
  fs.writeFileSync(
    path.join(resourcesDir, 'build_info.json'),
    JSON.stringify(buildInfo, null, 2)
  );
})

export default {
  system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}