electron-113: fixed issues w.r.t. reinstall cases on windows

This commit is contained in:
Vishwas Shashidhar 2017-08-22 21:18:21 +05:30
parent d4807279f6
commit bb939d77ee
3 changed files with 92 additions and 67 deletions

View File

@ -200,9 +200,9 @@ function updateUserConfig(newGlobalConfig, oldUserConfig) {
const configDataToUpdate = pick(newGlobalConfig, ['url', 'minimizeOnClose', 'launchOnStartup', 'alwaysOnTop']); const configDataToUpdate = pick(newGlobalConfig, ['url', 'minimizeOnClose', 'launchOnStartup', 'alwaysOnTop']);
const updatedUserConfigData = Object.assign(oldUserConfig, configDataToUpdate); const updatedUserConfigData = Object.assign(oldUserConfig, configDataToUpdate);
const jsonNewConfig = JSON.stringify(updatedUserConfigData, null, ' '); const jsonNewConfig = JSON.stringify(updatedUserConfigData, null, ' ');
// get user config path // get user config path
let userConfigFile; let userConfigFile;
if (isMac) { if (isMac) {
userConfigFile = path.join(dirs.userConfig(), configFileName); userConfigFile = path.join(dirs.userConfig(), configFileName);
} else { } else {
@ -219,6 +219,32 @@ 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 * Method to overwrite user config on windows installer
* @param {String} perUserInstall - Is a flag to determine whether we are installing for per user * @param {String} perUserInstall - Is a flag to determine whether we are installing for per user
@ -227,7 +253,7 @@ function updateUserConfig(newGlobalConfig, oldUserConfig) {
function updateUserConfigWin(perUserInstall) { function updateUserConfigWin(perUserInstall) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const userConfigFile = path.join(app.getPath('userData'), configFileName); const userConfigFile = path.join(app.getPath('userData'), configFileName);
// flag to determine whether per user installation
if (!perUserInstall) { if (!perUserInstall) {
reject(); reject();
return; return;
@ -235,40 +261,16 @@ function updateUserConfigWin(perUserInstall) {
// if user config file does't exists just copy global config file // if user config file does't exists just copy global config file
if (!fs.existsSync(userConfigFile)) { if (!fs.existsSync(userConfigFile)) {
const globalConfigFileName = path.join('config', configFileName); resolve(copyConfigWin());
const execPath = path.dirname(app.getPath('exe')); return;
const globalConfigPath = path.join(execPath, '', globalConfigFileName); }
const userConfigPath = app.getPath('userData');
childProcess.exec(`echo D|xcopy /y /e /s /c "${globalConfigPath}" "${userConfigPath}"`, { timeout: 60000 }, (err) => { Promise.all([readGlobalConfig(), getUserConfigData(userConfigFile)])
if (err) { .then((data) => {
resolve(updateUserConfig(data[0], data[1]));
})
.catch((err) => {
reject(err); reject(err);
return;
}
resolve();
});
}
readGlobalConfig().then((globalConfigData) => {
fs.readFile(userConfigFile, 'utf8', (err, userData) => {
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(userData);
} catch (e) {
reject('can not parse config file data: ' + userData + ', error: ' + err);
}
updateUserConfig(globalConfigData, userConfigData).then((error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}); });
}); });
} }
@ -284,38 +286,59 @@ function updateUserConfigMac(globalConfigPath) {
// if user config file does't exists just copy global config file // if user config file does't exists just copy global config file
if (!fs.existsSync(userConfigFile)) { if (!fs.existsSync(userConfigFile)) {
resolve(copyConfigMac(globalConfigPath));
return;
}
Promise.all([readGlobalConfig(), getUserConfigData(userConfigFile)])
.then((data) => {
resolve(updateUserConfig(data[0], data[1]));
})
.catch((err) => {
reject(err);
})
});
}
/**
* Method to copy global config file to user config directory for Windows
* @returns {Promise}
*/
function copyConfigWin() {
return new Promise((resolve, reject) => {
const globalConfigFileName = path.join('config', configFileName);
const execPath = path.dirname(app.getPath('exe'));
const globalConfigPath = path.join(execPath, '', globalConfigFileName);
const userConfigPath = app.getPath('userData');
childProcess.exec(`echo D|xcopy /y /e /s /c "${globalConfigPath}" "${userConfigPath}"`, { timeout: 60000 }, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
/**
* Method which copies global config file to user config directory for mac
* @param {String} globalConfigPath - The global config path from installer
* @returns {Promise}
*/
function copyConfigMac(globalConfigPath) {
return new Promise((resolve, reject) => {
let userConfigPath = dirs.userConfig() + '/'; let userConfigPath = dirs.userConfig() + '/';
let userName = process.env.USER; let userName = process.env.USER;
childProcess.exec(`rsync -r "${globalConfigPath}" "${userConfigPath}" && chown -R "${userName}" "${userConfigPath}"`, { timeout: 60000 }, (err) => { childProcess.exec(`rsync -r "${globalConfigPath}" "${userConfigPath}" && chown -R "${userName}" "${userConfigPath}"`, { timeout: 60000 }, (err) => {
if (err) { if (err) {
reject(err); reject(err);
}
resolve();
});
}
return readGlobalConfig().then((globalConfigData) => {
fs.readFile(userConfigFile, 'utf8', (err, userData) => {
if (err) {
reject('cannot open user config file: ' + userConfigFile + ', error: ' + err);
}
let userConfigData;
try {
// data is the contents of the text file we just read
userConfigData = JSON.parse(userData);
} catch (e) {
reject('can not parse config file data: ' + userData + ', error: ' + err);
}
updateUserConfig(globalConfigData, userConfigData).then((error) => {
if (error) {
reject(error);
return; return;
} }
resolve(); resolve();
}); });
}); });
});
});
} }
function clearCachedConfigs() { function clearCachedConfigs() {

View File

@ -108,7 +108,7 @@ function setupThenOpenMainWindow() {
if (!isMac && hasInstallFlag) { if (!isMac && hasInstallFlag) {
getConfigField('launchOnStartup') getConfigField('launchOnStartup')
.then(setStartup) .then(setStartup)
.then(updateUserConfigWin(perUserInstall)) .then(() => updateUserConfigWin(perUserInstall))
.then(app.quit) .then(app.quit)
.catch(app.quit); .catch(app.quit);
return; return;
@ -121,7 +121,7 @@ function setupThenOpenMainWindow() {
// access to the config file // access to the config file
let launchOnStartup = process.argv[3]; let launchOnStartup = process.argv[3];
setStartup(launchOnStartup) setStartup(launchOnStartup)
.then(updateUserConfigMac(process.argv[2])) .then(() => updateUserConfigMac(process.argv[2]))
.then(app.quit) .then(app.quit)
.catch(app.quit); .catch(app.quit);
return; return;

View File

@ -23,7 +23,9 @@
}, },
"jest": { "jest": {
"collectCoverage": true, "collectCoverage": true,
"transformIgnorePatterns": [] "transformIgnorePatterns": [
]
}, },
"build": { "build": {
"asarUnpack": [ "asarUnpack": [