-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-code.ts
81 lines (59 loc) · 1.97 KB
/
update-code.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: check-circle;
'use strict'
const SCRIPTS = [
'utils',
'tiny-dashboard',
'tiny-charts',
'mono-monthly-small',
'update-code',
'monobank',
]
const URL = 'https://raw.githubusercontent.com/Nodman/scriptables/main/build'
// taken from Max Zeryck blur script
const updateCode = async (scriptName: string) => {
const alert = new Alert()
const moduleName = `${scriptName}.js`
const url = `${URL}/${moduleName}`
const path = [...module.filename.split('/').slice(0, -1), moduleName].join('/')
alert.title = `Update "${moduleName}" code?`
alert.message = 'This will overwrite any of your local changes!'
alert.addCancelAction('Nope')
alert.addDestructiveAction('Yesh')
const actionIndex = await alert.present()
if (actionIndex === -1) {
return
}
// Determine if the user is using iCloud.
let files = FileManager.local()
const iCloudInUse = files.isFileStoredIniCloud(path)
// If so, use an iCloud file manager.
files = iCloudInUse ? FileManager.iCloud() : files
let message = ''
// Try to download the file.
try {
const req = new Request(url)
const codeString = await req.loadString()
files.writeString(path, codeString)
message = 'The code has been updated. If the script is open, close it for the change to take effect.'
} catch {
message = 'The update failed. Please try again later.'
}
const confirmAlert = new Alert()
confirmAlert.title = 'Update code'
confirmAlert.message = message
confirmAlert.addCancelAction('Ok')
await confirmAlert.present()
}
const selectScriptAlert = new Alert()
selectScriptAlert.title = 'Select script to update'
selectScriptAlert.addCancelAction('Cancel')
SCRIPTS.forEach(item => {
selectScriptAlert.addAction(item)
})
const actionIndex = await selectScriptAlert.presentSheet()
if (actionIndex !== -1) {
await updateCode(SCRIPTS[actionIndex])
}
export {}