diff --git a/main/app.ts b/main/app.ts index 98c7fbf..1c526b2 100644 --- a/main/app.ts +++ b/main/app.ts @@ -1,19 +1,10 @@ -import * as path from 'path'; import {app, Menu, globalShortcut, Tray} from 'electron'; import log from './log'; import {Config, Account} from './config'; import AccountSwitcher from './account_switcher'; import defaultMenu from './default_menu'; import Window from './window'; - -const IS_DARWIN = process.platform === 'darwin'; -const APP_ICON = path.join(__dirname, '..', 'resources', 'icon', 'icon.png'); - -function trayIcon(color: string) { - return path.join(__dirname, '..', 'resources', 'icon', `tray-icon-${ - color === 'white' ? 'white' : 'black' - }@2x.png`); -} +import {IS_DARWIN, APP_ICON, trayIcon} from './common'; export class App { private switcher: AccountSwitcher; diff --git a/main/common.ts b/main/common.ts new file mode 100644 index 0000000..54176a9 --- /dev/null +++ b/main/common.ts @@ -0,0 +1,16 @@ +import * as path from 'path'; +import {app} from 'electron'; + +export const IS_DEBUG = process.env.NODE_ENV === 'development'; +export const IS_DARWIN = process.platform === 'darwin'; +export const APP_ICON = path.join(__dirname, '..', 'resources', 'icon', 'icon.png'); +export const PRELOAD_JS = path.join(__dirname, '..', 'renderer', 'preload.js'); +export const DATA_DIR = app.getPath('userData'); +export const CONFIG_FILE = path.join(DATA_DIR, 'config.json'); + +export function trayIcon(color: string) { + return path.join(__dirname, '..', 'resources', 'icon', `tray-icon-${ + color === 'white' ? 'white' : 'black' + }@2x.png`); +} + diff --git a/main/config.ts b/main/config.ts index 9789b5c..f50c3b0 100644 --- a/main/config.ts +++ b/main/config.ts @@ -1,7 +1,7 @@ import {app, systemPreferences, dialog, shell} from 'electron'; import * as fs from 'fs'; -import {join} from 'path'; import log from './log'; +import {CONFIG_FILE} from './common'; export interface Account { host: string; @@ -68,22 +68,20 @@ function recommendConfigAndDie(file: string) { export default function loadConfig(): Promise { return new Promise(resolve => { - const dir = app.getPath('userData'); - const file = join(dir, 'config.json'); - fs.readFile(file, 'utf8', (err, json) => { + fs.readFile(CONFIG_FILE, 'utf8', (err, json) => { if (err) { - log.info('Configuration file was not found, will create:', file); + log.info('Configuration file was not found, will create:', CONFIG_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()) { - fs.writeFileSync(file, JSON.stringify(default_config, null, 2)); - recommendConfigAndDie(file); + fs.writeFileSync(CONFIG_FILE, JSON.stringify(default_config, null, 2)); + recommendConfigAndDie(CONFIG_FILE); } else { app.once('ready', () => { - fs.writeFileSync(file, JSON.stringify(default_config, null, 2)); - recommendConfigAndDie(file); + fs.writeFileSync(CONFIG_FILE, JSON.stringify(default_config, null, 2)); + recommendConfigAndDie(CONFIG_FILE); }); } return; @@ -96,7 +94,7 @@ export default function loadConfig(): Promise { } log.debug('Configuration was loaded successfully', config); if (!config.accounts || config.accounts[0].host === '' || config.accounts[0].name === '') { - recommendConfigAndDie(file); + recommendConfigAndDie(CONFIG_FILE); } else { resolve(config); } diff --git a/main/window.ts b/main/window.ts index 418f634..3b11dfa 100644 --- a/main/window.ts +++ b/main/window.ts @@ -1,15 +1,10 @@ -import * as path from 'path'; import {app, BrowserWindow, globalShortcut, shell, dialog, Menu} from 'electron'; import windowState = require('electron-window-state'); import * as menubar from 'menubar'; import {Config, Account} from './config'; import {partitionForAccount} from './account_switcher'; import log from './log'; - -const IS_DEBUG = process.env.NODE_ENV === 'development'; -const IS_DARWIN = process.platform === 'darwin'; -const APP_ICON = path.join(__dirname, '..', 'resources', 'icon', 'icon.png'); -const PRELOAD_JS = path.join(__dirname, '..', 'renderer', 'preload.js'); +import {IS_DEBUG, IS_DARWIN, APP_ICON, PRELOAD_JS, trayIcon} from './common'; export default class Window { static create(account: Account, config: Config, mb: Menubar.MenubarApp | null = null) { @@ -84,12 +79,6 @@ export default class Window { } } -function trayIcon(color: string) { - return path.join(__dirname, '..', 'resources', 'icon', `tray-icon-${ - color === 'white' ? 'white' : 'black' - }@2x.png`); -} - function startNormalWindow(account: Account, config: Config): Promise { log.debug('Setup a normal window'); return new Promise(resolve => {