PicList/src/main/events/picgoCoreIPC.ts

242 lines
7.3 KiB
TypeScript
Raw Normal View History

import path from 'path'
import GuiApi from 'apis/gui'
import {
dialog,
shell,
IpcMainEvent,
ipcMain,
app
} from 'electron'
2019-12-19 06:17:21 -05:00
import PicGoCore from '~/universal/types/picgo'
2019-12-20 05:45:26 -05:00
import { IPicGoHelperType } from '#/types/enum'
import shortKeyHandler from '../apis/app/shortKey/shortKeyHandler'
import picgo from '@core/picgo'
import { handleStreamlinePluginName } from '~/universal/utils/common'
import { IGuiMenuItem } from 'picgo/dist/src/types'
// eslint-disable-next-line
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
// const PluginHandler = requireFunc('picgo/dist/lib/PluginHandler').default
const STORE_PATH = app.getPath('userData')
// const CONFIG_PATH = path.join(STORE_PATH, '/data.json')
2019-12-19 06:17:21 -05:00
type PicGoNotice = {
title: string,
body: string[]
}
interface GuiMenuItem {
label: string
handle: (arg0: PicGoCore, arg1: GuiApi) => Promise<void>
}
2018-09-19 05:27:09 -04:00
// get uploader or transformer config
2019-12-20 05:45:26 -05:00
const getConfig = (name: string, type: IPicGoHelperType, ctx: PicGoCore) => {
2019-12-19 06:17:21 -05:00
let config: any[] = []
2018-09-19 05:27:09 -04:00
if (name === '') {
return config
} else {
const handler = ctx.helper[type].get(name)
if (handler) {
if (handler.config) {
config = handler.config(ctx)
}
}
return config
}
}
2019-12-19 06:17:21 -05:00
const handleConfigWithFunction = (config: any[]) => {
2018-09-20 03:14:19 -04:00
for (let i in config) {
if (typeof config[i].default === 'function') {
config[i].default = config[i].default()
}
if (typeof config[i].choices === 'function') {
config[i].choices = config[i].choices()
}
}
return config
}
const handleGetPluginList = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('getPluginList', (event: IpcMainEvent) => {
const pluginList = picgo.pluginLoader.getFullList()
const list = []
for (let i in pluginList) {
const plugin = picgo.pluginLoader.getPlugin(pluginList[i])!
const pluginPath = path.join(STORE_PATH, `/node_modules/${pluginList[i]}`)
const pluginPKG = requireFunc(path.join(pluginPath, 'package.json'))
2018-09-19 05:27:09 -04:00
const uploaderName = plugin.uploader || ''
const transformerName = plugin.transformer || ''
let menu: IGuiMenuItem[] = []
2019-01-11 08:13:16 -05:00
if (plugin.guiMenu) {
menu = plugin.guiMenu(picgo)
}
let gui = false
if (pluginPKG.keywords && pluginPKG.keywords.length > 0) {
if (pluginPKG.keywords.includes('picgo-gui-plugin')) {
gui = true
}
}
2019-12-19 06:17:21 -05:00
const obj: IPicGoPlugin = {
name: handleStreamlinePluginName(pluginList[i]),
fullName: pluginList[i],
2018-09-28 23:32:44 -04:00
author: pluginPKG.author.name || pluginPKG.author,
description: pluginPKG.description,
2018-09-28 05:31:22 -04:00
logo: 'file://' + path.join(pluginPath, 'logo.png').split(path.sep).join('/'),
2018-10-10 05:25:44 -04:00
version: pluginPKG.version,
gui,
config: {
2018-09-19 05:27:09 -04:00
plugin: {
fullName: pluginList[i],
name: handleStreamlinePluginName(pluginList[i]),
2018-09-20 03:14:19 -04:00
config: plugin.config ? handleConfigWithFunction(plugin.config(picgo)) : []
2018-09-19 05:27:09 -04:00
},
uploader: {
name: uploaderName,
2019-12-20 05:45:26 -05:00
config: handleConfigWithFunction(getConfig(uploaderName, IPicGoHelperType.uploader, picgo))
2018-09-19 05:27:09 -04:00
},
transformer: {
name: transformerName,
2019-12-20 05:45:26 -05:00
config: handleConfigWithFunction(getConfig(uploaderName, IPicGoHelperType.transformer, picgo))
2018-09-19 05:27:09 -04:00
}
},
enabled: picgo.getConfig(`picgoPlugins.${pluginList[i]}`),
2018-12-18 05:58:31 -05:00
homepage: pluginPKG.homepage ? pluginPKG.homepage : '',
2019-01-11 08:13:16 -05:00
guiMenu: menu,
2018-12-18 05:58:31 -05:00
ing: false
}
list.push(obj)
}
event.sender.send('pluginList', list)
picgo.cmd.program.removeAllListeners()
})
}
2018-09-28 05:31:22 -04:00
const handlePluginInstall = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('installPlugin', async (event: IpcMainEvent, msg: string) => {
const dispose = handleNPMError()
picgo.once('installSuccess', (notice: PicGoNotice) => {
event.sender.send('installSuccess', notice.body[0])
shortKeyHandler.registerPluginShortKey(notice.body[0])
picgo.removeAllListeners('installFailed')
dispose()
2018-09-28 05:31:22 -04:00
})
picgo.once('installFailed', () => {
picgo.removeAllListeners('installSuccess')
dispose()
})
await picgo.pluginHandler.install([msg])
picgo.cmd.program.removeAllListeners()
2018-09-28 05:31:22 -04:00
})
}
const handlePluginUninstall = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('uninstallPlugin', async (event: IpcMainEvent, msg: string) => {
const dispose = handleNPMError()
picgo.once('uninstallSuccess', (notice: PicGoNotice) => {
event.sender.send('uninstallSuccess', notice.body[0])
shortKeyHandler.unregisterPluginShortKey(notice.body[0])
picgo.removeAllListeners('uninstallFailed')
dispose()
2018-09-28 23:32:44 -04:00
})
picgo.once('uninstallFailed', () => {
picgo.removeAllListeners('uninstallSuccess')
dispose()
})
await picgo.pluginHandler.uninstall([msg])
2018-12-20 09:30:08 -05:00
picgo.cmd.program.removeAllListeners()
})
}
const handlePluginUpdate = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('updatePlugin', async (event: IpcMainEvent, msg: string) => {
const dispose = handleNPMError()
picgo.once('updateSuccess', (notice: { body: string[], title: string }) => {
event.sender.send('updateSuccess', notice.body[0])
picgo.removeAllListeners('updateFailed')
dispose()
2018-12-20 09:30:08 -05:00
})
picgo.once('updateFailed', () => {
picgo.removeAllListeners('updateSuccess')
dispose()
})
await picgo.pluginHandler.update([msg])
picgo.cmd.program.removeAllListeners()
2018-09-28 23:32:44 -04:00
})
}
const handleNPMError = (): IDispose => {
const handler = (msg: string) => {
if (msg === 'NPM is not installed') {
dialog.showMessageBox({
title: '发生错误',
message: '请安装Node.js并重启PicGo再继续操作',
buttons: ['Yes']
}).then((res) => {
if (res.response === 0) {
shell.openExternal('https://nodejs.org/')
}
})
}
}
picgo.once('failed', handler)
return () => picgo.off('failed', handler)
}
const handleGetPicBedConfig = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('getPicBedConfig', (event: IpcMainEvent, type: string) => {
const name = picgo.helper.uploader.get(type)?.name || type
if (picgo.helper.uploader.get(type)?.config) {
const config = handleConfigWithFunction(picgo.helper.uploader.get(type)!.config(picgo))
2018-12-24 03:05:30 -05:00
event.sender.send('getPicBedConfig', config, name)
} else {
event.sender.send('getPicBedConfig', [], name)
}
picgo.cmd.program.removeAllListeners()
2018-12-23 10:15:00 -05:00
})
}
const handlePluginActions = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => {
const plugin = picgo.pluginLoader.getPlugin(name)
const guiApi = new GuiApi()
if (plugin?.guiMenu?.(picgo)?.length) {
2019-12-19 06:17:21 -05:00
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
2019-01-11 08:13:16 -05:00
menu.forEach(item => {
if (item.label === label) {
item.handle(picgo, guiApi)
}
})
2019-01-11 04:22:33 -05:00
}
})
}
const handleRemoveFiles = () => {
2019-12-19 06:17:21 -05:00
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
const guiApi = new GuiApi()
setTimeout(() => {
picgo.emit('remove', files, guiApi)
}, 500)
2019-01-11 08:13:16 -05:00
})
}
const handlePicGoSaveData = () => {
ipcMain.on('picgoSaveData', (event: IpcMainEvent, data: IObj) => {
picgo.saveConfig(data)
})
}
2019-09-11 07:30:08 -04:00
export default {
listen () {
handleGetPluginList()
handlePluginInstall()
handlePluginUninstall()
handlePluginUpdate()
handleGetPicBedConfig()
handlePluginActions()
handleRemoveFiles()
handlePicGoSaveData()
}
2018-09-28 05:31:22 -04:00
}