mirror of
https://github.com/rhysd/Mstdn.git
synced 2025-01-21 20:52:11 +01:00
improve config bootstrapping
This commit is contained in:
parent
8678d93583
commit
3b41501656
@ -1,4 +1,4 @@
|
|||||||
import {app, systemPreferences} from 'electron';
|
import {app, systemPreferences, dialog, shell} from 'electron';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import {join} from 'path';
|
import {join} from 'path';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
@ -28,12 +28,33 @@ function makeDefaultConfig(): Config {
|
|||||||
icon_color: IsDarkMode ? 'white' : 'black',
|
icon_color: IsDarkMode ? 'white' : 'black',
|
||||||
always_on_top: false,
|
always_on_top: false,
|
||||||
normal_window: menubarBroken,
|
normal_window: menubarBroken,
|
||||||
zoom_factor: 1.0,
|
zoom_factor: 0.9,
|
||||||
accounts: [],
|
accounts: [{
|
||||||
|
name: '',
|
||||||
|
host: '',
|
||||||
|
default_page: '/web/timelines/home',
|
||||||
|
}],
|
||||||
keymaps: {},
|
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> {
|
export default function loadConfig(): Promise<Config> {
|
||||||
return new Promise<Config>(resolve => {
|
return new Promise<Config>(resolve => {
|
||||||
const dir = app.getPath('userData');
|
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
|
// 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.
|
// runtime creates data directory. As the result, writeFile() would fail to create a file.
|
||||||
if (app.isReady()) {
|
if (app.isReady()) {
|
||||||
fs.writeFile(file, JSON.stringify(default_config, null, 2));
|
fs.writeFileSync(file, JSON.stringify(default_config, null, 2));
|
||||||
|
recommendConfigAndDie(file);
|
||||||
} else {
|
} 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 {
|
try {
|
||||||
@ -59,10 +84,14 @@ export default function loadConfig(): Promise<Config> {
|
|||||||
config.hot_key = `CmdOrCtrl+${config.hot_key.slice(4)}`;
|
config.hot_key = `CmdOrCtrl+${config.hot_key.slice(4)}`;
|
||||||
}
|
}
|
||||||
log.debug('Configuration was loaded successfully', config);
|
log.debug('Configuration was loaded successfully', config);
|
||||||
|
if (!config.accounts || config.accounts[0].host === '' || config.accounts[0].name === '') {
|
||||||
|
recommendConfigAndDie(file);
|
||||||
|
} else {
|
||||||
resolve(config);
|
resolve(config);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('Error on loading JSON file, will load default configuration:', e.message);
|
log.debug('Error on loading JSON file', e);
|
||||||
resolve(makeDefaultConfig());
|
showDyingDialog('Error on loading JSON file', e.message)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as Mousetrap from 'mousetrap';
|
// import * as Mousetrap from 'mousetrap';
|
||||||
import {Config} from '../main/config';
|
import {Config} from '../main/config';
|
||||||
import * as Ipc from './ipc';
|
import * as Ipc from './ipc';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user