-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathopen-editor.ts
More file actions
43 lines (34 loc) · 904 Bytes
/
open-editor.ts
File metadata and controls
43 lines (34 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {ChildProcess, spawn} from 'node:child_process'
interface EditorOptions {
editor?: string
}
export function openEditor(
file: string,
opts: EditorOptions = {},
): Promise<void> {
return new Promise((resolve, reject) => {
const editor = getEditor(opts.editor)
const args = editor.split(/\s+/)
const bin = args.shift()
if (!bin) {
reject(new Error('Editor binary not found'))
return
}
const ps: ChildProcess = spawn(bin, [...args, file], {stdio: 'inherit'})
ps.on('exit', () => {
resolve()
})
ps.on('error', (err: Error) => {
reject(err)
})
})
}
function getEditor(editor?: string): string {
return (
editor || process.env.VISUAL || process.env.EDITOR || getDefaultEditor()
)
}
function getDefaultEditor(): string {
return process.platform.startsWith('win') ? 'notepad' : 'vim'
}
export default openEditor