File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed
Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -113,22 +113,33 @@ class CommandEdit extends CommandPolykey {
113113 const editorProc = spawn ( process . env . EDITOR ?? 'nano' , [ tmpFile ] , {
114114 stdio : 'inherit' ,
115115 } ) ;
116- editorProc . on ( 'error' , ( e ) => {
116+ // Define event handlers
117+ const cleanup = ( ) => {
118+ editorProc . removeListener ( 'error' , onError ) ;
119+ editorProc . removeListener ( 'close' , onClose ) ;
120+ } ;
121+ const onError = ( err : Error ) => {
122+ cleanup ( ) ;
117123 const error = new errors . ErrorPolykeyCLIEditSecret (
118124 `Failed to run command ${ process . env . EDITOR } ` ,
119- { cause : e } ,
125+ { cause : err } ,
120126 ) ;
121127 reject ( error ) ;
122- } ) ;
123- editorProc . on ( 'close' , ( code ) => {
128+ } ;
129+ const onClose = ( code : number | null ) => {
130+ cleanup ( ) ;
124131 if ( code !== 0 ) {
125132 const error = new errors . ErrorPolykeyCLIEditSecret (
126133 `Editor exited with code ${ code } ` ,
127134 ) ;
128135 reject ( error ) ;
136+ } else {
137+ resolve ( ) ;
129138 }
130- resolve ( ) ;
131- } ) ;
139+ } ;
140+ // Connect event handlers to events
141+ editorProc . on ( 'error' , onError ) ;
142+ editorProc . on ( 'close' , onClose ) ;
132143 } ) ;
133144 let content : string ;
134145 try {
You can’t perform that action at this time.
0 commit comments