diff --git a/src/background.ts b/src/background.ts index 68f06be94..03afd1f71 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,11 +1,10 @@ -'use strict' - +import 'reflect-metadata' // Required by TypoORM. +;('use strict') import { app, protocol, BrowserWindow, ipcMain, shell, Menu } from 'electron' import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer' import { updateConnectionMessage } from '@/api/connection' import { quitAndRenameLogger } from './utils/logger' -import db from './database/index' import updateChecker from './main/updateChecker' import getMenuTemplate from './main/getMenuTemplate' import saveFile from './main/saveFile' @@ -14,13 +13,14 @@ import newWindow from './main/newWindow' import useConnection, { initOptionModel } from '@/database/useConnection' import useServices from '@/database/useServices' -interface WindowSizeModel { - width: number - height: number -} - declare const __static: string +let theme: Theme = 'light' +let autoCheckUpdate: boolean = true +const windowSize = { + width: 1025, + height: 749, +} const isDevelopment: boolean = process.env.NODE_ENV !== 'production' const isMac: boolean = process.platform === 'darwin' @@ -30,10 +30,6 @@ let win: BrowserWindow | null let menu: Menu | null -const windowSize = db.get('windowSize') - -const theme = db.get('settings.currentTheme') - // Scheme must be registered before the app is ready protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]) @@ -85,16 +81,18 @@ function beforeAppQuit() { app.quit() } -async function beforeWindowReady() { - await ConnectionInit({ - doMigrations: true, - undoMigrations: false, - } as initOptionModel) +async function createWindow() { + // Init tables and connect to local database. + await ConnectionInit({ doMigrations: true } as initOptionModel) const { settingService } = useServices() await settingService.setSetting() -} - -function createWindow() { + const setting = await settingService.getSetting() + if (setting) { + theme = setting.currentTheme + autoCheckUpdate = setting.autoCheck + windowSize.height = setting.height + windowSize.width = setting.width + } // Create the browser window. win = new BrowserWindow({ ...windowSize, @@ -133,8 +131,27 @@ function createWindow() { win = null beforeAppQuit() }) + handleIpcMessages() + if (autoCheckUpdate) { + updateChecker() + } } +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', async () => { + if (isDevelopment && !process.env.IS_TEST) { + // Install Vue Devtools + try { + await installExtension(VUEJS_DEVTOOLS) + } catch (e) { + console.error('Vue Devtools failed to install:', e.toString()) + } + } + createWindow() +}) + // Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar @@ -152,27 +169,6 @@ app.on('activate', () => { } }) -// This method will be called when Electron has finished -// initialization and is ready to create browser windows. -// Some APIs can only be used after this event occurs. -app.on('ready', async () => { - await beforeWindowReady() - const autoCheckUpdate = db.get('settings.autoCheck') - if (isDevelopment && !process.env.IS_TEST) { - // Install Vue Devtools - try { - await installExtension(VUEJS_DEVTOOLS) - } catch (e) { - console.error('Vue Devtools failed to install:', e.toString()) - } - } - createWindow() - handleIpcMessages() - if (autoCheckUpdate) { - updateChecker() - } -}) - // Disabled create new window app.on('web-contents-created', (event, contents) => { // tslint:disable-next-line:no-shadowed-variable diff --git a/src/database/services/SettingService.ts b/src/database/services/SettingService.ts index 805625bff..ba1c1e73e 100644 --- a/src/database/services/SettingService.ts +++ b/src/database/services/SettingService.ts @@ -11,6 +11,18 @@ export default class SettingService { ) {} public async setSetting() { + const data = await this.collectionRepository.find() + if (data.length) { + return + } return await this.collectionRepository.insert({}) } + + public async getSetting() { + const data = await this.collectionRepository.find() + if (!data.length) { + return + } + return data[0] + } } diff --git a/src/database/useConnection.ts b/src/database/useConnection.ts index ce2c3ea97..464b93979 100644 --- a/src/database/useConnection.ts +++ b/src/database/useConnection.ts @@ -12,10 +12,11 @@ const useConnection = () => { useContainer(Container) sqlConnection = await createConnection(ORMConfig) if (initOption.doMigrations) { - sqlConnection.runMigrations() + await sqlConnection.runMigrations() } else if (initOption.undoMigrations) { - sqlConnection.undoLastMigration() + await sqlConnection.undoLastMigration() } + return sqlConnection } async function ConnectionDestory() { if (sqlConnection) { diff --git a/src/main.ts b/src/main.ts index 99040d106..f20b4d5e2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,24 +1,24 @@ import 'reflect-metadata' // Required by TypoORM. import Vue from 'vue' +import VueI18n from 'vue-i18n' +import VueClipboard from 'vue-clipboard2' +import path from 'path' +import log4js from 'log4js' import 'element-ui/lib/theme-chalk/index.css' import ElementLocale from 'element-ui/lib/locale' + import App from './App.vue' import router from './router/index' import store from './store/index' -import VueI18n from 'vue-i18n' -import VueClipboard from 'vue-clipboard2' import Lang from './lang' import element from './utils/element' import VueLog4js from './plugins/logPlugin/index' -import log4js from 'log4js' import { getOrCreateLogDir } from './utils/logger' import logConfig from './plugins/logPlugin/logConfig.json' -import path from 'path' import useConnection, { initOptionModel } from './database/useConnection' const { ConnectionInit } = useConnection() - // Init typeORM connection before Vue APP start, after this DI services are available. ConnectionInit({ doMigrations: false, undoMigrations: false } as initOptionModel).then(() => { const LOG_DIR = getOrCreateLogDir()