-
Notifications
You must be signed in to change notification settings - Fork 4
/
updateUtils.ts
91 lines (80 loc) · 3.27 KB
/
updateUtils.ts
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
import type { CurrentlyRunningInfo, UseUpdatesReturnType } from "expo-updates"
// Access 'extra' properties from update manifests
const isAvailableUpdateCritical = (updatesSystem: any) => {
const criticalIndexCurrent =
updatesSystem?.currentlyRunning?.manifest?.extra?.expoClient?.extra?.criticalIndex ?? 0
const criticalIndexUpdate =
updatesSystem?.availableUpdate?.manifest?.extra?.expoClient?.extra?.criticalIndex ?? 0
return criticalIndexUpdate > criticalIndexCurrent
}
const manifestMessage = (manifest: any) => {
return manifest?.extra?.expoClient?.extra?.message ?? ""
}
// Utils for constructing display text
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 { initializationError, checkError, downloadError } = updatesSystem
const initializationErrorDescription = initializationError?.message
? `Error on init: ${initializationError?.message}\n`
: ""
const checkErrorDescription = checkError?.message
? `Error on check: ${checkError?.message}\n`
: ""
const downloadErrorDescription = downloadError?.message
? `Error on download: ${downloadError?.message}\n`
: ""
return initializationErrorDescription + checkErrorDescription + downloadErrorDescription
}
export {
availableUpdateTitle,
availableUpdateDescription,
errorDescription,
currentlyRunningTitle,
currentlyRunningDescription,
isAvailableUpdateCritical,
manifestMessage,
}