(callback?: StringCallback)
| 105 | } |
| 106 | |
| 107 | public runAsync(callback?: StringCallback): Promise<string> { |
| 108 | this.createTempFile(); |
| 109 | const promise = new Promise<void>((resolve, reject) => { |
| 110 | try { |
| 111 | const editorProcess = spawn(this.editor.bin, this.editorArgs(), { |
| 112 | shell: false, |
| 113 | stdio: 'inherit', |
| 114 | }); |
| 115 | editorProcess.once('error', (launchError) => { |
| 116 | reject(new LaunchEditorError(launchError)); |
| 117 | }); |
| 118 | editorProcess.once('exit', (code: number | null) => { |
| 119 | this.lastExitStatus = code ?? 0; |
| 120 | resolve(); |
| 121 | }); |
| 122 | } catch (launchError) { |
| 123 | reject(new LaunchEditorError(launchError)); |
| 124 | } |
| 125 | }) |
| 126 | .then(() => { |
| 127 | this.readTemporaryFile(); |
| 128 | return this.text; |
| 129 | }) |
| 130 | .finally(() => { |
| 131 | this.cleanup(); |
| 132 | }); |
| 133 | |
| 134 | if (callback) { |
| 135 | promise.then( |
| 136 | (text) => callback(undefined, text), |
| 137 | (err: unknown) => |
| 138 | callback(err instanceof Error ? err : new Error(String(err)), undefined), |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | return promise; |
| 143 | } |
| 144 | |
| 145 | private cleanup(): void { |
| 146 | if (!this.tempDir) return; |
no test coverage detected