1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

feat: add logger

This commit is contained in:
sunnylqm 2024-02-24 11:36:58 +08:00
parent 4f9e1495c8
commit f01716bcb2
No known key found for this signature in database
2 changed files with 51 additions and 52 deletions

View File

@ -1,4 +1,4 @@
import { CheckResult, PushyOptions, ProgressData } from './type'; import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { assertRelease, log } from './utils'; import { assertRelease, log } from './utils';
import { import {
EmitterSubscription, EmitterSubscription,
@ -12,9 +12,9 @@ import {
pushyNativeEventEmitter, pushyNativeEventEmitter,
currentVersion, currentVersion,
packageVersion, packageVersion,
report,
rolledBackVersion, rolledBackVersion,
setLocalHashInfo, setLocalHashInfo,
isRolledBack,
} from './core'; } from './core';
const defaultServer = { const defaultServer = {
@ -25,6 +25,8 @@ const defaultServer = {
}; };
const empty = {}; const empty = {};
const noop = () => {};
export class Pushy { export class Pushy {
options: PushyOptions = { options: PushyOptions = {
appKey: '', appKey: '',
@ -32,6 +34,7 @@ export class Pushy {
autoMarkSuccess: true, autoMarkSuccess: true,
useAlert: true, useAlert: true,
strategy: 'both', strategy: 'both',
logger: noop,
}; };
lastChecking: number; lastChecking: number;
@ -55,10 +58,45 @@ export class Pushy {
for (const [key, value] of Object.entries(options)) { for (const [key, value] of Object.entries(options)) {
if (value !== undefined) { if (value !== undefined) {
this.options[key] = value; this.options[key] = value;
if (key === 'logger') {
if (isRolledBack) {
this.report({
type: 'rollback',
data: {
rolledBackVersion,
},
});
}
}
} }
} }
}; };
report = ({
type,
message = '',
data = {},
}: {
type: EventType;
message?: string;
data?: Record<string, string | number>;
}) => {
log(type + ' ' + message);
const { logger = noop, appKey } = this.options;
logger({
type,
data: {
appKey,
currentVersion,
cInfo,
packageVersion,
buildTime,
message,
...data,
},
});
};
getCheckUrl = (endpoint: string = this.options.server!.main) => { getCheckUrl = (endpoint: string = this.options.server!.main) => {
return `${endpoint}/checkUpdate/${this.options.appKey}`; return `${endpoint}/checkUpdate/${this.options.appKey}`;
}; };
@ -79,7 +117,7 @@ export class Pushy {
} }
this.marked = true; this.marked = true;
PushyModule.markSuccess(); PushyModule.markSuccess();
report({ type: 'markSuccess' }); this.report({ type: 'markSuccess' });
}; };
switchVersion = (hash: string) => { switchVersion = (hash: string) => {
assertRelease(); assertRelease();
@ -108,7 +146,7 @@ export class Pushy {
return this.lastResult; return this.lastResult;
} }
this.lastChecking = now; this.lastChecking = now;
report({ type: 'checking' }); this.report({ type: 'checking' });
const fetchPayload = { const fetchPayload = {
method: 'POST', method: 'POST',
headers: { headers: {
@ -126,7 +164,7 @@ export class Pushy {
try { try {
resp = await fetch(this.getCheckUrl(), fetchPayload); resp = await fetch(this.getCheckUrl(), fetchPayload);
} catch (e) { } catch (e) {
report({ this.report({
type: 'errorChecking', type: 'errorChecking',
message: 'Can not connect to update server. Trying backup endpoints.', message: 'Can not connect to update server. Trying backup endpoints.',
}); });
@ -142,7 +180,7 @@ export class Pushy {
} }
} }
if (!resp) { if (!resp) {
report({ this.report({
type: 'errorChecking', type: 'errorChecking',
message: 'Can not connect to update server. Please check your network.', message: 'Can not connect to update server. Please check your network.',
}); });
@ -153,7 +191,7 @@ export class Pushy {
this.lastResult = result; this.lastResult = result;
if (resp.status !== 200) { if (resp.status !== 200) {
report({ this.report({
type: 'errorChecking', type: 'errorChecking',
message: result.message, message: result.message,
}); });
@ -214,7 +252,7 @@ export class Pushy {
); );
} }
let succeeded = false; let succeeded = false;
report({ type: 'downloading' }); this.report({ type: 'downloading' });
if (diffUrl) { if (diffUrl) {
log('downloading diff'); log('downloading diff');
try { try {
@ -257,7 +295,7 @@ export class Pushy {
delete this.progressHandlers[hash]; delete this.progressHandlers[hash];
} }
if (!succeeded) { if (!succeeded) {
return report({ return this.report({
type: 'errorUpdate', type: 'errorUpdate',
data: { newVersion: hash }, data: { newVersion: hash },
}); });
@ -278,17 +316,17 @@ export class Pushy {
if (Platform.OS !== 'android') { if (Platform.OS !== 'android') {
return; return;
} }
report({ type: 'downloadingApk' }); this.report({ type: 'downloadingApk' });
if (Platform.Version <= 23) { if (Platform.Version <= 23) {
try { try {
const granted = await PermissionsAndroid.request( const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
); );
if (granted !== PermissionsAndroid.RESULTS.GRANTED) { if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
return report({ type: 'rejectStoragePermission' }); return this.report({ type: 'rejectStoragePermission' });
} }
} catch (err) { } catch (err) {
return report({ type: 'errorStoragePermission' }); return this.report({ type: 'errorStoragePermission' });
} }
} }
const progressKey = 'downloadingApk'; const progressKey = 'downloadingApk';
@ -307,7 +345,7 @@ export class Pushy {
target: 'update.apk', target: 'update.apk',
hash: progressKey, hash: progressKey,
}).catch(() => { }).catch(() => {
report({ type: 'errowDownloadAndInstallApk' }); this.report({ type: 'errowDownloadAndInstallApk' });
}); });
if (this.progressHandlers[progressKey]) { if (this.progressHandlers[progressKey]) {
this.progressHandlers[progressKey].remove(); this.progressHandlers[progressKey].remove();

View File

@ -1,5 +1,4 @@
import { NativeEventEmitter, NativeModules, Platform } from 'react-native'; import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { EventType, UpdateEventsLogger } from './type';
import { log } from './utils'; import { log } from './utils';
const { const {
version: v, version: v,
@ -58,44 +57,6 @@ if (!uuid) {
PushyModule.setUuid(uuid); PushyModule.setUuid(uuid);
} }
const noop = () => {};
let reporter: UpdateEventsLogger = noop;
export function onPushyEvents(customReporter: UpdateEventsLogger) {
reporter = customReporter;
if (isRolledBack) {
report({
type: 'rollback',
data: {
rolledBackVersion,
},
});
}
}
export function report({
type,
message = '',
data = {},
}: {
type: EventType;
message?: string;
data?: Record<string, string | number>;
}) {
log(type + ' ' + message);
reporter({
type,
data: {
currentVersion,
cInfo,
packageVersion,
buildTime,
message,
...data,
},
});
}
log('uuid: ' + uuid); log('uuid: ' + uuid);
export const cInfo = { export const cInfo = {