mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-27 01:53:45 +00:00
typescript: Migrate linux-update-util to typescript.
This commit is contained in:
committed by
Akash Nimare
parent
7374fcbe78
commit
8ef39553b8
77
app/renderer/js/utils/linux-update-util.ts
Normal file
77
app/renderer/js/utils/linux-update-util.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import JsonDB from 'node-json-db';
|
||||
|
||||
import electron = require('electron');
|
||||
import Logger = require('./logger-util');
|
||||
|
||||
const remote =
|
||||
process.type === 'renderer' ? electron.remote : electron;
|
||||
|
||||
const logger = new Logger({
|
||||
file: 'linux-update-util.log',
|
||||
timestamp: true
|
||||
});
|
||||
|
||||
/* To make the util runnable in both main and renderer process */
|
||||
const { dialog, app } = remote;
|
||||
let instance: null | LinuxUpdateUtil = null;
|
||||
|
||||
class LinuxUpdateUtil {
|
||||
db: any; // TODO: TypeScript - Figure out how to annotate to JsonDB
|
||||
|
||||
constructor() {
|
||||
if (instance) {
|
||||
return instance;
|
||||
} else {
|
||||
instance = this;
|
||||
}
|
||||
|
||||
this.reloadDB();
|
||||
return instance;
|
||||
}
|
||||
|
||||
getUpdateItem(key: string, defaultValue: any = null): any {
|
||||
this.reloadDB();
|
||||
const value = this.db.getData('/')[key];
|
||||
if (value === undefined) {
|
||||
this.setUpdateItem(key, defaultValue);
|
||||
return defaultValue;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
setUpdateItem(key: string, value: any): void {
|
||||
this.db.push(`/${key}`, value, true);
|
||||
this.reloadDB();
|
||||
}
|
||||
|
||||
removeUpdateItem(key: string): void {
|
||||
this.db.delete(`/${key}`);
|
||||
this.reloadDB();
|
||||
}
|
||||
|
||||
reloadDB(): void {
|
||||
const linuxUpdateJsonPath = path.join(app.getPath('userData'), '/config/updates.json');
|
||||
try {
|
||||
const file = fs.readFileSync(linuxUpdateJsonPath, 'utf8');
|
||||
JSON.parse(file);
|
||||
} catch (err) {
|
||||
if (fs.existsSync(linuxUpdateJsonPath)) {
|
||||
fs.unlinkSync(linuxUpdateJsonPath);
|
||||
dialog.showErrorBox(
|
||||
'Error saving update notifications.',
|
||||
'We encountered an error while saving the update notifications.'
|
||||
);
|
||||
logger.error('Error while JSON parsing updates.json: ');
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
this.db = new JsonDB(linuxUpdateJsonPath, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new LinuxUpdateUtil();
|
||||
Reference in New Issue
Block a user