override userData path for desktop

This commit is contained in:
antelle 2017-06-05 13:33:12 +02:00
parent 1b07e77167
commit f4d5e51983
2 changed files with 39 additions and 4 deletions

View File

@ -41,7 +41,12 @@ const Launcher = {
}, callback);
},
getUserDataPath: function(fileName) {
return this.joinPath(this.remoteApp().getPath('userData'), fileName || '');
if (!this.userDataPath) {
const realUserDataPath = this.remoteApp().getPath('userData');
const suffixReplacementRegex = /[\\/]temp[\\/]\d+\.\d+[\\/]?$/;
this.userDataPath = realUserDataPath.replace(suffixReplacementRegex, '');
}
return this.joinPath(this.userDataPath, fileName || '');
},
getTempPath: function(fileName) {
return this.joinPath(this.remoteApp().getPath('temp'), fileName || '');

View File

@ -11,14 +11,19 @@ let appReady = false;
let restartPending = false;
let mainWindowPosition = {};
let updateMainWindowPositionTimeout = null;
const windowPositionFileName = path.join(app.getPath('userData'), 'window-position.json');
const appSettingsFileName = path.join(app.getPath('userData'), 'app-settings.json');
const userDataDir = app.getPath('userData');
const windowPositionFileName = path.join(userDataDir, 'window-position.json');
const appSettingsFileName = path.join(userDataDir, 'app-settings.json');
const tempUserDataPath = path.join(userDataDir, 'temp');
const tempUserDataPathRand = Date.now().toString() + Math.random().toString();
let htmlPath = process.argv.filter(arg => arg.startsWith('--htmlpath=')).map(arg => arg.replace('--htmlpath=', ''))[0];
if (!htmlPath) {
htmlPath = 'file://' + path.join(__dirname, 'index.html');
}
app.setPath('userData', path.join(tempUserDataPath, tempUserDataPathRand));
app.on('window-all-closed', () => {
if (restartPending) {
// unbind all handlers, load new app.js module and pass control to it
@ -29,7 +34,7 @@ app.on('window-all-closed', () => {
electron.globalShortcut.unregisterAll();
electron.powerMonitor.removeAllListeners('suspend');
electron.powerMonitor.removeAllListeners('resume');
const userDataAppFile = path.join(app.getPath('userData'), 'app.js');
const userDataAppFile = path.join(userDataDir, 'app.js');
delete require.cache[require.resolve('./app.js')];
require(userDataAppFile);
app.emit('ready');
@ -46,6 +51,7 @@ app.on('ready', () => {
createMainWindow();
setGlobalShortcuts();
subscribePowerEvents();
deleteOldTempFiles();
}
});
app.on('open-file', (e, path) => {
@ -349,3 +355,27 @@ function subscribePowerEvents() {
emitBackboneEvent('power-monitor-resume');
});
}
function deleteOldTempFiles() {
setTimeout(() => {
for (const dir of fs.readdirSync(tempUserDataPath)) {
if (dir !== tempUserDataPathRand) {
try {
deleteRecursive(path.join(tempUserDataPath, dir));
} catch (e) {}
}
}
}, 1000);
}
function deleteRecursive(dir) {
for (const file of fs.readdirSync(dir)) {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
deleteRecursive(filePath);
} else {
fs.unlinkSync(filePath);
}
}
fs.rmdirSync(dir);
}