diff --git a/.gitignore b/.gitignore index a9a2055..f44e1fb 100644 --- a/.gitignore +++ b/.gitignore @@ -42,5 +42,6 @@ node_modules/ npm-debug.log Example/**/update.json +Example/**/.update +Example/**/.pushy yarn-error.log -Example/testHotUpdate/.pushy diff --git a/Example/testHotUpdate/.update b/Example/testHotUpdate/.update deleted file mode 100644 index f30ea17..0000000 --- a/Example/testHotUpdate/.update +++ /dev/null @@ -1,3 +0,0 @@ -{ - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoidXNlciIsInVpZCI6OTQ3NywiZXhwIjoxNjc5MjczODYwOTgzLCJpYXQiOjE2NzY2ODE4NjB9.RtoiYnN_2hJ6-aRsx6JkRWil0-RxkdpL7yD7koAky8E" -} \ No newline at end of file diff --git a/Example/testHotUpdate/src/TestConsole.js b/Example/testHotUpdate/src/TestConsole.js index 969eec9..6dac3d9 100644 --- a/Example/testHotUpdate/src/TestConsole.js +++ b/Example/testHotUpdate/src/TestConsole.js @@ -1,21 +1,23 @@ -import React from 'react'; +/* eslint-disable react-native/no-inline-styles */ +/* eslint-disable react/react-in-jsx-scope */ +import {useState} from 'react'; import { ActivityIndicator, Alert, Modal, TextInput, Button, - NativeModules, StyleSheet, SafeAreaView, Text, View, } from 'react-native'; -const Pushy = NativeModules.Pushy; +import {PushyModule} from 'react-native-update'; + export default function TestConsole({visible}) { - const [text, setText] = React.useState(''); - const [running, setRunning] = React.useState(false); + const [text, setText] = useState(''); + const [running, setRunning] = useState(false); return ( @@ -47,7 +49,7 @@ export default function TestConsole({visible}) { const methodName = inputs[0]; let params; if (inputs.length === 1) { - await Pushy[methodName](); + await PushyModule[methodName](); } else { if (inputs.length === 2) { params = inputs[1]; @@ -58,7 +60,7 @@ export default function TestConsole({visible}) { } console.log({inputs, params}); } - await Pushy[methodName](params); + await PushyModule[methodName](params); } Alert.alert('done'); } catch (e) { diff --git a/ios/RCTPushy/RCTPushy.m b/ios/RCTPushy/RCTPushy.m index b5bff84..a664076 100644 --- a/ios/RCTPushy/RCTPushy.m +++ b/ios/RCTPushy/RCTPushy.m @@ -3,7 +3,7 @@ #import "RCTPushyManager.h" // Thanks to this guard, we won't import this header when we build for the old architecture. #ifdef RCT_NEW_ARCH_ENABLED -#import "RNUpdateSpec.h" +#import "RCTPushySpec.h" #endif #import diff --git a/lib/NativeUpdate.js b/lib/NativeUpdate.js index be0d8c6..4706eac 100644 --- a/lib/NativeUpdate.js +++ b/lib/NativeUpdate.js @@ -4,38 +4,46 @@ */ 'use strict'; -import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; -import {TurboModuleRegistry} from 'react-native'; +import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport'; +import { TurboModuleRegistry } from 'react-native'; export interface Spec extends TurboModule { - getConstants: () => { - downloadRootDir: string; - packageVersion: string; - currentVersion: string; - isFirstTime: boolean; - rolledBackVersion: string; - buildTime: string; - blockUpdate: Object; - uuid: string; - isUsingBundleUrl: boolean; + getConstants: () => { + downloadRootDir: string, + packageVersion: string, + currentVersion: string, + isFirstTime: boolean, + rolledBackVersion: string, + buildTime: string, + blockUpdate: Object, + uuid: string, + isUsingBundleUrl: boolean, }; - setLocalHashInfo(hash: string, info:string): void; - getLocalHashInfo(hash: string):Promise; + setLocalHashInfo(hash: string, info: string): void; + getLocalHashInfo(hash: string): Promise; setUuid(uuid: string): void; - setBlockUpdate(options: {reason: string,until: number}): void; - downloadPatchFromPpk(options: { updateUrl:string, + setBlockUpdate(options: { reason: string, until: number }): void; + downloadPatchFromPpk(options: { + updateUrl: string, hash: string, - originHash: string}):Promise; - downloadPatchFromPackage(options: { updateUrl:string, - hash: string}): Promise; - downloadFullUpdate(options: { updateUrl:string, - hash: string}): Promise; - reloadUpdate(options: { hash: string}): void; - setNeedUpdate(options: { hash: string}): void; + originHash: string, + }): Promise; + downloadPatchFromPackage(options: { + updateUrl: string, + hash: string, + }): Promise; + downloadFullUpdate(options: { + updateUrl: string, + hash: string, + }): Promise; + reloadUpdate(options: { hash: string }): void; + setNeedUpdate(options: { hash: string }): void; markSuccess(): void; - downloadAndInstallApk(options: { url: string, - target : string, - hash: string}): Promise; + downloadAndInstallApk(options: { + url: string, + target: string, + hash: string, + }): Promise; } -export default (TurboModuleRegistry.get('Pushy'): ?Spec); \ No newline at end of file +export default (TurboModuleRegistry.get('Pushy'): ?Spec); diff --git a/lib/main.js b/lib/main.js index 96c2aa7..779722e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -16,14 +16,17 @@ const { } = require('react-native/Libraries/Core/ReactNativeVersion'); const RNVersion = `${v.major}.${v.minor}.${v.patch}`; const isTurboModuleEnabled = global.__turboModuleProxy != null; -const Pushy = isTurboModuleEnabled ? - require("./NativeUpdate").default : - NativeModules.Pushy; -if (!Pushy) { +export const PushyModule = isTurboModuleEnabled + ? require('./NativeUpdate').default + : NativeModules.Pushy; + +if (!PushyModule) { throw new Error('react-native-update模块无法加载,请对照安装文档检查配置。'); } -const PushyConstants = isTurboModuleEnabled ? Pushy.getConstants(): Pushy; +const PushyConstants = isTurboModuleEnabled + ? PushyModule.getConstants() + : PushyModule; export const downloadRootDir = PushyConstants.downloadRootDir; export const packageVersion = PushyConstants.packageVersion; @@ -43,22 +46,22 @@ if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) { } function setLocalHashInfo(hash, info) { - Pushy.setLocalHashInfo(hash, JSON.stringify(info)); + PushyModule.setLocalHashInfo(hash, JSON.stringify(info)); } async function getLocalHashInfo(hash) { - return JSON.parse(await Pushy.getLocalHashInfo(hash)); + return JSON.parse(await PushyModule.getLocalHashInfo(hash)); } export async function getCurrentVersionInfo() { return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {}; } -const eventEmitter = new NativeEventEmitter(Pushy); +const eventEmitter = new NativeEventEmitter(PushyModule); if (!uuid) { uuid = require('nanoid/non-secure').nanoid(); - Pushy.setUuid(uuid); + PushyModule.setUuid(uuid); } function logger(text) { @@ -166,7 +169,7 @@ function checkOperation(op) { reason: action.reason, until: Math.round((Date.now() + action.duration) / 1000), }; - Pushy.setBlockUpdate(blockUpdate); + PushyModule.setBlockUpdate(blockUpdate); } }); } @@ -212,7 +215,7 @@ export async function downloadUpdate(options, eventListeners) { if (options.diffUrl) { logger('downloading diff'); try { - await Pushy.downloadPatchFromPpk({ + await PushyModule.downloadPatchFromPpk({ updateUrl: options.diffUrl, hash: options.hash, originHash: currentVersion, @@ -225,7 +228,7 @@ export async function downloadUpdate(options, eventListeners) { if (!succeeded && options.pdiffUrl) { logger('downloading pdiff'); try { - await Pushy.downloadPatchFromPackage({ + await PushyModule.downloadPatchFromPackage({ updateUrl: options.pdiffUrl, hash: options.hash, }); @@ -237,7 +240,7 @@ export async function downloadUpdate(options, eventListeners) { if (!succeeded && options.updateUrl) { logger('downloading full patch'); try { - await Pushy.downloadFullUpdate({ + await PushyModule.downloadFullUpdate({ updateUrl: options.updateUrl, hash: options.hash, }); @@ -276,7 +279,7 @@ export function switchVersion(hash) { assertRelease(); if (assertHash(hash)) { logger('switchVersion: ' + hash); - Pushy.reloadUpdate({ hash }); + PushyModule.reloadUpdate({ hash }); } } @@ -284,7 +287,7 @@ export function switchVersionLater(hash) { assertRelease(); if (assertHash(hash)) { logger('switchVersionLater: ' + hash); - Pushy.setNeedUpdate({ hash }); + PushyModule.setNeedUpdate({ hash }); } } @@ -296,7 +299,7 @@ export function markSuccess() { return; } marked = true; - Pushy.markSuccess(); + PushyModule.markSuccess(); report(currentVersion, 'success'); } @@ -326,7 +329,7 @@ export async function downloadAndInstallApk({ url, onDownloadProgress }) { }, ); } - await Pushy.downloadAndInstallApk({ + await PushyModule.downloadAndInstallApk({ url, target: 'update.apk', hash, diff --git a/package.json b/package.json index 4c16cf8..bd9c575 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "codegenConfig": { "libraries": [ { - "name": "RNUpdateSpec", + "name": "RCTPushySpec", "type": "modules", "jsSrcsDir": "lib" }