| 63 | } |
| 64 | |
| 65 | export class ExternalEditor { |
| 66 | public editor!: EditorParams; |
| 67 | public lastExitStatus: number = 0; |
| 68 | |
| 69 | private text: string = ''; |
| 70 | private tempFile: string = ''; |
| 71 | private tempDir: string = ''; |
| 72 | private fileOptions: FileOptions = {}; |
| 73 | |
| 74 | constructor(text: string = '', fileOptions: FileOptions = {}) { |
| 75 | this.text = text; |
| 76 | this.fileOptions = fileOptions; |
| 77 | |
| 78 | this.editor = parseEditorCommand( |
| 79 | process.env['VISUAL'] ?? |
| 80 | process.env['EDITOR'] ?? |
| 81 | (process.platform.startsWith('win') ? 'notepad' : 'vim'), |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public run(): string { |
| 86 | this.createTempFile(); |
| 87 | try { |
| 88 | try { |
| 89 | const editorProcess = spawnSync(this.editor.bin, this.editorArgs(), { |
| 90 | shell: false, |
| 91 | stdio: 'inherit', |
| 92 | }); |
| 93 | if (editorProcess.error) { |
| 94 | throw editorProcess.error; |
| 95 | } |
| 96 | this.lastExitStatus = editorProcess.status ?? 0; |
| 97 | } catch (launchError) { |
| 98 | throw new LaunchEditorError(launchError); |
| 99 | } |
| 100 | this.readTemporaryFile(); |
| 101 | return this.text; |
| 102 | } finally { |
| 103 | this.cleanup(); |
| 104 | } |
| 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) { |
nothing calls this directly
no outgoing calls
no test coverage detected