2017-04-16 03:15:35 +09:00
import { app , systemPreferences , dialog , shell } from 'electron' ;
2017-04-15 16:32:44 +09:00
import * as fs from 'fs' ;
import { join } from 'path' ;
import log from './log' ;
export interface Account {
host : string ;
name : string ;
2017-04-15 17:05:32 +09:00
default_page : string ;
2017-04-15 16:32:44 +09:00
}
export interface Config {
hot_key : string ;
icon_color : string ;
always_on_top : boolean ;
normal_window : boolean ;
zoom_factor : number ;
accounts : Account [ ] ;
keymaps : { [ key : string ] : string } ;
}
function makeDefaultConfig ( ) : Config {
const IsDarkMode = ( process . platform === 'darwin' ) && systemPreferences . isDarkMode ( ) ;
const menubarBroken = process . platform === 'win32' ;
return {
hot_key : 'CmdOrCtrl+Shift+S' ,
icon_color : IsDarkMode ? 'white' : 'black' ,
always_on_top : false ,
normal_window : menubarBroken ,
2017-04-16 03:15:35 +09:00
zoom_factor : 0.9 ,
accounts : [ {
name : '' ,
host : '' ,
default_page : '/web/timelines/home' ,
} ] ,
2017-04-16 04:14:58 +09:00
keymaps : {
j : 'scroll-down' ,
k : 'scroll-up' ,
i : 'scroll-top' ,
m : 'scroll-bottom' ,
1 : '/web/timelines/home' ,
2 : '/web/notifications' ,
3 : '/web/timelines/public/local' ,
4 : '/web/timelines/public' ,
5 : '/web/getting-started'
} ,
2017-04-15 16:32:44 +09:00
} ;
}
2017-04-16 03:15:35 +09:00
function showDyingDialog ( title : string , detail : string ) {
dialog . showMessageBox ( {
type : 'info' ,
message : title ,
detail ,
} , ( ) = > {
app . quit ( ) ;
} ) ;
}
function recommendConfigAndDie ( file : string ) {
2017-04-16 12:44:53 +09:00
const title = 'Please write configuration in JSON' ;
2017-04-16 03:15:35 +09:00
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 ) ;
}
2017-04-15 16:32:44 +09:00
export default function loadConfig ( ) : Promise < Config > {
return new Promise < Config > ( resolve = > {
const dir = app . getPath ( 'userData' ) ;
const file = join ( dir , 'config.json' ) ;
fs . readFile ( file , 'utf8' , ( err , json ) = > {
if ( err ) {
log . info ( 'Configuration file was not found, will create:' , file ) ;
const default_config = makeDefaultConfig ( ) ;
// Note:
// 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 ( ) ) {
2017-04-16 03:15:35 +09:00
fs . writeFileSync ( file , JSON . stringify ( default_config , null , 2 ) ) ;
recommendConfigAndDie ( file ) ;
2017-04-15 16:32:44 +09:00
} else {
2017-04-16 03:15:35 +09:00
app . once ( 'ready' , ( ) = > {
fs . writeFileSync ( file , JSON . stringify ( default_config , null , 2 ) ) ;
recommendConfigAndDie ( file ) ;
} ) ;
2017-04-15 16:32:44 +09:00
}
2017-04-16 03:15:35 +09:00
return ;
2017-04-15 16:32:44 +09:00
}
try {
const config = JSON . parse ( json ) ;
if ( config . hot_key && config . hot_key . startsWith ( 'mod+' ) ) {
config . hot_key = ` CmdOrCtrl+ ${ config . hot_key . slice ( 4 ) } ` ;
}
log . debug ( 'Configuration was loaded successfully' , config ) ;
2017-04-16 03:15:35 +09:00
if ( ! config . accounts || config . accounts [ 0 ] . host === '' || config . accounts [ 0 ] . name === '' ) {
recommendConfigAndDie ( file ) ;
} else {
resolve ( config ) ;
}
2017-04-15 16:32:44 +09:00
} catch ( e ) {
2017-04-16 03:15:35 +09:00
log . debug ( 'Error on loading JSON file' , e ) ;
2017-04-16 12:44:53 +09:00
showDyingDialog ( 'Error on loading JSON file' , e . message ) ;
2017-04-15 16:32:44 +09:00
}
} ) ;
} ) ;
}