-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (93 loc) · 3.13 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { prepareUser } from '@/api/cache'
import { PREVIEW_WIDGET_SIZE, SCRIPT_START_DATETIME, UPDATE_INTERVAL, setCurrentDatetime } from '@/constants'
import { getLayout } from '@/layout'
import { openSettings } from '@/settings/editor/settingsEditor'
import { handleError } from '@/utils/errors'
import { getModuleFileManager as getFileManagerOptions, readConfig } from '@/utils/scriptable/fileSystem'
import { selectOption } from '@/utils/scriptable/input'
import { KeychainManager } from '@/utils/scriptable/keychainManager'
import { checkForUpdates, shouldCheckForUpdates } from '@/utils/updater'
import { createWidget } from '@/widget'
// initialize the keychain manager
new KeychainManager('untis')
// check for updates (in a try-catch block to prevent the script from crashing if the update fails)
try {
if (shouldCheckForUpdates(UPDATE_INTERVAL)) {
await checkForUpdates()
}
} catch (error) {
console.error('⏫❌ Could not check for updates.')
log(error)
}
async function setupAndCreateWidget() {
const { useICloud, fileManager } = getFileManagerOptions()
const widgetConfig = await readConfig(useICloud)
// update the overriding current date time
const customDatetime = new Date(widgetConfig.debugSettings.customDatetime)
if (customDatetime && !isNaN(customDatetime.getTime())) {
setCurrentDatetime(customDatetime)
}
const user = await prepareUser(widgetConfig)
const widget = await createWidget(user, getLayout(), widgetConfig)
return widget
}
async function presentWidget() {
const widget = await setupAndCreateWidget()
switch (PREVIEW_WIDGET_SIZE) {
case 'small':
widget.presentSmall()
break
case 'medium':
widget.presentMedium()
break
case 'large':
widget.presentLarge()
break
}
}
function showDocumentation() {
console.log('📖 Opening documentation in Safari.')
Safari.openInApp('https://github.com/JFK-05/scriptable-untis#readme')
}
enum ScriptActions {
VIEW = '💻 Show Widget',
OPEN_SETTINGS = '⚙️ Open Settings',
// CHANGE_CREDENTIALS = '🔑 Change Credentials',
// UPDATE = '⏫ Update Script',
// CLEAR_CACHE = '🗑️ Clear Cache',
SHOW_DOCUMENTATION = '📖 Open Documentation',
}
const actionMap: Record<ScriptActions, Function> = {
[ScriptActions.VIEW]: presentWidget,
// [ScriptActions.CHANGE_CREDENTIALS]: fillLoginDataInKeychain,
[ScriptActions.OPEN_SETTINGS]: openSettings,
// [ScriptActions.CLEAR_CACHE]: clearCache,
[ScriptActions.SHOW_DOCUMENTATION]: showDocumentation,
// [ScriptActions.UPDATE]: checkForUpdates,
}
async function runInteractive() {
const actions = Object.values(ScriptActions).filter((item) => {
return isNaN(Number(item))
})
const input = (await selectOption(actions, {
title: 'What do you want to do?',
})) as ScriptActions | null
const action = actionMap[input]
if (!action) {
console.log('No action selected, exiting.')
return
}
await action()
}
try {
if (config.runsInWidget) {
const widget = await setupAndCreateWidget()
Script.setWidget(widget)
} else {
await runInteractive()
}
} catch (error) {
handleError(error)
}
console.log(`Script finished in ${new Date().getTime() - SCRIPT_START_DATETIME.getTime()}ms.`)
Script.complete()