Skip to content

Commit 7e86618

Browse files
committed
🐛 Fix: give a hint when node.js is not installed
1 parent 96cdfea commit 7e86618

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/main/events/picgoCoreIPC.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,16 @@ const handleGetPluginList = () => {
111111

112112
const handlePluginInstall = () => {
113113
ipcMain.on('installPlugin', async (event: IpcMainEvent, msg: string) => {
114+
const dispose = handleNPMError()
114115
picgo.once('installSuccess', (notice: PicGoNotice) => {
115116
event.sender.send('installSuccess', notice.body[0].replace(/picgo-plugin-/, ''))
116117
shortKeyHandler.registerPluginShortKey(notice.body[0])
117118
picgo.removeAllListeners('installFailed')
119+
dispose()
118120
})
119121
picgo.once('installFailed', () => {
120-
handleNPMError()
121122
picgo.removeAllListeners('installSuccess')
123+
dispose()
122124
})
123125
await picgo.pluginHandler.install([msg])
124126
picgo.cmd.program.removeAllListeners()
@@ -127,14 +129,16 @@ const handlePluginInstall = () => {
127129

128130
const handlePluginUninstall = () => {
129131
ipcMain.on('uninstallPlugin', async (event: IpcMainEvent, msg: string) => {
132+
const dispose = handleNPMError()
130133
picgo.once('uninstallSuccess', (notice: PicGoNotice) => {
131134
event.sender.send('uninstallSuccess', notice.body[0].replace(/picgo-plugin-/, ''))
132135
shortKeyHandler.unregisterPluginShortKey(notice.body[0])
133136
picgo.removeAllListeners('uninstallFailed')
137+
dispose()
134138
})
135139
picgo.once('uninstallFailed', () => {
136-
handleNPMError()
137140
picgo.removeAllListeners('uninstallSuccess')
141+
dispose()
138142
})
139143
await picgo.pluginHandler.uninstall([msg])
140144
picgo.cmd.program.removeAllListeners()
@@ -143,29 +147,37 @@ const handlePluginUninstall = () => {
143147

144148
const handlePluginUpdate = () => {
145149
ipcMain.on('updatePlugin', async (event: IpcMainEvent, msg: string) => {
150+
const dispose = handleNPMError()
146151
picgo.once('updateSuccess', (notice: { body: string[], title: string }) => {
147152
event.sender.send('updateSuccess', notice.body[0].replace(/picgo-plugin-/, ''))
148153
picgo.removeAllListeners('updateFailed')
154+
dispose()
149155
})
150156
picgo.once('updateFailed', () => {
151-
handleNPMError()
152157
picgo.removeAllListeners('updateSuccess')
158+
dispose()
153159
})
154160
await picgo.pluginHandler.update([msg])
155161
picgo.cmd.program.removeAllListeners()
156162
})
157163
}
158164

159-
const handleNPMError = () => {
160-
dialog.showMessageBox({
161-
title: '发生错误',
162-
message: '请安装Node.js并重启PicGo再继续操作',
163-
buttons: ['Yes']
164-
}).then((res) => {
165-
if (res.response === 0) {
166-
shell.openExternal('https://nodejs.org/')
165+
const handleNPMError = (): IDispose => {
166+
const handler = (msg: string) => {
167+
if (msg === 'NPM is not installed') {
168+
dialog.showMessageBox({
169+
title: '发生错误',
170+
message: '请安装Node.js并重启PicGo再继续操作',
171+
buttons: ['Yes']
172+
}).then((res) => {
173+
if (res.response === 0) {
174+
shell.openExternal('https://nodejs.org/')
175+
}
176+
})
167177
}
168-
})
178+
}
179+
picgo.once('failed', handler)
180+
return () => picgo.off('failed', handler)
169181
}
170182

171183
const handleGetPicBedConfig = () => {

src/universal/types/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ interface INPMSearchResultObject {
170170
}
171171
}
172172

173+
type IDispose = () => void
174+
173175
// GuiApi
174176
interface IGuiApi {
175177
showInputBox: (options: IShowInputBoxOption) => Promise<string>

0 commit comments

Comments
 (0)