@@ -111,14 +111,16 @@ const handleGetPluginList = () => {
111111
112112const 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 ( / p i c g o - p l u g i n - / , '' ) )
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
128130const 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 ( / p i c g o - p l u g i n - / , '' ) )
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
144148const 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 ( / p i c g o - p l u g i n - / , '' ) )
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
171183const handleGetPicBedConfig = ( ) => {
0 commit comments