1
0
Fork 0

typescript: Immediately prescribe types to results of readJSON()

master
Rob Pilling 2023-06-13 23:22:14 +01:00
parent d848d40a25
commit 259dd24482
9 changed files with 22 additions and 20 deletions

View File

@ -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)

View File

@ -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":

View File

@ -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') ||

View File

@ -53,7 +53,7 @@ function cleanAndSave(config: Config): Config {
let infoFiles: Array<string> = storage.list(/\.info$/);
let installedAppIds: Array<string> = [];
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<string> = [];
@ -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;
}
}

View File

@ -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<string>) => {
@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;