Repository navigation
Expand file tree
/
Copy pathUpdater Mechanism Code.js
More file actions
96 lines (84 loc) · 3.09 KB
/
Copy pathUpdater Mechanism Code.js
File metadata and controls
96 lines (84 loc) · 3.09 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: wrench;
/*
$$$$$$$$$$$$$$$$$$$$$$
Updater Mechanism Code
by: mvan231
$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$
Information:
to implement this update check you can follow the commented lines of code below.
The method involves setting a variable to the output of the update check function and passing in the current version number of your code. 
$$$$$$$$$$$$$$$$$$$$$$
*/
//provide the script URL where it is located
let scriptURL = 'https://raw.githubusercontent.com/mvan231/Scriptable/main/Updater%20Mechanism%20Code.js'
//uCheck is set up to receive the output of the update check function. It will either be set to true or false depending on whether or not an update is available
let uCheck = await updateCheck(1.0)
log(`${uCheck} to update available`)
//set w as a ListWidget to display the update status if needed
let w = new ListWidget()
if(uCheck){
let upText = w.addText('Update Available')
upText.font = Font.boldSystemFont(8)
}
w.addText("This is a widget")
//finish the script so we can oresent the information
Script.setWidget(w)
Script.complete()
w.presentMedium()
//begin the updateCheck function
async function updateCheck(version){
/*
#####
Update Check
#####
*/
//setup of uC variable to hold the response from the server request
let uC
try{
//load the JSON file located at the same place as the script file but with the additional 'on' at the end
let updateCheck = new Request(`${scriptURL}on`)
uC = await updateCheck.loadJSON()
}catch(e){return log(e)}
log(uC)
log(uC.version)
//setup the 'needUpdate' variable as false unless the server version and the current version don't match
let needUpdate = false
if (uC.version != version){
needUpdate = true
log("Server version available")
if (!config.runsInWidget)
{//if the code is being executed from somewhere other than inside of a widget
log("running standalone")
//generate an alert to notify the user of the update and the changes included
let upd = new Alert()
upd.title="Server Version Available"
//pressing 'OK' downloads the new script file and installs it immediately
upd.addAction("OK")
//pressung 'Later' will exit the updater and continue running the script. This is silent if running in the widget
upd.addDestructiveAction("Later")
upd.message="Changes:\n"+uC.notes+"\n\nPress OK to get the update from GitHub"
if (await upd.present()==0){
let r = new Request(scriptURL)
//download the updated script file
let updatedCode = await r.loadString()
let fm = FileManager.iCloud()
let path = fm.joinPath(fm.documentsDirectory(), `${Script.name()}.js`)
log(path)
//save the downloaded code file to the Scriptable folder in iCloud Drive
fm.writeString(path, updatedCode)
throw new Error("Update Complete!")
}
}
}else{
log("up to date")
}
return needUpdate
/*
#####
End Update Check
#####
*/
}