mirror of
https://github.com/rhysd/Mstdn.git
synced 2025-02-02 06:52:13 +01:00
separate duplicate codes as common.ts
This commit is contained in:
parent
a15e6a25fb
commit
ed99f9fd8f
11
main/app.ts
11
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;
|
||||
|
16
main/common.ts
Normal file
16
main/common.ts
Normal file
@ -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`);
|
||||
}
|
||||
|
@ -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<Config> {
|
||||
return new Promise<Config>(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<Config> {
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
@ -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<Window> {
|
||||
log.debug('Setup a normal window');
|
||||
return new Promise<Window>(resolve => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user