-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheditor.ts
57 lines (51 loc) · 1.55 KB
/
editor.ts
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
import { getEnvironmentVariables } from './env.js';
import { debug, resolveExecutable } from './misc-utils.js';
/**
* Information about the editor present on the user's computer.
*
* path - The path to the executable representing the editor.
*
* args - Command-line arguments to pass to the executable when
* calling it.
*/
export type Editor = {
path: string;
args: string[];
};
/**
* Looks for an executable that represents a code editor on your computer. Tries
* the `EDITOR` environment variable first, falling back to the executable that
* represents VSCode (`code`).
*
* @returns A promise that contains information about the found editor (path and
* arguments), or null otherwise.
*/
export async function determineEditor(): Promise<Editor | null> {
let executablePath: string | null = null;
const executableArgs: string[] = [];
const { EDITOR } = getEnvironmentVariables();
if (EDITOR !== undefined) {
try {
executablePath = await resolveExecutable(EDITOR);
} catch (error) {
debug(
`Could not resolve executable ${EDITOR} (${error}), falling back to VSCode`,
);
}
}
if (executablePath === null) {
try {
executablePath = await resolveExecutable('code');
// Waits until the file is closed before returning
executableArgs.push('--wait');
} catch (error) {
debug(
`Could not resolve path to VSCode: ${error}, continuing regardless`,
);
}
}
if (executablePath !== null) {
return { path: executablePath, args: executableArgs };
}
return null;
}