-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.ts
97 lines (87 loc) · 2.63 KB
/
extension.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
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
'use strict';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import * as vscode from 'vscode';
interface ShellConfig {
shell: string;
args?: string[];
label?: string;
launchName?: string;
cwd?: string;
}
interface ShellLauncherConfig {
shells: {
linux: ShellConfig[];
osx: ShellConfig[];
windows: ShellConfig[];
};
}
interface ShellQuickPickItem extends vscode.QuickPickItem {
_shell: ShellConfig;
}
function getShells(): ShellConfig[] {
const config = <ShellLauncherConfig>vscode.workspace.getConfiguration().get('shellLauncher');
const shells = config.shells;
if (os.platform() === 'win32') {
return shells.windows;
}
if (os.platform() === 'darwin') {
return shells.osx;
}
return shells.linux;
}
function getShellLabel(shell: ShellConfig) {
if (shell.label) {
return shell.label;
}
return getShellDescription(shell);
}
function getShellDescription(shell: ShellConfig) {
if (!shell.args || shell.args.length === 0) {
return shell.shell;
}
return `${shell.shell} ${shell.args.join(' ')}`;
}
export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand('shellLauncher.launch', () => {
const shells = getShells();
const options: vscode.QuickPickOptions = {
placeHolder: 'Select the shell to launch'
}
const items: ShellQuickPickItem[] = shells.filter(s => {
// If the basename is the same assume it's being pulled from the PATH
if (path.basename(s.shell) === s.shell) {
return true;
}
try {
fs.accessSync(s.shell, fs.constants.R_OK | fs.constants.X_OK);
} catch {
return false;
}
return true;
}).map(s => {
return {
label: getShellLabel(s),
description: getShellDescription(s),
_shell: s
};
});
vscode.window.showQuickPick(items, options).then(item => {
if (!item) {
return;
}
const shell = item._shell;
const terminalOptions: vscode.TerminalOptions = {
cwd: shell.cwd,
name: shell.launchName,
shellPath: shell.shell,
shellArgs: shell.args
};
const terminal = vscode.window.createTerminal(terminalOptions);
terminal.show();
});
});
context.subscriptions.push(disposable);
}
export function deactivate() { }