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 {app, Menu, globalShortcut, Tray} from 'electron';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
import {Config, Account} from './config';
|
import {Config, Account} from './config';
|
||||||
import AccountSwitcher from './account_switcher';
|
import AccountSwitcher from './account_switcher';
|
||||||
import defaultMenu from './default_menu';
|
import defaultMenu from './default_menu';
|
||||||
import Window from './window';
|
import Window from './window';
|
||||||
|
import {IS_DARWIN, APP_ICON, trayIcon} from './common';
|
||||||
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`);
|
|
||||||
}
|
|
||||||
|
|
||||||
export class App {
|
export class App {
|
||||||
private switcher: AccountSwitcher;
|
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 {app, systemPreferences, dialog, shell} from 'electron';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import {join} from 'path';
|
|
||||||
import log from './log';
|
import log from './log';
|
||||||
|
import {CONFIG_FILE} from './common';
|
||||||
|
|
||||||
export interface Account {
|
export interface Account {
|
||||||
host: string;
|
host: string;
|
||||||
@ -68,22 +68,20 @@ function recommendConfigAndDie(file: string) {
|
|||||||
|
|
||||||
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');
|
fs.readFile(CONFIG_FILE, 'utf8', (err, json) => {
|
||||||
const file = join(dir, 'config.json');
|
|
||||||
fs.readFile(file, 'utf8', (err, json) => {
|
|
||||||
if (err) {
|
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();
|
const default_config = makeDefaultConfig();
|
||||||
// Note:
|
// Note:
|
||||||
// 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.writeFileSync(file, JSON.stringify(default_config, null, 2));
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify(default_config, null, 2));
|
||||||
recommendConfigAndDie(file);
|
recommendConfigAndDie(CONFIG_FILE);
|
||||||
} else {
|
} else {
|
||||||
app.once('ready', () => {
|
app.once('ready', () => {
|
||||||
fs.writeFileSync(file, JSON.stringify(default_config, null, 2));
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify(default_config, null, 2));
|
||||||
recommendConfigAndDie(file);
|
recommendConfigAndDie(CONFIG_FILE);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -96,7 +94,7 @@ export default function loadConfig(): Promise<Config> {
|
|||||||
}
|
}
|
||||||
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 === '') {
|
if (!config.accounts || config.accounts[0].host === '' || config.accounts[0].name === '') {
|
||||||
recommendConfigAndDie(file);
|
recommendConfigAndDie(CONFIG_FILE);
|
||||||
} else {
|
} else {
|
||||||
resolve(config);
|
resolve(config);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
import * as path from 'path';
|
|
||||||
import {app, BrowserWindow, globalShortcut, shell, dialog, Menu} from 'electron';
|
import {app, BrowserWindow, globalShortcut, shell, dialog, Menu} from 'electron';
|
||||||
import windowState = require('electron-window-state');
|
import windowState = require('electron-window-state');
|
||||||
import * as menubar from 'menubar';
|
import * as menubar from 'menubar';
|
||||||
import {Config, Account} from './config';
|
import {Config, Account} from './config';
|
||||||
import {partitionForAccount} from './account_switcher';
|
import {partitionForAccount} from './account_switcher';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
|
import {IS_DEBUG, IS_DARWIN, APP_ICON, PRELOAD_JS, trayIcon} from './common';
|
||||||
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');
|
|
||||||
|
|
||||||
export default class Window {
|
export default class Window {
|
||||||
static create(account: Account, config: Config, mb: Menubar.MenubarApp | null = null) {
|
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> {
|
function startNormalWindow(account: Account, config: Config): Promise<Window> {
|
||||||
log.debug('Setup a normal window');
|
log.debug('Setup a normal window');
|
||||||
return new Promise<Window>(resolve => {
|
return new Promise<Window>(resolve => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user