@@ -5,6 +5,36 @@ import { getAppLogger } from './lib/util'
5
5
const SCRIPTABLE_DIR = '/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents'
6
6
const logger = getAppLogger ( )
7
7
8
+ export function doDowngrade ( appFile = `${ Script . name ( ) } .js` ) {
9
+ const fm = FileManager . iCloud ( )
10
+ if ( fm . fileExists ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` ) ) {
11
+ fm . remove ( `${ SCRIPTABLE_DIR } /${ appFile } ` )
12
+ fm . move ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` , `${ SCRIPTABLE_DIR } /${ appFile } ` )
13
+ } else {
14
+ OK ( 'Downgrade Dailed' , { message : `There is no previous version of ${ appFile } ` } )
15
+ }
16
+ }
17
+
18
+ async function doUpgrade ( url : string , appFile = `${ Script . name ( ) } .js` ) {
19
+ const req = new Request ( url )
20
+ const data = await req . load ( )
21
+ if ( req . response . statusCode === 200 ) {
22
+ const fm = FileManager . iCloud ( )
23
+ // try to backup current script - log errors, script could have been renamed for example
24
+ try {
25
+ if ( fm . fileExists ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` ) ) {
26
+ fm . remove ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` )
27
+ }
28
+ fm . move ( `${ SCRIPTABLE_DIR } /${ appFile } ` , `${ SCRIPTABLE_DIR } /${ appFile } .backup` )
29
+ } catch ( e ) {
30
+ logger . log ( `Failed to backup current script: ${ e } ` )
31
+ }
32
+ fm . write ( `${ SCRIPTABLE_DIR } /${ appFile } ` , data )
33
+ } else {
34
+ OK ( 'Download Error' , { message : `Failed to download release: ${ req . response . statusCode } ` } )
35
+ }
36
+ }
37
+
8
38
const { present, connect, setState } = getTable < {
9
39
release : GithubRelease | undefined
10
40
currentVersion : string
@@ -138,26 +168,10 @@ const upgrade = connect(({ state: { currentVersion, release } }) => {
138
168
title : `Confirm Install - App will update "${ appFile } " and auto-close` ,
139
169
onOptionSelect : async ( opt ) => {
140
170
if ( opt === 'Install' ) {
141
- const req = new Request ( release . url )
142
- const data = await req . load ( )
143
- if ( req . response . statusCode === 200 ) {
144
- const fm = FileManager . iCloud ( )
145
- // try to backup current script - log errors, script could have been renamed for example
146
- try {
147
- if ( fm . fileExists ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` ) ) {
148
- fm . remove ( `${ SCRIPTABLE_DIR } /${ appFile } .backup` )
149
- }
150
- fm . move ( `${ SCRIPTABLE_DIR } /${ appFile } ` , `${ SCRIPTABLE_DIR } /${ appFile } .backup` )
151
- } catch ( e ) {
152
- logger . log ( `Failed to backup current script: ${ e } ` )
153
- }
154
- fm . write ( `${ SCRIPTABLE_DIR } /${ appFile } ` , data )
155
- Script . complete ( )
156
- // @ts -ignore - undocumented api
157
- App . close ( )
158
- } else {
159
- OK ( 'Download Error' , { message : `Failed to download release: ${ req . response . statusCode } ` } )
160
- }
171
+ await doUpgrade ( release . url , appFile )
172
+ Script . complete ( )
173
+ // @ts -ignore - undocumented api
174
+ App . close ( )
161
175
}
162
176
} ,
163
177
} )
0 commit comments