typescript: Migrate linux-update-util to typescript.

This commit is contained in:
Priyank Patel
2019-06-19 21:18:24 -04:00
committed by Akash Nimare
parent 7374fcbe78
commit 8ef39553b8

View File

@@ -1,12 +1,14 @@
'use strict'; 'use strict';
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const process = require('process'); import JsonDB from 'node-json-db';
import electron = require('electron');
import Logger = require('./logger-util');
const remote = const remote =
process.type === 'renderer' ? require('electron').remote : require('electron'); process.type === 'renderer' ? electron.remote : electron;
const JsonDB = require('node-json-db');
const Logger = require('./logger-util');
const logger = new Logger({ const logger = new Logger({
file: 'linux-update-util.log', file: 'linux-update-util.log',
@@ -15,10 +17,11 @@ const logger = new Logger({
/* To make the util runnable in both main and renderer process */ /* To make the util runnable in both main and renderer process */
const { dialog, app } = remote; const { dialog, app } = remote;
let instance: null | LinuxUpdateUtil = null;
let instance = null;
class LinuxUpdateUtil { class LinuxUpdateUtil {
db: any; // TODO: TypeScript - Figure out how to annotate to JsonDB
constructor() { constructor() {
if (instance) { if (instance) {
return instance; return instance;
@@ -30,7 +33,7 @@ class LinuxUpdateUtil {
return instance; return instance;
} }
getUpdateItem(key, defaultValue = null) { getUpdateItem(key: string, defaultValue: any = null): any {
this.reloadDB(); this.reloadDB();
const value = this.db.getData('/')[key]; const value = this.db.getData('/')[key];
if (value === undefined) { if (value === undefined) {
@@ -41,17 +44,17 @@ class LinuxUpdateUtil {
} }
} }
setUpdateItem(key, value) { setUpdateItem(key: string, value: any): void {
this.db.push(`/${key}`, value, true); this.db.push(`/${key}`, value, true);
this.reloadDB(); this.reloadDB();
} }
removeUpdateItem(key) { removeUpdateItem(key: string): void {
this.db.delete(`/${key}`); this.db.delete(`/${key}`);
this.reloadDB(); this.reloadDB();
} }
reloadDB() { reloadDB(): void {
const linuxUpdateJsonPath = path.join(app.getPath('userData'), '/config/updates.json'); const linuxUpdateJsonPath = path.join(app.getPath('userData'), '/config/updates.json');
try { try {
const file = fs.readFileSync(linuxUpdateJsonPath, 'utf8'); const file = fs.readFileSync(linuxUpdateJsonPath, 'utf8');