2021-04-24 05:56:56 -04:00
|
|
|
import './errorHandler'
|
2020-04-24 23:34:04 -04:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
globalShortcut,
|
2020-09-30 05:32:35 -04:00
|
|
|
protocol,
|
|
|
|
Notification
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'electron'
|
|
|
|
import {
|
2021-04-24 00:13:58 -04:00
|
|
|
createProtocol
|
2020-04-24 23:34:04 -04:00
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
2021-04-24 00:13:58 -04:00
|
|
|
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
|
2020-04-24 23:34:04 -04:00
|
|
|
import beforeOpen from '~/main/utils/beforeOpen'
|
|
|
|
import ipcList from '~/main/events/ipcList'
|
|
|
|
import busEventList from '~/main/events/busEventList'
|
2022-01-08 02:44:09 -05:00
|
|
|
import { IWindowList } from '#/types/enum'
|
2020-04-24 23:34:04 -04:00
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
|
|
|
import {
|
2021-07-26 12:15:11 -04:00
|
|
|
updateShortKeyFromVersion212,
|
|
|
|
migrateGalleryFromVersion230
|
2020-04-24 23:34:04 -04:00
|
|
|
} from '~/main/migrate'
|
|
|
|
import {
|
|
|
|
uploadChoosedFiles,
|
|
|
|
uploadClipboardFiles
|
|
|
|
} from 'apis/app/uploader/apis'
|
|
|
|
import {
|
|
|
|
createTray
|
|
|
|
} from 'apis/app/system'
|
|
|
|
import server from '~/main/server/index'
|
|
|
|
import updateChecker from '~/main/utils/updateChecker'
|
|
|
|
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
|
|
|
|
import { getUploadFiles } from '~/main/utils/handleArgv'
|
2021-07-26 12:15:11 -04:00
|
|
|
import db, { GalleryDB } from '~/main/apis/core/datastore'
|
2020-04-24 23:34:04 -04:00
|
|
|
import bus from '@core/bus'
|
2021-04-23 12:44:49 -04:00
|
|
|
import logger from 'apis/core/picgo/logger'
|
2021-08-01 05:02:54 -04:00
|
|
|
import picgo from 'apis/core/picgo'
|
2022-01-09 21:20:56 -05:00
|
|
|
import fixPath from './fixPath'
|
2022-02-20 08:30:22 -05:00
|
|
|
import { initI18n } from '~/main/utils/handleI18n'
|
2020-04-24 23:34:04 -04:00
|
|
|
|
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2021-04-23 12:44:49 -04:00
|
|
|
|
|
|
|
const handleStartUpFiles = (argv: string[], cwd: string) => {
|
|
|
|
const files = getUploadFiles(argv, cwd, logger)
|
|
|
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
|
|
|
if (files === null) {
|
|
|
|
logger.info('cli -> uploading file from clipboard')
|
|
|
|
uploadClipboardFiles()
|
|
|
|
} else {
|
|
|
|
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
|
|
|
|
const win = windowManager.getAvailableWindow()
|
|
|
|
uploadChoosedFiles(win.webContents, files)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
class LifeCycle {
|
2021-08-01 05:02:54 -04:00
|
|
|
private async beforeReady () {
|
2020-04-24 23:34:04 -04:00
|
|
|
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
|
2022-01-08 21:11:14 -05:00
|
|
|
// fix the $PATH in macOS & linux
|
2020-04-24 23:34:04 -04:00
|
|
|
fixPath()
|
|
|
|
beforeOpen()
|
2022-02-20 08:30:22 -05:00
|
|
|
initI18n()
|
2020-04-24 23:34:04 -04:00
|
|
|
ipcList.listen()
|
|
|
|
busEventList.listen()
|
|
|
|
updateShortKeyFromVersion212(db, db.get('settings.shortKey'))
|
2021-08-01 05:02:54 -04:00
|
|
|
await migrateGalleryFromVersion230(db, GalleryDB.getInstance(), picgo)
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onReady () {
|
2021-08-01 05:02:54 -04:00
|
|
|
const readyFunction = async () => {
|
2021-08-01 02:50:25 -04:00
|
|
|
console.log('on ready')
|
2020-04-24 23:34:04 -04:00
|
|
|
createProtocol('picgo')
|
|
|
|
if (isDevelopment && !process.env.IS_TEST) {
|
|
|
|
// Install Vue Devtools
|
|
|
|
try {
|
2021-04-24 00:13:58 -04:00
|
|
|
await installExtension(VUEJS_DEVTOOLS)
|
2022-01-04 10:40:28 -05:00
|
|
|
} catch (e: any) {
|
2020-04-24 23:34:04 -04:00
|
|
|
console.error('Vue Devtools failed to install:', e.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
2020-12-28 20:59:50 -05:00
|
|
|
createTray()
|
2020-04-24 23:34:04 -04:00
|
|
|
db.set('needReload', false)
|
|
|
|
updateChecker()
|
|
|
|
// 不需要阻塞
|
|
|
|
process.nextTick(() => {
|
|
|
|
shortKeyHandler.init()
|
|
|
|
})
|
|
|
|
server.startup()
|
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
2021-04-23 12:44:49 -04:00
|
|
|
handleStartUpFiles(process.argv, process.cwd())
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2020-09-30 05:32:35 -04:00
|
|
|
|
2021-08-20 22:42:52 -04:00
|
|
|
if (global.notificationList && global.notificationList?.length > 0) {
|
|
|
|
while (global.notificationList?.length) {
|
2020-09-30 05:32:35 -04:00
|
|
|
const option = global.notificationList.pop()
|
|
|
|
const notice = new Notification(option!)
|
|
|
|
notice.show()
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 05:02:54 -04:00
|
|
|
}
|
|
|
|
if (!app.isReady()) {
|
|
|
|
app.on('ready', readyFunction)
|
|
|
|
} else {
|
|
|
|
readyFunction()
|
|
|
|
}
|
2020-04-24 23:34:04 -04:00
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onRunning () {
|
|
|
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
2021-04-23 12:44:49 -04:00
|
|
|
logger.info('detect second instance')
|
|
|
|
const result = handleStartUpFiles(commandLine, workingDirectory)
|
|
|
|
if (!result) {
|
2020-04-24 23:34:04 -04:00
|
|
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)!
|
|
|
|
if (settingWindow.isMinimized()) {
|
|
|
|
settingWindow.restore()
|
|
|
|
}
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.on('activate', () => {
|
|
|
|
createProtocol('picgo')
|
|
|
|
if (!windowManager.has(IWindowList.TRAY_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.TRAY_WINDOW)
|
|
|
|
}
|
|
|
|
if (!windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
windowManager.create(IWindowList.SETTING_WINDOW)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
app.setLoginItemSettings({
|
|
|
|
openAtLogin: db.get('settings.autoStart') || false
|
|
|
|
})
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
app.setAppUserModelId('com.molunerfinn.picgo')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.XDG_CURRENT_DESKTOP && process.env.XDG_CURRENT_DESKTOP.includes('Unity')) {
|
|
|
|
process.env.XDG_CURRENT_DESKTOP = 'Unity'
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2020-04-24 23:34:04 -04:00
|
|
|
private onQuit () {
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('will-quit', () => {
|
|
|
|
globalShortcut.unregisterAll()
|
|
|
|
bus.removeAllListeners()
|
|
|
|
server.shutdown()
|
|
|
|
})
|
|
|
|
// Exit cleanly on request from parent process in development mode.
|
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 10:40:28 -05:00
|
|
|
|
2021-07-26 12:15:11 -04:00
|
|
|
async launchApp () {
|
2020-04-24 23:34:04 -04:00
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
if (!gotTheLock) {
|
|
|
|
app.quit()
|
|
|
|
} else {
|
2021-08-01 05:02:54 -04:00
|
|
|
await this.beforeReady()
|
2020-04-24 23:34:04 -04:00
|
|
|
this.onReady()
|
|
|
|
this.onRunning()
|
|
|
|
this.onQuit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bootstrap = new LifeCycle()
|
|
|
|
|
|
|
|
export {
|
|
|
|
bootstrap
|
|
|
|
}
|