-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathupdateUtils.ts
More file actions
114 lines (99 loc) · 4.1 KB
/
updateUtils.ts
File metadata and controls
114 lines (99 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import Constants from "expo-constants"
import type { CurrentlyRunningInfo, UseUpdatesReturnType } from "expo-updates"
import { ExpoConfig, ExpoUpdatesManifest } from "expo/config"
import { NativeInterfaceState } from "../../../modules/interface-demo"
const expoConfig: ExpoConfig = require("../../../app.json").expo as unknown as ExpoConfig
export const updateUrl: string = expoConfig.updates?.url ?? ""
const isAvailableUpdateCritical = (updatesSystem: UseUpdatesReturnType) => {
const { currentlyRunning, availableUpdate } = updatesSystem
const criticalIndexCurrent =
(currentlyRunning.manifest as ExpoUpdatesManifest)?.extra?.expoClient?.extra?.criticalIndex ??
Constants?.expoConfig?.extra?.criticalIndex ??
0
const criticalIndexUpdate =
(availableUpdate?.manifest as ExpoUpdatesManifest)?.extra?.expoClient?.extra?.criticalIndex ?? 0
return criticalIndexUpdate > criticalIndexCurrent
}
const manifestMessage = (manifest: any) => {
return manifest?.extra?.expoClient?.extra?.message ?? ""
}
// Utils for constructing display text
export const isInDevelopmentMode = (currentlyRunning: CurrentlyRunningInfo) => {
return __DEV__ && currentlyRunning.updateId === undefined
}
const currentlyRunningTitle = (currentlyRunning: CurrentlyRunningInfo) => {
if (isInDevelopmentMode(currentlyRunning)) {
return "No update soup for you in dev mode"
}
return currentlyRunning?.isEmbeddedLaunch ? "Running the embedded bundle:" : "Running an update:"
}
const currentlyRunningDescription = (
currentlyRunning: CurrentlyRunningInfo,
lastCheckForUpdateTime?: Date,
) => {
return (
` ID: ${currentlyRunning.updateId}\n` +
` Created: ${currentlyRunning.createdAt?.toISOString()}\n` +
` Channel: ${currentlyRunning.channel}\n` +
` Runtime Version: ${currentlyRunning.runtimeVersion}\n` +
` Message: ${manifestMessage(currentlyRunning.manifest)}\n` +
` Last check: ${lastCheckForUpdateTime?.toISOString()}\n`
)
}
const availableUpdateTitle = (updatesSystem: UseUpdatesReturnType) => {
if (isInDevelopmentMode(updatesSystem.currentlyRunning)) {
return "No update soup for you in dev mode"
}
return updatesSystem.isUpdateAvailable
? `${isAvailableUpdateCritical(updatesSystem) ? "A critical update" : "An update"} ${
updatesSystem.isUpdatePending ? "has been downloaded" : "is available"
}`
: "App is running the latest update"
}
const availableUpdateDescription = (updatesSystem: UseUpdatesReturnType) => {
if (isInDevelopmentMode(updatesSystem.currentlyRunning)) {
return ""
}
const availableUpdate = updatesSystem.availableUpdate
const updateDescription = availableUpdate
? ` ID: ${availableUpdate.updateId}\n` +
` Created: ${availableUpdate.createdAt?.toISOString() || ""}\n` +
` Message: ${manifestMessage(availableUpdate.manifest)}\n` +
` Critical: ${isAvailableUpdateCritical(updatesSystem)}\n`
: "No available update\n"
return updateDescription
}
const errorDescription = (updatesSystem: UseUpdatesReturnType) => {
const { checkError, downloadError } = updatesSystem
const checkErrorDescription = checkError?.message
? `Error on check: ${checkError?.message}\n`
: ""
const downloadErrorDescription = downloadError?.message
? `Error on download: ${downloadError?.message}\n`
: ""
return checkErrorDescription + downloadErrorDescription
}
const nativeInterfaceDescription = (nativeInterfaceState: NativeInterfaceState) => {
const { runtimeVersion, launchedUpdateId, embeddedUpdateId, lastDownloadTime } =
nativeInterfaceState
return (
` Launched ID: ${launchedUpdateId}\n` +
` Embedded ID: ${embeddedUpdateId}\n` +
` Runtime Version: ${runtimeVersion}\n` +
` Last download time: ${
lastDownloadTime !== null ? `${lastDownloadTime.toFixed(3)} s` : "null"
}\n` +
` Recent events:\n` +
nativeInterfaceState.recentEvents.map((event) => `- ${event.type}`).join("\n")
)
}
export {
availableUpdateTitle,
availableUpdateDescription,
errorDescription,
currentlyRunningTitle,
currentlyRunningDescription,
isAvailableUpdateCritical,
manifestMessage,
nativeInterfaceDescription,
}