electron-113: refactored the code as per PR comments

This commit is contained in:
Vishwas Shashidhar 2017-08-23 14:32:58 +05:30
parent 8aa5f50459
commit f2909cdebd
2 changed files with 13 additions and 32 deletions

View File

@ -54,14 +54,18 @@ function getUserConfigField(fieldName) {
});
}
function readUserConfig() {
function readUserConfig(customConfigPath) {
return new Promise(function(resolve, reject) {
if (userConfig) {
resolve(userConfig);
return;
}
let configPath = path.join(app.getPath('userData'), configFileName);
let configPath = customConfigPath;
if (!configPath) {
configPath = path.join(app.getPath('userData'), configFileName);
}
fs.readFile(configPath, 'utf8', function(err, data) {
if (err) {
@ -218,32 +222,6 @@ function updateUserConfig(newGlobalConfig, oldUserConfig) {
});
}
/**
* Method to read user config data
* @param {String} userConfigFile - The user config file path
* @returns {Promise}
*/
function getUserConfigData(userConfigFile) {
return new Promise((resolve, reject) => {
fs.readFile(userConfigFile, 'utf8', (err, data) => {
if (err) {
reject('cannot open user config file: ' + userConfigFile + ', error: ' + err);
return;
}
let userConfigData;
try {
// data is the contents of the text file we just read
userConfigData = JSON.parse(data);
} catch (e) {
reject('can not parse config file data: ' + data + ', error: ' + err);
return;
}
resolve(userConfigData);
});
});
}
/**
* Method to overwrite user config on windows installer
* @param {String} perUserInstall - Is a flag to determine whether we are installing for per user
@ -264,7 +242,7 @@ function updateUserConfigWin(perUserInstall) {
return;
}
Promise.all([readGlobalConfig(), getUserConfigData(userConfigFile)])
Promise.all([readGlobalConfig(), readUserConfig(userConfigFile)])
.then((data) => {
resolve(updateUserConfig(data[0], data[1]));
})
@ -289,13 +267,13 @@ function updateUserConfigMac(globalConfigPath) {
return;
}
Promise.all([readGlobalConfig(), getUserConfigData(userConfigFile)])
Promise.all([readGlobalConfig(), readUserConfig(userConfigFile)])
.then((data) => {
resolve(updateUserConfig(data[0], data[1]));
})
.catch((err) => {
reject(err);
})
});
});
}

View File

@ -120,8 +120,11 @@ function setupThenOpenMainWindow() {
// as the app is launched as a root user we don't get
// access to the config file
let launchOnStartup = process.argv[3];
// We wire this in via the post install script
// to get the config file path where the app is installed
let appGlobalConfigPath = process.argv[2];
setStartup(launchOnStartup)
.then(() => updateUserConfigMac(process.argv[2]))
.then(() => updateUserConfigMac(appGlobalConfigPath))
.then(app.quit)
.catch(app.quit);
return;