-
Notifications
You must be signed in to change notification settings - Fork 17
/
TerminalCommandExecuter.ts
125 lines (98 loc) · 4.28 KB
/
TerminalCommandExecuter.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { commands, ThemeIcon, workspace, window, Terminal } from 'vscode';
import { Commands, EXTENSION_NAME, NodeVersionManagers } from '../constants';
import { Subscription } from '../models';
import { Extension } from './Extension';
import { getPlatform } from '../utils';
import { TeamsToolkitIntegration } from './TeamsToolkitIntegration';
import { Folders } from './Folders';
import { join } from 'path';
interface ShellSetting {
path: string;
}
export class TerminalCommandExecuter {
private static shellPath: string | undefined = undefined;
public static register() {
const subscriptions: Subscription[] = Extension.getInstance().subscriptions;
TerminalCommandExecuter.registerCommands(subscriptions);
TerminalCommandExecuter.initShellPath();
}
public static get shell() {
return TerminalCommandExecuter.shellPath;
}
private static initShellPath() {
const shell: string | { path: string } | undefined = TerminalCommandExecuter.getShellPath();
if (typeof shell !== 'string' && !!shell) {
TerminalCommandExecuter.shellPath = shell.path;
} else {
TerminalCommandExecuter.shellPath = shell || undefined;
}
}
/**
* Retrieve the automation profile for the current platform
* @returns
*/
private static getShellPath(): string | ShellSetting | undefined {
const platform = getPlatform();
const terminalSettings = workspace.getConfiguration('terminal');
const automationProfile = terminalSettings.get<string | ShellSetting>(`integrated.automationProfile.${platform}`);
if (!!automationProfile) {
return automationProfile;
}
const defaultProfile = terminalSettings.get<string>(`integrated.defaultProfile.${platform}`);
const profiles = terminalSettings.get<{ [prop: string]: ShellSetting }>(`integrated.profiles.${platform}`);
if (defaultProfile && profiles && profiles[defaultProfile]) {
return profiles[defaultProfile];
}
return terminalSettings.get(`integrated.shell.${platform}`);
}
private static registerCommands(subscriptions: Subscription[]) {
subscriptions.push(
commands.registerCommand(Commands.executeTerminalCommand, TerminalCommandExecuter.runCommand)
);
}
private static async createTerminal(name?: string, icon?: string): Promise<Terminal | undefined> {
let terminal = window.terminals.find(t => t.name === name);
if (!terminal) {
terminal = window.createTerminal({
name: name ? name : undefined,
iconPath: icon ? new ThemeIcon(icon) : undefined
});
// Check the user's settings to see if they want to use nvm or nvs
// Get the user's preferred node version manager -- nvm or nvs or none, if they don't want to use either
const nodeVersionManager: string = TerminalCommandExecuter.getExtensionSettings('nodeVersionManager', 'nvm');
// Check if nvm is used
const nvmFiles = await workspace.findFiles('.nvmrc', '**/node_modules/**');
// If there are .nvmrc files and the user wants to use nvm, then use their preferred node version manager
if (nvmFiles.length > 0 && nodeVersionManager !== NodeVersionManagers.none) {
if (nodeVersionManager === NodeVersionManagers.nvs) {
terminal.sendText('nvs use');
} else {
terminal.sendText('nvm use');
}
}
}
return terminal;
}
private static getExtensionSettings<T>(setting: string, defaultValue: T): T {
return workspace.getConfiguration(EXTENSION_NAME).get<T>(setting, defaultValue);
}
private static async runInTerminal(command: string, terminal?: Terminal | undefined) {
if (terminal) {
terminal.show(true);
terminal.sendText(` ${command}`);
}
}
// eslint-disable-next-line no-unused-vars
public static async runCommand(command: string, args: string[]) {
const terminal = await TerminalCommandExecuter.createTerminal('Gulp task', 'tasks-list-configure');
const wsFolder = await Folders.getWorkspaceFolder();
if (wsFolder) {
let currentProjectPath = wsFolder.uri.fsPath;
if (TeamsToolkitIntegration.isTeamsToolkitProject) {
currentProjectPath = join(currentProjectPath, 'src');
}
TerminalCommandExecuter.runInTerminal(`cd ${currentProjectPath}`, terminal);
}
TerminalCommandExecuter.runInTerminal(command, terminal);
}
}