From 259dd244822d7182808ed6fa8cbf764914b61d41 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 13 Jun 2023 23:22:14 +0100 Subject: [PATCH] typescript: Immediately prescribe types to results of readJSON() --- apps/clkinfostopw/settings.ts | 6 ++++-- apps/folderlaunch/app.ts | 4 ++-- apps/folderlaunch/configLoad.js | 4 ++-- apps/folderlaunch/configLoad.ts | 14 +++++++------- apps/folderlaunch/settings.ts | 6 +++--- apps/popconlaunch/boot.js | 2 +- apps/popconlaunch/boot.ts | 2 +- apps/popconlaunch/settings.ts | 2 +- apps/widhid/wid.ts | 2 +- 9 files changed, 22 insertions(+), 20 deletions(-) diff --git a/apps/clkinfostopw/settings.ts b/apps/clkinfostopw/settings.ts index a9a35f245..a7bfce1e5 100644 --- a/apps/clkinfostopw/settings.ts +++ b/apps/clkinfostopw/settings.ts @@ -10,8 +10,10 @@ type StopWatchSettings = { const SETTINGS_FILE = "clkinfostopw.setting.json"; const storage = require("Storage"); - const settings: StopWatchSettings = storage.readJSON(SETTINGS_FILE, true) || {}; - settings.format ??= StopWatchFormat.HMS; + const settings: StopWatchSettings = Object.assign( + { format: StopWatchFormat.HMS }, + storage.readJSON(SETTINGS_FILE, true), + ); const save = () => { storage.writeJSON(SETTINGS_FILE, settings) diff --git a/apps/folderlaunch/app.ts b/apps/folderlaunch/app.ts index 3e2dbacc9..cab686f61 100644 --- a/apps/folderlaunch/app.ts +++ b/apps/folderlaunch/app.ts @@ -115,7 +115,7 @@ // Get the icon and text, skip if the space is empty. Always draw text for folders even if disabled switch (entry.type) { case 'app': - let app: AppInfo = storage.readJSON(entry.id + '.info', false); + let app = storage.readJSON(entry.id + '.info', false) as AppInfo; icon = storage.read(app.icon!)!; text = app.name; empty = false; @@ -186,7 +186,7 @@ switch (entry.type) { case "app": Bangle.buzz(); - let infoFile = storage.readJSON(entry.id + '.info', false); + let infoFile = storage.readJSON(entry.id + '.info', false) as AppInfo; load(infoFile.src); break; case "folder": diff --git a/apps/folderlaunch/configLoad.js b/apps/folderlaunch/configLoad.js index 2c27df1b4..f0610bf62 100644 --- a/apps/folderlaunch/configLoad.js +++ b/apps/folderlaunch/configLoad.js @@ -28,7 +28,7 @@ function cleanAndSave(config) { var installedAppIds = []; for (var _i = 0, infoFiles_1 = infoFiles; _i < infoFiles_1.length; _i++) { var infoFile = infoFiles_1[_i]; - installedAppIds.push(storage.readJSON(infoFile, true).id); + installedAppIds.push((storage.readJSON(infoFile, true) || {}).id); } var toRemove = []; for (var appId in config.apps) @@ -67,7 +67,7 @@ module.exports = { infoFiles.sort(infoFileSorter); for (var _i = 0, infoFiles_2 = infoFiles; _i < infoFiles_2.length; _i++) { var infoFile = infoFiles_2[_i]; - var app_1 = storage.readJSON(infoFile, false); + var app_1 = storage.readJSON(infoFile, false) || {}; if ((!config.showClocks && app_1.type == 'clock') || (!config.showLaunchers && app_1.type == 'launch') || (app_1.type == 'widget') || diff --git a/apps/folderlaunch/configLoad.ts b/apps/folderlaunch/configLoad.ts index 0cb9d59f6..0c1e3c0c1 100644 --- a/apps/folderlaunch/configLoad.ts +++ b/apps/folderlaunch/configLoad.ts @@ -53,7 +53,7 @@ function cleanAndSave(config: Config): Config { let infoFiles: Array = storage.list(/\.info$/); let installedAppIds: Array = []; for (let infoFile of infoFiles) - installedAppIds.push(storage.readJSON(infoFile, true).id); + installedAppIds.push(((storage.readJSON(infoFile, true) || {}) as AppInfo).id); // Remove nonexistent apps from appInfo let toRemove: Array = []; @@ -77,8 +77,8 @@ function cleanAndSave(config: Config): Config { * @return negative if a should go first, positive if b should go first, zero if equivalent. */ let infoFileSorter = (a: string, b: string): number => { - let aJson: AppInfo = storage.readJSON(a, false); - let bJson: AppInfo = storage.readJSON(b, false); + let aJson = storage.readJSON(a, false) as AppInfo; + let bJson = storage.readJSON(b, false) as AppInfo; var n = (0 | aJson.sortorder!) - (0 | bJson.sortorder!); if (n) return n; // do sortorder first if (aJson.name < bJson.name) return -1; @@ -97,7 +97,7 @@ export = { * @return the loaded configuration */ getConfig: (): Config => { - let config = storage.readJSON(SETTINGS_FILE, true) || DEFAULT_CONFIG; + let config = (storage.readJSON(SETTINGS_FILE, true) as Config | undefined) || DEFAULT_CONFIG; // We only need to load data from the filesystem if there is a change if (config.hash == storage.hash(/\.info$/)) { @@ -110,7 +110,7 @@ export = { infoFiles.sort(infoFileSorter); for (let infoFile of infoFiles) { - let app: AppInfo = storage.readJSON(infoFile, false); + let app = storage.readJSON(infoFile, false) as AppInfo | undefined || ({} as AppInfo); // If the app is to be hidden by policy, exclude it completely if ( @@ -138,12 +138,12 @@ export = { // Note: Relies on curFolder secretly being a reference rather than a copy let curFolder: Folder = config.rootFolder; let depth = 0; - for (let folderName of config.apps[app.id].folder) { + for (let folderName of config.apps[app.id]!.folder) { if (curFolder.folders.hasOwnProperty(folderName)) { curFolder = curFolder.folders[folderName]!; depth++; } else { - config.apps[app.id].folder = config.apps[app.id].folder.slice(0, depth); + config.apps[app.id]!.folder = config.apps[app.id]!.folder.slice(0, depth); break; } } diff --git a/apps/folderlaunch/settings.ts b/apps/folderlaunch/settings.ts index 4433b09a2..8848b7cf8 100644 --- a/apps/folderlaunch/settings.ts +++ b/apps/folderlaunch/settings.ts @@ -24,7 +24,7 @@ onchange // Do nothing, but stop typescript from yelling at me for this function being unused. It gets used by eval. I know eval is evil, but the menus are a bit limited. for (let app in config.apps) { - let appInfo: AppInfo = storage.readJSON(app + '.info', false); + let appInfo = storage.readJSON(app + '.info', false) as AppInfo; menu[appInfo.name] = { value: config.hidden.includes(app), format: (value: boolean) => (value ? 'Yes' : 'No'), @@ -36,7 +36,7 @@ }; let getAppInfo = (id: string): AppInfo => { - return storage.readJSON(id + '.info', false); + return storage.readJSON(id + '.info', false) as AppInfo; } let showFolderMenu = (path: Array) => { @@ -142,7 +142,7 @@ } } for (let appId of folder.apps) { - menu[storage.readJSON(appId + '.info', false).name] = () => { }; + menu[(storage.readJSON(appId + '.info', false) as AppInfo).name] = () => { }; } E.showMenu(menu); } diff --git a/apps/popconlaunch/boot.js b/apps/popconlaunch/boot.js index 42c0fc229..e760b501e 100644 --- a/apps/popconlaunch/boot.js +++ b/apps/popconlaunch/boot.js @@ -26,7 +26,7 @@ trimCache(cache); require("Storage").writeJSON("popcon.cache.json", cache); if (orderChanged) { - var info = oldRead("popconlaunch.info", true); + var info = oldRead("popconlaunch.info", true) || { cacheBuster: true }; info.cacheBuster = !info.cacheBuster; require("Storage").writeJSON("popconlaunch.info", info); } diff --git a/apps/popconlaunch/boot.ts b/apps/popconlaunch/boot.ts index f683fc2b1..2802be59f 100644 --- a/apps/popconlaunch/boot.ts +++ b/apps/popconlaunch/boot.ts @@ -38,7 +38,7 @@ const saveCache = (cache: Cache, orderChanged: boolean) => { require("Storage").writeJSON("popcon.cache.json", cache); if(orderChanged){ // ensure launchers reload their caches: - const info = (oldRead("popconlaunch.info", true) as undefined | AppInfo & { cacheBuster?: boolean }) || {}; + const info = (oldRead("popconlaunch.info", true) as undefined | AppInfo & { cacheBuster?: boolean }) || {cacheBuster:true}; info.cacheBuster = !info.cacheBuster; require("Storage").writeJSON("popconlaunch.info", info); } diff --git a/apps/popconlaunch/settings.ts b/apps/popconlaunch/settings.ts index e301df9b2..6f02dc438 100644 --- a/apps/popconlaunch/settings.ts +++ b/apps/popconlaunch/settings.ts @@ -6,7 +6,7 @@ const S = require("Storage"); S.erase("popcon.cache.json"); - const info: AppInfo & { cacheBuster?: boolean } = S.readJSON("popconlaunch.info", true); + const info = S.readJSON("popconlaunch.info", true) as AppInfo & { cacheBuster?: boolean }; info.cacheBuster = !info.cacheBuster; S.writeJSON("popconlaunch.info", info); diff --git a/apps/widhid/wid.ts b/apps/widhid/wid.ts index c8bd2410f..1de293b36 100644 --- a/apps/widhid/wid.ts +++ b/apps/widhid/wid.ts @@ -1,5 +1,5 @@ (() => { - const settings: Settings = require("Storage").readJSON("setting.json", true) || { HID: false } as Settings; + const settings = require("Storage").readJSON("setting.json", true) as Settings || ({ HID: false } as Settings); if (settings.HID !== "kbmedia") { console.log("widhid: can't enable, HID setting isn't \"kbmedia\""); return;