Skip to content

Commit 8bb4f3b

Browse files
committed
add downgrade debug option - move upgrade code into function
1 parent d49275b commit 8bb4f3b

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

src/about.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ import { getAppLogger } from './lib/util'
55
const SCRIPTABLE_DIR = '/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents'
66
const logger = getAppLogger()
77

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+
838
const { present, connect, setState } = getTable<{
939
release: GithubRelease | undefined
1040
currentVersion: string
@@ -138,26 +168,10 @@ const upgrade = connect(({ state: { currentVersion, release } }) => {
138168
title: `Confirm Install - App will update "${appFile}" and auto-close`,
139169
onOptionSelect: async (opt) => {
140170
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()
161175
}
162176
},
163177
})

src/app.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Bluelink, Status, ClimateRequest, ChargeLimit } from './lib/bluelink-re
33
import { getTable, Div, P, Img, quickOptions, DivChild, Spacer, destructiveConfirm } from 'lib/scriptable-utils'
44
import { loadConfigScreen, deleteConfig, setConfig } from 'config'
55
import { Version } from 'lib/version'
6-
import { loadAboutScreen } from 'about'
6+
import { loadAboutScreen, doDowngrade } from 'about'
77
import { deleteWidgetCache } from 'widget'
88
import { getAppLogger } from './lib/util'
99
import { getWidgetLogger } from 'widget'
@@ -166,7 +166,7 @@ const settings = (bl: Bluelink) => {
166166
loadConfigScreen(bl)
167167
},
168168
onTripleTap() {
169-
quickOptions(['Share Debug Logs', 'Reset All Settings', 'Cancel'], {
169+
quickOptions(['Share Debug Logs', 'Reset All Settings', 'Downgrade to Previous Version', 'Cancel'], {
170170
title: 'Choose Debug Option:',
171171
onOptionSelect: (opt) => {
172172
if (opt === 'Cancel') return
@@ -198,6 +198,17 @@ const settings = (bl: Bluelink) => {
198198
})
199199
break
200200
}
201+
case 'Downgrade to Previous Version': {
202+
destructiveConfirm('Confirm downgrade to saved older app version?', {
203+
confirmButtonTitle: 'Yes, downgrade',
204+
onConfirm: () => {
205+
doDowngrade()
206+
// @ts-ignore - undocumented api
207+
App.close()
208+
},
209+
})
210+
break
211+
}
201212
}
202213
},
203214
})

0 commit comments

Comments
 (0)