-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
109 lines (86 loc) · 2.26 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import process from 'node:process';
import {execa} from 'execa';
import {getEditor, defaultEditor} from 'env-editor';
import {parseLineColumnPath, stringifyLineColumnPath} from 'line-column-path';
import open from 'open';
export function getEditorInfo(files, options = {}) {
if (!Array.isArray(files)) {
throw new TypeError(`Expected an \`Array\`, got ${typeof files}`);
}
const editor = options.editor ? getEditor(options.editor) : defaultEditor();
const editorArguments = [];
if (['vscode', 'vscodium'].includes(editor.id)) {
editorArguments.push('--goto');
}
for (const file of files) {
const parsed = parseLineColumnPath(file);
if (['sublime', 'atom', 'zed', 'vscode', 'vscodium'].includes(editor.id)) {
editorArguments.push(stringifyLineColumnPath(parsed));
if (options.wait) {
editorArguments.push('--wait');
}
continue;
}
if (['webstorm', 'intellij'].includes(editor.id)) {
editorArguments.push(stringifyLineColumnPath(parsed, {column: false}));
if (options.wait) {
editorArguments.push('--wait');
}
continue;
}
if (editor.id === 'textmate') {
editorArguments.push(
'--line',
stringifyLineColumnPath(parsed, {
file: false,
}),
parsed.file,
);
if (options.wait) {
editorArguments.push('--wait');
}
continue;
}
if (['vim', 'neovim'].includes(editor.id)) {
editorArguments.push(
`+call cursor(${parsed.line}, ${parsed.column})`,
parsed.file,
);
continue;
}
editorArguments.push(parsed.file);
}
return {
binary: editor.binary,
arguments: editorArguments,
isTerminalEditor: editor.isTerminalEditor,
};
}
export default async function openEditor(files, options = {}) {
const result = getEditorInfo(files, options);
const stdio = result.isTerminalEditor ? 'inherit' : 'ignore';
const subprocess = execa(result.binary, result.arguments, {
detached: true,
stdio,
});
// Fallback
subprocess.on('error', () => {
const result = getEditorInfo(files, {
...options,
editor: '',
});
for (const file of result.arguments) {
open(file);
}
});
if (options.wait) {
return new Promise(resolve => {
subprocess.on('exit', resolve);
});
}
if (result.isTerminalEditor) {
subprocess.on('exit', process.exit);
} else {
subprocess.unref();
}
}