Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions harmony/codePush/src/main/ets/CodePushNativeModule.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CodePushConstants } from './CodePushConstants';
import { RNOHContext, TurboModule } from '@rnoh/react-native-openharmony/ts';
import { UITurboModule, UITurboModuleContext } from '@rnoh/react-native-openharmony/ts';
import dataPreferences from '@ohos.data.preferences';
import { CodePushUpdateUtils } from './CodePushUpdateUtils';
import { CodePushUpdateManager } from './CodePushUpdateManager';
import { CodePushTelemetryManager } from './CodePushTelemetryManager';
import { CodePush } from './CodePush';
import common from '@ohos.app.ability.common';
import fs, { ListFileOptions } from '@ohos.file.fs';
import FileUtils from './FileUtils';
import { SettingsManager } from './SettingsManager';
import { BusinessError } from '@kit.BasicServicesKit';
import { CodePushUtils } from './CodePushUtils';
Expand Down Expand Up @@ -40,7 +39,7 @@ function generateUUID(): string {
return uuid;
}

export class CodePushNativeModule extends TurboModule implements TM.RTNCodePush.Spec {
export class CodePushNativeModule extends UITurboModule implements TM.RTNCodePush.Spec {
private mBinaryContentsHash: string = "";
private mClientUniqueId: string = "";
private mCodePush: CodePush | null = null;
Expand All @@ -49,13 +48,13 @@ export class CodePushNativeModule extends TurboModule implements TM.RTNCodePush.
private mTelemetryManager: CodePushTelemetryManager | null = null;
private mSettingsManager: SettingsManager | null = null;
private mUpdateManager: CodePushUpdateManager | null = null;
installMode = 0;
private installMode: number = 1;

sync(): Promise<unknown> {
throw new Error('Method not implemented.');
}

constructor(rnContext: RNOHContext, codePush: CodePush) {
constructor(rnContext: UITurboModuleContext, codePush: CodePush) {
super(rnContext);
Logger.info(TAG, `constructor start`)
dataPreferences.getPreferences(context, CodePushConstants.CODE_PUSH_PREFERENCES,
Expand Down Expand Up @@ -138,7 +137,7 @@ export class CodePushNativeModule extends TurboModule implements TM.RTNCodePush.
async doInBackgroundForUpdateMetadata(updateState: number) {
Logger.info(TAG, `doInBackgroundForUpdateMetadata updateState=${updateState}`);
try {
let currentPackage = new CodePushUpdateManager('').getCurrentPackage();
let currentPackage = await new CodePushUpdateManager('').getCurrentPackage();
if (!currentPackage) {
Logger.info(TAG, `currentPackage is empty`);
return null;
Expand Down
41 changes: 36 additions & 5 deletions harmony/codePush/src/main/ets/CodePushPackage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RNPackage, TurboModulesFactory, } from "@rnoh/react-native-openharmony/ts";
import type { TurboModule, TurboModuleContext, } from "@rnoh/react-native-openharmony/ts";
import { RNPackage, UITurboModule,UITurboModuleContext,UITurboModuleFactory } from "@rnoh/react-native-openharmony/ts";
import { TM } from "@rnoh/react-native-openharmony/generated/ts";
import { CodePushNativeModule } from './CodePushNativeModule';
import { CodePushBuilder } from './CodePushBuilder';
import fs, { ReadTextOptions } from "@ohos.file.fs";
import common from '@ohos.app.ability.common';
import { bundleManager } from '@kit.AbilityKit';

class CodePushModulesFactory extends TurboModulesFactory {
createTurboModule(name: string): TurboModule | null {
class CodePushModulesFactory extends UITurboModuleFactory {
createTurboModule(name: string): UITurboModule | null {
if (name === TM.RTNCodePush.NAME) {
let codePush = new CodePushBuilder().build()
return new CodePushNativeModule(this.ctx, codePush);
Expand All @@ -19,7 +21,36 @@ class CodePushModulesFactory extends TurboModulesFactory {
}

export class CodePushPackage extends RNPackage {
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
createTurboModulesFactory(ctx: UITurboModuleContext): UITurboModuleFactory {
this.comparingVersion()
return new CodePushModulesFactory(ctx);
}
comparingVersion(){
let context = getContext(this) as common.UIAbilityContext;
const codepushjsonpath = context.filesDir + '/CodePush/codepush.json';
if(fs.accessSync(codepushjsonpath)){
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
bundleManager.getBundleInfoForSelf(bundleFlags).then((appInfo) => {
const sAppVersion = appInfo.versionName;
const cpJSON = this.readFileToString(codepushjsonpath);
const bundlePath = context.filesDir + '/CodePush/' + cpJSON['currentPackage'] + '/app.json';
const bundleJSON = this.readFileToString(bundlePath);
if(sAppVersion !== bundleJSON['appVersion']){
console.log(`appVersion不一致:原${bundleJSON['appVersion']}-新${sAppVersion}`)
fs.rmdirSync(context.filesDir)
}
});
}
}
readFileToString(filePath:string):object{
let readTextOptions: ReadTextOptions = {
offset: 0,
length: 0,
encoding: 'utf-8'
}
let stat = fs.statSync(filePath);
readTextOptions.length = stat.size;
let str = fs.readTextSync(filePath,readTextOptions);
return JSON.parse(str)
}
}
16 changes: 10 additions & 6 deletions harmony/codePush/src/main/ets/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ export default class FileUtils {
Logger.info(TAG, "deleteDirectoryAtPath attempted with null directoryPath");
return;
}
// 删除整个目录
if (fs.statSync(directoryPath).isDirectory()) {
fs.rmdirSync(directoryPath)
// 删除单个文件
} else {
fs.unlinkSync(directoryPath)
if(fs.accessSync(directoryPath)){
// 删除整个目录
if (fs.statSync(directoryPath).isDirectory()) {
fs.rmdirSync(directoryPath)
// 删除单个文件
} else {
fs.unlinkSync(directoryPath)
}
}else{
return;
}
}

Expand Down