mirror of
https://github.com/finos/SymphonyElectron.git
synced 2025-02-25 18:55:29 -06:00
electron-126: fixes issues with user config changes on installation
This commit is contained in:
89
js/config.js
89
js/config.js
@@ -4,24 +4,25 @@ const electron = require('electron');
|
||||
const app = electron.app;
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const AppDirectory = require('appdirectory');
|
||||
const omit = require('lodash.omit');
|
||||
|
||||
const isDevEnv = require('./utils/misc.js').isDevEnv;
|
||||
const isMac = require('./utils/misc.js').isMac;
|
||||
const getRegistry = require('./utils/getRegistry.js');
|
||||
const configFileName = 'Symphony.config';
|
||||
|
||||
// For modifying user config while installation
|
||||
const pick = require('lodash.pick');
|
||||
const AppDirectory = require('appdirectory');
|
||||
const dirs = new AppDirectory('Symphony');
|
||||
|
||||
const log = require('./log.js');
|
||||
const logLevels = require('./enums/logLevels.js');
|
||||
|
||||
const configFileName = 'Symphony.config';
|
||||
const dirs = new AppDirectory('Symphony');
|
||||
|
||||
// cached config when first reading files. initially undefined and will be
|
||||
// updated when read from disk.
|
||||
let userConfig;
|
||||
let globalConfig;
|
||||
|
||||
let ignoreSettings = ['minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'url'];
|
||||
|
||||
/**
|
||||
* Tries to read given field from user config file, if field doesn't exist
|
||||
* then tries reading from global config. User config is stord in directory:
|
||||
@@ -216,37 +217,35 @@ function saveUserConfig(fieldName, newValue, oldConfig) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to update multiple user config field
|
||||
* @param {Object} newGlobalConfig - The latest config changes from installer
|
||||
* @param {Object} oldUserConfig - The old user config data
|
||||
* @returns {Promise}
|
||||
* Clears the existing user config settings including
|
||||
* 'minimizeOnClose', 'launchOnStartup', 'url' and 'alwaysOnTop'
|
||||
* @param {Object} oldUserConfig the old user config object
|
||||
*/
|
||||
function updateUserConfig(newGlobalConfig, oldUserConfig) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
// Picking some values from global config to overwrite user config
|
||||
const configDataToUpdate = pick(newGlobalConfig, ['minimizeOnClose', 'launchOnStartup', 'alwaysOnTop']);
|
||||
const updatedUserConfigData = Object.assign(oldUserConfig, configDataToUpdate);
|
||||
const jsonNewConfig = JSON.stringify(updatedUserConfigData, null, ' ');
|
||||
|
||||
// get user config path
|
||||
let userConfigFile;
|
||||
function clearUserConfig(oldUserConfig) {
|
||||
|
||||
return new Promise((resolve) => {
|
||||
|
||||
// create a new object from the old user config
|
||||
// by ommitting the user related settings from
|
||||
// the old user config
|
||||
let newUserConfig = omit(oldUserConfig, ignoreSettings);
|
||||
let newUserConfigString = JSON.stringify(newUserConfig);
|
||||
|
||||
// get the user config path
|
||||
let userConfigFile;
|
||||
if (isMac) {
|
||||
userConfigFile = path.join(dirs.userConfig(), configFileName);
|
||||
} else {
|
||||
userConfigFile = path.join(app.getPath('userData'), configFileName);
|
||||
}
|
||||
|
||||
fs.writeFile(userConfigFile, jsonNewConfig, 'utf8', (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
// write the new user config changes to the user config file
|
||||
fs.writeFileSync(userConfigFile, newUserConfigString, 'utf-8');
|
||||
|
||||
return resolve();
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,12 +254,12 @@ function updateUserConfig(newGlobalConfig, oldUserConfig) {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function updateUserConfigWin(perUserInstall) {
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
|
||||
// we get the user config path using electron
|
||||
const userConfigFile = path.join(app.getPath('userData'), configFileName);
|
||||
|
||||
|
||||
// if it's not a per user installation or if the
|
||||
// user config file doesn't exist, we simple move on
|
||||
if (!perUserInstall || !fs.existsSync(userConfigFile)) {
|
||||
@@ -272,9 +271,9 @@ function updateUserConfigWin(perUserInstall) {
|
||||
// In case the file exists, we remove it so that all the
|
||||
// values are fetched from the global config
|
||||
// https://perzoinc.atlassian.net/browse/ELECTRON-126
|
||||
Promise.all([readGlobalConfig(), readUserConfig(userConfigFile)])
|
||||
Promise.all([readUserConfig(userConfigFile)])
|
||||
.then((data) => {
|
||||
resolve(updateUserConfig(data[0], data[1]));
|
||||
resolve(clearUserConfig(data[0]));
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
@@ -293,20 +292,24 @@ function updateUserConfigMac() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const userConfigFile = path.join(dirs.userConfig(), configFileName);
|
||||
|
||||
// if user config file does't exists just copy global config file
|
||||
// if user config file does't exist, just use the global config settings
|
||||
// i.e. until an user makes changes manually using the menu items
|
||||
if (!fs.existsSync(userConfigFile)) {
|
||||
log.send(logLevels.WARN, 'config: Could not find the user config file!');
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
Promise.all([readGlobalConfig(), readUserConfig(userConfigFile)])
|
||||
.then((data) => {
|
||||
resolve(updateUserConfig(data[0], data[1]));
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
// In case the file exists, we remove it so that all the
|
||||
// values are fetched from the global config
|
||||
// https://perzoinc.atlassian.net/browse/ELECTRON-126
|
||||
Promise.all([readUserConfig(userConfigFile)])
|
||||
.then((data) => {
|
||||
resolve(clearUserConfig(data[0]));
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -332,4 +335,4 @@ module.exports = {
|
||||
saveUserConfig,
|
||||
clearCachedConfigs
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
@@ -40,8 +40,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
});
|
||||
|
||||
const nodeURL = require('url');
|
||||
|
||||
// hold ref so doesn't get GC'ed
|
||||
const local = {
|
||||
ipcRenderer: ipcRenderer
|
||||
|
||||
10
package-lock.json
generated
10
package-lock.json
generated
@@ -2392,6 +2392,11 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"electron-log": {
|
||||
"version": "2.2.9",
|
||||
"resolved": "https://registry.npmjs.org/electron-log/-/electron-log-2.2.9.tgz",
|
||||
"integrity": "sha512-WNMSipQYurNxY14RO6IKgcxcZg1e4aNVpUUJK9q7Bqe0TZEKn1e5h4HiQKhTgVLqKrUn++ugOZrty450P9vpjA=="
|
||||
},
|
||||
"electron-macos-sign": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/electron-macos-sign/-/electron-macos-sign-1.6.0.tgz",
|
||||
@@ -5147,6 +5152,11 @@
|
||||
"integrity": "sha1-LcvSwofLwKVcxCMovQxzYVDVPj8=",
|
||||
"dev": true
|
||||
},
|
||||
"lodash.omit": {
|
||||
"version": "4.5.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz",
|
||||
"integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA="
|
||||
},
|
||||
"lodash.toarray": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/lodash.toarray/-/lodash.toarray-4.4.0.tgz",
|
||||
|
||||
@@ -103,7 +103,7 @@
|
||||
"electron-squirrel-startup": "^1.0.0",
|
||||
"filesize": "^3.5.10",
|
||||
"keymirror": "0.1.1",
|
||||
"lodash.pick": "^4.4.0",
|
||||
"lodash.omit": "^4.5.0",
|
||||
"winreg": "^1.2.3"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
|
||||
Reference in New Issue
Block a user