From 7e866185e9330f2719cd35acd48e92dca2b023f0 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Sat, 11 Apr 2020 20:52:04 +0800 Subject: [PATCH] :bug: Fix: give a hint when node.js is not installed --- src/main/events/picgoCoreIPC.ts | 36 ++++++++++++++++++++++----------- src/universal/types/types.d.ts | 2 ++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/events/picgoCoreIPC.ts b/src/main/events/picgoCoreIPC.ts index 7f96cfd5..17998f06 100644 --- a/src/main/events/picgoCoreIPC.ts +++ b/src/main/events/picgoCoreIPC.ts @@ -111,14 +111,16 @@ const handleGetPluginList = () => { const handlePluginInstall = () => { ipcMain.on('installPlugin', async (event: IpcMainEvent, msg: string) => { + const dispose = handleNPMError() picgo.once('installSuccess', (notice: PicGoNotice) => { event.sender.send('installSuccess', notice.body[0].replace(/picgo-plugin-/, '')) shortKeyHandler.registerPluginShortKey(notice.body[0]) picgo.removeAllListeners('installFailed') + dispose() }) picgo.once('installFailed', () => { - handleNPMError() picgo.removeAllListeners('installSuccess') + dispose() }) await picgo.pluginHandler.install([msg]) picgo.cmd.program.removeAllListeners() @@ -127,14 +129,16 @@ const handlePluginInstall = () => { const handlePluginUninstall = () => { ipcMain.on('uninstallPlugin', async (event: IpcMainEvent, msg: string) => { + const dispose = handleNPMError() picgo.once('uninstallSuccess', (notice: PicGoNotice) => { event.sender.send('uninstallSuccess', notice.body[0].replace(/picgo-plugin-/, '')) shortKeyHandler.unregisterPluginShortKey(notice.body[0]) picgo.removeAllListeners('uninstallFailed') + dispose() }) picgo.once('uninstallFailed', () => { - handleNPMError() picgo.removeAllListeners('uninstallSuccess') + dispose() }) await picgo.pluginHandler.uninstall([msg]) picgo.cmd.program.removeAllListeners() @@ -143,29 +147,37 @@ const handlePluginUninstall = () => { const handlePluginUpdate = () => { 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].replace(/picgo-plugin-/, '')) picgo.removeAllListeners('updateFailed') + dispose() }) picgo.once('updateFailed', () => { - handleNPMError() picgo.removeAllListeners('updateSuccess') + dispose() }) await picgo.pluginHandler.update([msg]) picgo.cmd.program.removeAllListeners() }) } -const handleNPMError = () => { - dialog.showMessageBox({ - title: '发生错误', - message: '请安装Node.js并重启PicGo再继续操作', - buttons: ['Yes'] - }).then((res) => { - if (res.response === 0) { - shell.openExternal('https://nodejs.org/') +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 = () => { diff --git a/src/universal/types/types.d.ts b/src/universal/types/types.d.ts index 3a4cabfe..c121b617 100644 --- a/src/universal/types/types.d.ts +++ b/src/universal/types/types.d.ts @@ -170,6 +170,8 @@ interface INPMSearchResultObject { } } +type IDispose = () => void + // GuiApi interface IGuiApi { showInputBox: (options: IShowInputBoxOption) => Promise