diff --git a/src/common/JsonFileManager.js b/src/common/JsonFileManager.js new file mode 100644 index 00000000..16a2048c --- /dev/null +++ b/src/common/JsonFileManager.js @@ -0,0 +1,36 @@ +const fs = require('fs'); + +class JsonFileManager { + constructor(file) { + this.jsonFile = file; + try { + this.json = JSON.parse(fs.readFileSync(file, 'utf-8')); + } catch (err) { + this.json = {}; + } + } + + writeToFile() { + fs.writeFile(this.jsonFile, JSON.stringify(this.json, null, 2), (err) => { + if (err) { + console.error(err); + } + }); + } + + setJson(json) { + this.json = json; + this.writeToFile(); + } + + setValue(key, value) { + this.json[key] = value; + this.writeToFile(); + } + + getValue(key) { + return this.json[key]; + } +} + +module.exports = JsonFileManager; diff --git a/src/main.js b/src/main.js index 7a9c1f6f..fb4235fb 100644 --- a/src/main.js +++ b/src/main.js @@ -39,6 +39,7 @@ const downloadURL = require('./main/downloadURL'); const allowProtocolDialog = require('./main/allowProtocolDialog'); const PermissionManager = require('./main/PermissionManager'); const permissionRequestHandler = require('./main/permissionRequestHandler'); +const AppStateManager = require('./main/AppStateManager'); const SpellChecker = require('./main/SpellChecker'); @@ -50,6 +51,7 @@ var mainWindow = null; let spellChecker = null; let deeplinkingUrl = null; let scheme = null; +let appState = null; let permissionManager = null; var argv = require('yargs').parse(process.argv.slice(1)); @@ -69,8 +71,7 @@ var config = {}; try { const configFile = app.getPath('userData') + '/config.json'; config = settings.readFileSync(configFile); - if (config.version !== settings.version || wasUpdated()) { - clearAppCache(); + if (config.version !== settings.version) { config = settings.upgrade(config); settings.writeFileSync(configFile, config); } @@ -189,8 +190,8 @@ function shouldShowTrayIcon() { return false; } -function wasUpdated() { - return config.lastMattermostVersion !== app.getVersion(); +function wasUpdated(lastAppVersion) { + return lastAppVersion !== app.getVersion(); } function clearAppCache() { @@ -370,6 +371,14 @@ app.on('ready', () => { if (global.willAppQuit) { return; } + + const appStateJson = path.join(app.getPath('userData'), 'app-state.json'); + appState = new AppStateManager(appStateJson); + if (wasUpdated(appState.lastAppVersion)) { + clearAppCache(); + } + appState.lastAppVersion = app.getVersion(); + if (global.isDev) { installExtension.default(installExtension.REACT_DEVELOPER_TOOLS). then((name) => console.log(`Added Extension: ${name}`)). diff --git a/src/main/AppStateManager.js b/src/main/AppStateManager.js new file mode 100644 index 00000000..7a0bdca2 --- /dev/null +++ b/src/main/AppStateManager.js @@ -0,0 +1,13 @@ +const JsonFileManager = require('../common/JsonFileManager'); + +class AppStateManager extends JsonFileManager { + set lastAppVersion(version) { + this.setValue('lastAppVersion', version); + } + + get lastAppVersion() { + return this.getValue('lastAppVersion'); + } +} + +module.exports = AppStateManager;