🔨 Refactor: improved code structure for greater clarity and understanding

This commit is contained in:
Molunerfinn 2020-04-10 23:28:46 +08:00
parent cc182b08f7
commit 3df2253ae9
26 changed files with 91 additions and 141 deletions

View File

@ -8,27 +8,27 @@ import {
installVueDevtools
} from 'vue-cli-plugin-electron-builder/lib'
import db from '#/datastore'
import { IWindowList } from '~/main/apis/window/constants'
import windowManager from '~/main/apis/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
import windowManager from 'apis/app/window/windowManager'
import {
uploadChoosedFiles,
uploadClipboardFiles
} from '~/main/apis/uploader/api'
} from 'apis/app/uploader/apis'
import beforeOpen from '~/main/utils/beforeOpen'
import updateChecker from '~/main/utils/updateChecker'
import ipcList from '~/main/apis/eventCenter/ipcList'
import busEventList from '~/main/apis/eventCenter/busEventList'
import ipcList from '~/main/events/ipcList'
import busEventList from '~/main/events/busEventList'
import fixPath from 'fix-path'
import { getUploadFiles } from '~/main/utils/handleArgv'
import bus from '~/main/utils/eventBus'
import bus from '@core/bus'
import {
updateShortKeyFromVersion212
} from '~/main/migrate'
import shortKeyHandler from '~/main/apis/shortKey/shortKeyHandler'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
import server from '~/main/server/index'
import {
createTray
} from '~/main/apis/app'
} from 'apis/app/system'
const isDevelopment = process.env.NODE_ENV !== 'production'
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])

13
src/main/apis/README.md Normal file
View File

@ -0,0 +1,13 @@
# apis folder
## core
The lowest level APIs that are not dependent on each other. The upper APIs depend on them.
## app
Provide key API interfaces for PicGo application, including uploader, window management, shortcut key system, etc
## gui
GuiApi for PicGo plugins.

View File

@ -1,4 +1,4 @@
import bus from '../../utils/eventBus'
import bus from '@core/bus'
import PicGoCore from '~/universal/types/picgo'
import path from 'path'
import {
@ -6,11 +6,11 @@ import {
globalShortcut,
BrowserWindow
} from 'electron'
import logger from '../../utils/logger'
import GuiApi from '../gui'
import logger from '@core/picgo/logger'
import GuiApi from '../../gui'
import db from '#/datastore'
import shortKeyService from './shortKeyService'
import picgo from '../picgo'
import picgo from '@core/picgo'
class ShortKeyHandler {
private isInModifiedMode: boolean = false

View File

@ -1,4 +1,4 @@
import logger from '../../utils/logger'
import logger from '@core/picgo/logger'
class ShortKeyService {
private commandList: Map<string, IShortKeyHandler> = new Map()
registerCommand (command: string, handler: IShortKeyHandler) {

View File

@ -7,12 +7,12 @@ import {
systemPreferences,
Notification
} from 'electron'
import uploader from '~/main/apis/uploader'
import uploader from 'apis/app/uploader'
import getPicBeds from '~/main/utils/getPicBeds'
import db from '#/datastore'
import windowManager from '~/main/apis/window/windowManager'
import { IWindowList } from '~/main/apis/window/constants'
import picgo from '~/main/apis/picgo'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
import picgo from '@core/picgo'
import pasteTemplate from '#/utils/pasteTemplate'
import pkg from 'root/package.json'
import { handleCopyUrl } from '~/main/utils/common'

View File

@ -2,9 +2,9 @@ import {
Notification,
WebContents
} from 'electron'
import windowManager from '~/main/apis/window/windowManager'
import { IWindowList } from '~/main/apis/window/constants'
import uploader from './'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
import uploader from '.'
import pasteTemplate from '#/utils/pasteTemplate'
import db from '#/datastore'
import { handleCopyUrl } from '~/main/utils/common'

View File

@ -6,10 +6,10 @@ import {
WebContents
} from 'electron'
import dayjs from 'dayjs'
import picgo from '~/main/apis/picgo'
import picgo from '@core/picgo'
import db from '#/datastore'
import windowManager from '~/main/apis/window/windowManager'
import { IWindowList } from '~/main/apis/window/constants'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
const waitForShow = (webcontent: WebContents) => {
return new Promise((resolve, reject) => {

View File

@ -8,11 +8,11 @@ import {
RENAME_WINDOW_URL
} from './constants'
import { IWindowListItem } from '#/types/electron'
import bus from '~/main/utils/eventBus'
import bus from '@core/bus'
import db from '#/datastore'
import { getWindowId } from '~/main/apis/bus'
import { getWindowId } from '@core/bus/apis'
import { BrowserWindow, app } from 'electron'
import { CREATE_APP_MENU } from '~/main/apis/bus/constants'
import { CREATE_APP_MENU } from '@core/bus/constants'
const windowList = new Map<IWindowList, IWindowListItem>()
@ -37,14 +37,14 @@ windowList.set(IWindowList.TRAY_WINDOW, {
}
},
callback (window) {
const id = window!.id
window!.loadURL(TRAY_WINDOW_URL)
window!.on('closed', () => {
const id = window.id
window.loadURL(TRAY_WINDOW_URL)
window.on('closed', () => {
bus.emit(DELETE_WINDOW_EVENT, id)
})
window!.on('blur', () => {
window!.hide()
window.on('blur', () => {
window.hide()
})
}
})
@ -82,9 +82,9 @@ windowList.set(IWindowList.SETTING_WINDOW, {
return options
},
callback (window) {
const id = window!.id
window!.loadURL(SETTING_WINDOW_URL)
window!.on('closed', () => {
const id = window.id
window.loadURL(SETTING_WINDOW_URL)
window.on('closed', () => {
bus.emit('toggleShortKeyModifiedMode', false)
bus.emit(DELETE_WINDOW_EVENT, id)
if (process.platform === 'linux') {
@ -125,9 +125,9 @@ windowList.set(IWindowList.MINI_WINDOW, {
return obj
},
callback (window) {
const id = window!.id
window!.loadURL(MINI_WINDOW_URL)
window!.on('closed', () => {
const id = window.id
window.loadURL(MINI_WINDOW_URL)
window.on('closed', () => {
bus.emit(DELETE_WINDOW_EVENT, id)
})
}
@ -159,7 +159,7 @@ windowList.set(IWindowList.RENAME_WINDOW, {
return options
},
async callback (window) {
window!.loadURL(RENAME_WINDOW_URL)
window.loadURL(RENAME_WINDOW_URL)
const currentWindowId = await getWindowId()
const currentWindow = BrowserWindow.fromId(currentWindowId)
if (currentWindow && currentWindow.isVisible()) {
@ -173,7 +173,7 @@ windowList.set(IWindowList.RENAME_WINDOW, {
} else { // if is the miniWindow
positionY = bounds.y + bounds.height / 2
}
window!.setPosition(positionX, positionY, false)
window.setPosition(positionX, positionY, false)
}
}
})

View File

@ -8,7 +8,7 @@ import {
DELETE_WINDOW_EVENT,
CREATE_WINDOW_EVENT
} from './constants'
import bus from '~/main/utils/eventBus'
import bus from '@core/bus'
class WindowManager implements IWindowManager {
private windowMap: Map<IWindowList | string, BrowserWindow> = new Map()

View File

@ -1,4 +1,4 @@
import bus from '../../utils/eventBus'
import bus from '.'
import {
UPLOAD_WITH_FILES,
UPLOAD_WITH_FILES_RESPONSE,

View File

@ -0,0 +1,5 @@
import picgo from '.'
const logger = picgo.log
export default logger

View File

@ -6,13 +6,13 @@ import {
ipcMain
} from 'electron'
import db from '#/datastore'
import uploader from '../uploader'
import uploader from 'apis/app/uploader'
import pasteTemplate from '#/utils/pasteTemplate'
import { handleCopyUrl } from '~/main/utils/common'
import {
getWindowId,
getSettingWindowId
} from '~/main/apis/bus'
} from '@core/bus/apis'
import {
SHOW_INPUT_BOX
} from '~/universal/events/constants'

View File

@ -1,13 +1,13 @@
import bus from '~/main/utils/eventBus'
import bus from '@core/bus'
import {
uploadClipboardFiles,
uploadChoosedFiles
} from '~/main/apis/uploader/api'
} from 'apis/app/uploader/apis'
import {
createMenu
} from '~/main/apis/app'
import { IWindowList } from '~/main/apis/window/constants'
import windowManager from '~/main/apis/window/windowManager'
} from 'apis/app/system'
import { IWindowList } from 'apis/app/window/constants'
import windowManager from 'apis/app/window/windowManager'
import {
UPLOAD_WITH_FILES,
UPLOAD_WITH_FILES_RESPONSE,
@ -18,7 +18,7 @@ import {
GET_SETTING_WINDOW_ID,
GET_SETTING_WINDOW_ID_RESPONSE,
CREATE_APP_MENU
} from '~/main/apis/bus/constants'
} from '@core/bus/constants'
function initEventCenter () {
const eventList: any = {
'picgo:upload': uploadClipboardFiles,

View File

@ -5,19 +5,19 @@ import {
Notification,
IpcMainEvent
} from 'electron'
import windowManager from '~/main/apis/window/windowManager'
import { IWindowList } from '~/main/apis/window/constants'
import uploader from '~/main/apis/uploader'
import windowManager from 'apis/app/window/windowManager'
import { IWindowList } from 'apis/app/window/constants'
import uploader from 'apis/app/uploader'
import pasteTemplate from '#/utils/pasteTemplate'
import db from '#/datastore'
import server from '~/main/server'
import getPicBeds from '~/main/utils/getPicBeds'
import shortKeyHandler from '~/main/apis/shortKey/shortKeyHandler'
import bus from '~/main/utils/eventBus'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
import bus from '@core/bus'
import {
uploadClipboardFiles,
uploadChoosedFiles
} from '~/main/apis/uploader/api'
} from '~/main/apis/app/uploader/apis'
import picgoCoreIPC from './picgoCoreIPC'
import { handleCopyUrl } from '~/main/utils/common'

View File

@ -1,5 +1,5 @@
import path from 'path'
import GuiApi from '../gui'
import GuiApi from 'apis/gui'
import {
dialog,
shell,
@ -9,8 +9,8 @@ import {
} from 'electron'
import PicGoCore from '~/universal/types/picgo'
import { IPicGoHelperType } from '#/types/enum'
import shortKeyHandler from '../shortKey/shortKeyHandler'
import picgo from '~/main/apis/picgo'
import shortKeyHandler from '../apis/app/shortKey/shortKeyHandler'
import picgo from '@core/picgo'
// eslint-disable-next-line
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require

View File

@ -4,8 +4,8 @@ import {
handleResponse,
ensureHTTPLink
} from './utils'
import picgo from '~/main/apis/picgo'
import logger from '~/main/utils/logger'
import picgo from '@core/picgo'
import logger from '@core/picgo/logger'
import axios from 'axios'
class Server {

View File

@ -2,11 +2,11 @@ import router from './router'
import {
uploadWithClipboardFiles,
uploadWithFiles
} from '~/main/apis/bus/index'
} from '@core/bus/apis'
import {
handleResponse
} from './utils'
import logger from '../utils/logger'
import logger from '@core/picgo/logger'
router.post('/upload', async ({
response,

View File

@ -1,4 +1,4 @@
import picgo from '../apis/picgo'
import picgo from '@core/picgo'
const getPicBeds = () => {
const picBedTypes = picgo.helper.uploader.getIdList()

View File

@ -1,76 +0,0 @@
import chalk from 'chalk'
import dayjs from 'dayjs'
import fs from 'fs-extra'
import path from 'path'
import util from 'util'
import db from '#/datastore'
import { app } from 'electron'
import { IChalkType } from '#/types/enum'
const baseDir = app.getPath('userData')
class Logger {
private level = {
success: IChalkType.success,
info: IChalkType.info,
warn: IChalkType.warn,
error: IChalkType.error
}
protected handleLog (type: ILogType, msg: ILoggerType): ILoggerType {
// if configPath is invalid then this.ctx.config === undefined
// if not then check config.silent
const log = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`)
console.log(log, msg)
process.nextTick(() => {
this.handleWriteLog(type, msg)
})
return msg
}
protected handleWriteLog (type: string, msg: ILoggerType): void {
try {
const logLevel = db.get('settings.logLevel')
const logPath = db.get('settings.logPath') || path.join(baseDir, './picgo.log')
if (this.checkLogLevel(type, logLevel)) {
const picgoLog = fs.createWriteStream(logPath, { flags: 'a', encoding: 'utf8' })
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] ${msg}`
const logger = new console.Console(picgoLog)
if (typeof msg === 'object' && type === 'error') {
log += `\n------Error Stack Begin------\n${util.format(msg.stack)}\n-------Error Stack End-------`
}
logger.log(log)
picgoLog.destroy()
}
} catch (e) {
console.log(e)
}
}
protected checkLogLevel (type: string, level: undefined | string | string[]): boolean {
if (level === undefined || level === 'all') {
return true
}
if (Array.isArray(level)) {
return level.some((item: string) => (item === type || item === 'all'))
} else {
return type === level
}
}
success (msg: ILoggerType): ILoggerType {
return this.handleLog('success', msg)
}
info (msg: ILoggerType): ILoggerType {
return this.handleLog('info', msg)
}
error (msg: ILoggerType): ILoggerType {
return this.handleLog('error', msg)
}
warn (msg: ILoggerType): ILoggerType {
return this.handleLog('warn', msg)
}
}
export default new Logger()

View File

@ -3,13 +3,13 @@ import {
} from 'electron'
import {
IWindowList
} from '~/main/apis/window/constants'
} from 'apis/app/window/constants'
declare interface IWindowListItem {
isValid: boolean
multiple: boolean
options: () => IBrowserWindowOptions,
callback: (window: BrowserWindow | null) => void
callback: (window: BrowserWindow) => void
}
declare interface IWindowManager {

View File

@ -30,6 +30,12 @@
],
"#/*": [
"src/universal/*"
],
"apis/*": [
"src/main/apis/*"
],
"@core/*": [
"src/main/apis/core/*"
]
},
"lib": [

View File

@ -20,6 +20,8 @@ module.exports = {
.set('~', resolve('src'))
.set('root', resolve('./'))
.set('#', resolve('src/universal'))
.set('apis', resolve('src/main/apis'))
.set('@core', resolve('src/main/apis/core'))
},
builderOptions: {
productName: 'PicGo',