1
0
mirror of https://github.com/rhysd/Mstdn.git synced 2025-01-20 19:52:10 +01:00

improve config bootstrapping

This commit is contained in:
rhysd 2017-04-16 03:15:35 +09:00
parent 8678d93583
commit 3b41501656
2 changed files with 39 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import {app, systemPreferences} from 'electron';
import {app, systemPreferences, dialog, shell} from 'electron';
import * as fs from 'fs';
import {join} from 'path';
import log from './log';
@ -28,12 +28,33 @@ function makeDefaultConfig(): Config {
icon_color: IsDarkMode ? 'white' : 'black',
always_on_top: false,
normal_window: menubarBroken,
zoom_factor: 1.0,
accounts: [],
zoom_factor: 0.9,
accounts: [{
name: '',
host: '',
default_page: '/web/timelines/home',
}],
keymaps: {},
};
}
function showDyingDialog(title: string, detail: string) {
dialog.showMessageBox({
type: 'info',
message: title,
detail,
}, () => {
app.quit();
});
}
function recommendConfigAndDie(file: string) {
const title = 'Please write configuration in JSON'
const detail = 'You need to write up name and host in first item of accounts. Restart this app after writing up them. Please see README for more detail: https://github.com/rhysd/Mstdn#readme';
shell.openItem(file);
showDyingDialog(title, detail);
}
export default function loadConfig(): Promise<Config> {
return new Promise<Config>(resolve => {
const dir = app.getPath('userData');
@ -46,11 +67,15 @@ export default function loadConfig(): Promise<Config> {
// If calling writeFile() directly here, it tries to create config file before Electron
// runtime creates data directory. As the result, writeFile() would fail to create a file.
if (app.isReady()) {
fs.writeFile(file, JSON.stringify(default_config, null, 2));
fs.writeFileSync(file, JSON.stringify(default_config, null, 2));
recommendConfigAndDie(file);
} else {
app.once('ready', () => fs.writeFile(file, JSON.stringify(default_config, null, 2)));
app.once('ready', () => {
fs.writeFileSync(file, JSON.stringify(default_config, null, 2));
recommendConfigAndDie(file);
});
}
return resolve(default_config);
return;
}
try {
@ -59,10 +84,14 @@ export default function loadConfig(): Promise<Config> {
config.hot_key = `CmdOrCtrl+${config.hot_key.slice(4)}`;
}
log.debug('Configuration was loaded successfully', config);
resolve(config);
if (!config.accounts || config.accounts[0].host === '' || config.accounts[0].name === '') {
recommendConfigAndDie(file);
} else {
resolve(config);
}
} catch (e) {
log.error('Error on loading JSON file, will load default configuration:', e.message);
resolve(makeDefaultConfig());
log.debug('Error on loading JSON file', e);
showDyingDialog('Error on loading JSON file', e.message)
}
});
});

View File

@ -1,4 +1,4 @@
import * as Mousetrap from 'mousetrap';
// import * as Mousetrap from 'mousetrap';
import {Config} from '../main/config';
import * as Ipc from './ipc';
import log from './log';