typescript: Immediately prescribe types to results of readJSON()
parent
d848d40a25
commit
259dd24482
|
|
@ -10,8 +10,10 @@ type StopWatchSettings = {
|
||||||
const SETTINGS_FILE = "clkinfostopw.setting.json";
|
const SETTINGS_FILE = "clkinfostopw.setting.json";
|
||||||
|
|
||||||
const storage = require("Storage");
|
const storage = require("Storage");
|
||||||
const settings: StopWatchSettings = storage.readJSON(SETTINGS_FILE, true) || {};
|
const settings: StopWatchSettings = Object.assign(
|
||||||
settings.format ??= StopWatchFormat.HMS;
|
{ format: StopWatchFormat.HMS },
|
||||||
|
storage.readJSON(SETTINGS_FILE, true),
|
||||||
|
);
|
||||||
|
|
||||||
const save = () => {
|
const save = () => {
|
||||||
storage.writeJSON(SETTINGS_FILE, settings)
|
storage.writeJSON(SETTINGS_FILE, settings)
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@
|
||||||
// Get the icon and text, skip if the space is empty. Always draw text for folders even if disabled
|
// Get the icon and text, skip if the space is empty. Always draw text for folders even if disabled
|
||||||
switch (entry.type) {
|
switch (entry.type) {
|
||||||
case 'app':
|
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!)!;
|
icon = storage.read(app.icon!)!;
|
||||||
text = app.name;
|
text = app.name;
|
||||||
empty = false;
|
empty = false;
|
||||||
|
|
@ -186,7 +186,7 @@
|
||||||
switch (entry.type) {
|
switch (entry.type) {
|
||||||
case "app":
|
case "app":
|
||||||
Bangle.buzz();
|
Bangle.buzz();
|
||||||
let infoFile = storage.readJSON(entry.id + '.info', false);
|
let infoFile = storage.readJSON(entry.id + '.info', false) as AppInfo;
|
||||||
load(infoFile.src);
|
load(infoFile.src);
|
||||||
break;
|
break;
|
||||||
case "folder":
|
case "folder":
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ function cleanAndSave(config) {
|
||||||
var installedAppIds = [];
|
var installedAppIds = [];
|
||||||
for (var _i = 0, infoFiles_1 = infoFiles; _i < infoFiles_1.length; _i++) {
|
for (var _i = 0, infoFiles_1 = infoFiles; _i < infoFiles_1.length; _i++) {
|
||||||
var infoFile = infoFiles_1[_i];
|
var infoFile = infoFiles_1[_i];
|
||||||
installedAppIds.push(storage.readJSON(infoFile, true).id);
|
installedAppIds.push((storage.readJSON(infoFile, true) || {}).id);
|
||||||
}
|
}
|
||||||
var toRemove = [];
|
var toRemove = [];
|
||||||
for (var appId in config.apps)
|
for (var appId in config.apps)
|
||||||
|
|
@ -67,7 +67,7 @@ module.exports = {
|
||||||
infoFiles.sort(infoFileSorter);
|
infoFiles.sort(infoFileSorter);
|
||||||
for (var _i = 0, infoFiles_2 = infoFiles; _i < infoFiles_2.length; _i++) {
|
for (var _i = 0, infoFiles_2 = infoFiles; _i < infoFiles_2.length; _i++) {
|
||||||
var infoFile = infoFiles_2[_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') ||
|
if ((!config.showClocks && app_1.type == 'clock') ||
|
||||||
(!config.showLaunchers && app_1.type == 'launch') ||
|
(!config.showLaunchers && app_1.type == 'launch') ||
|
||||||
(app_1.type == 'widget') ||
|
(app_1.type == 'widget') ||
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ function cleanAndSave(config: Config): Config {
|
||||||
let infoFiles: Array<string> = storage.list(/\.info$/);
|
let infoFiles: Array<string> = storage.list(/\.info$/);
|
||||||
let installedAppIds: Array<string> = [];
|
let installedAppIds: Array<string> = [];
|
||||||
for (let infoFile of infoFiles)
|
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
|
// Remove nonexistent apps from appInfo
|
||||||
let toRemove: Array<string> = [];
|
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.
|
* @return negative if a should go first, positive if b should go first, zero if equivalent.
|
||||||
*/
|
*/
|
||||||
let infoFileSorter = (a: string, b: string): number => {
|
let infoFileSorter = (a: string, b: string): number => {
|
||||||
let aJson: AppInfo = storage.readJSON(a, false);
|
let aJson = storage.readJSON(a, false) as AppInfo;
|
||||||
let bJson: AppInfo = storage.readJSON(b, false);
|
let bJson = storage.readJSON(b, false) as AppInfo;
|
||||||
var n = (0 | aJson.sortorder!) - (0 | bJson.sortorder!);
|
var n = (0 | aJson.sortorder!) - (0 | bJson.sortorder!);
|
||||||
if (n) return n; // do sortorder first
|
if (n) return n; // do sortorder first
|
||||||
if (aJson.name < bJson.name) return -1;
|
if (aJson.name < bJson.name) return -1;
|
||||||
|
|
@ -97,7 +97,7 @@ export = {
|
||||||
* @return the loaded configuration
|
* @return the loaded configuration
|
||||||
*/
|
*/
|
||||||
getConfig: (): Config => {
|
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
|
// We only need to load data from the filesystem if there is a change
|
||||||
if (config.hash == storage.hash(/\.info$/)) {
|
if (config.hash == storage.hash(/\.info$/)) {
|
||||||
|
|
@ -110,7 +110,7 @@ export = {
|
||||||
infoFiles.sort(infoFileSorter);
|
infoFiles.sort(infoFileSorter);
|
||||||
|
|
||||||
for (let infoFile of infoFiles) {
|
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 the app is to be hidden by policy, exclude it completely
|
||||||
if (
|
if (
|
||||||
|
|
@ -138,12 +138,12 @@ export = {
|
||||||
// Note: Relies on curFolder secretly being a reference rather than a copy
|
// Note: Relies on curFolder secretly being a reference rather than a copy
|
||||||
let curFolder: Folder = config.rootFolder;
|
let curFolder: Folder = config.rootFolder;
|
||||||
let depth = 0;
|
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)) {
|
if (curFolder.folders.hasOwnProperty(folderName)) {
|
||||||
curFolder = curFolder.folders[folderName]!;
|
curFolder = curFolder.folders[folderName]!;
|
||||||
depth++;
|
depth++;
|
||||||
} else {
|
} 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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.
|
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) {
|
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] = {
|
menu[appInfo.name] = {
|
||||||
value: config.hidden.includes(app),
|
value: config.hidden.includes(app),
|
||||||
format: (value: boolean) => (value ? 'Yes' : 'No'),
|
format: (value: boolean) => (value ? 'Yes' : 'No'),
|
||||||
|
|
@ -36,7 +36,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
let getAppInfo = (id: string): AppInfo => {
|
let getAppInfo = (id: string): AppInfo => {
|
||||||
return storage.readJSON(id + '.info', false);
|
return storage.readJSON(id + '.info', false) as AppInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
let showFolderMenu = (path: Array<string>) => {
|
let showFolderMenu = (path: Array<string>) => {
|
||||||
|
|
@ -142,7 +142,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let appId of folder.apps) {
|
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);
|
E.showMenu(menu);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
trimCache(cache);
|
trimCache(cache);
|
||||||
require("Storage").writeJSON("popcon.cache.json", cache);
|
require("Storage").writeJSON("popcon.cache.json", cache);
|
||||||
if (orderChanged) {
|
if (orderChanged) {
|
||||||
var info = oldRead("popconlaunch.info", true);
|
var info = oldRead("popconlaunch.info", true) || { cacheBuster: true };
|
||||||
info.cacheBuster = !info.cacheBuster;
|
info.cacheBuster = !info.cacheBuster;
|
||||||
require("Storage").writeJSON("popconlaunch.info", info);
|
require("Storage").writeJSON("popconlaunch.info", info);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ const saveCache = (cache: Cache, orderChanged: boolean) => {
|
||||||
require("Storage").writeJSON("popcon.cache.json", cache);
|
require("Storage").writeJSON("popcon.cache.json", cache);
|
||||||
if(orderChanged){
|
if(orderChanged){
|
||||||
// ensure launchers reload their caches:
|
// 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;
|
info.cacheBuster = !info.cacheBuster;
|
||||||
require("Storage").writeJSON("popconlaunch.info", info);
|
require("Storage").writeJSON("popconlaunch.info", info);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
const S = require("Storage");
|
const S = require("Storage");
|
||||||
S.erase("popcon.cache.json");
|
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;
|
info.cacheBuster = !info.cacheBuster;
|
||||||
S.writeJSON("popconlaunch.info", info);
|
S.writeJSON("popconlaunch.info", info);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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") {
|
if (settings.HID !== "kbmedia") {
|
||||||
console.log("widhid: can't enable, HID setting isn't \"kbmedia\"");
|
console.log("widhid: can't enable, HID setting isn't \"kbmedia\"");
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue