-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathIntegratedTerminal.ts
70 lines (60 loc) · 2.26 KB
/
IntegratedTerminal.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
import { Terminal, window, workspace } from 'vscode'
import { getRunExperimentCommand } from './cli/commands'
import { getReadyPythonExtension } from './extensions/python'
import { delay } from './util'
// Static class that creates and holds a reference to an integrated terminal and can run commands in it.
export class IntegratedTerminal {
static termName = 'DVC'
private static instance: Terminal | undefined
static openCurrentInstance = async (): Promise<Terminal | undefined> => {
if (!IntegratedTerminal.instance) {
await IntegratedTerminal.initializeInstance()
}
IntegratedTerminal.instance?.show(true)
return IntegratedTerminal.instance
}
static run = async (command: string): Promise<void> => {
const currentTerminal = await IntegratedTerminal.openCurrentInstance()
return currentTerminal?.sendText(command, true)
}
static dispose = (): void => {
const currentTerminal = IntegratedTerminal.instance
if (currentTerminal) {
currentTerminal.dispose()
IntegratedTerminal.instance = undefined
}
}
private static initializeInstance = async (): Promise<void> => {
IntegratedTerminal.deleteReferenceOnClose()
const pythonExtension = await getReadyPythonExtension()
if (
pythonExtension &&
workspace.getConfiguration().get('python.terminal.activateEnvironment')
) {
return IntegratedTerminal.createInstance(5000)
}
return IntegratedTerminal.createInstance(2000)
}
private static deleteReferenceOnClose = (): void => {
window.onDidCloseTerminal(async event => {
if (
IntegratedTerminal.instance &&
event.name === IntegratedTerminal.termName
) {
IntegratedTerminal.instance = undefined
}
})
}
private static createInstance = async (ms: number): Promise<void> => {
IntegratedTerminal.instance = window.createTerminal({
name: IntegratedTerminal.termName
// hideFromUser: true <- cannot use this as the python extension will not activate the environment
// https://github.com/microsoft/vscode-python/issues/11122
})
return delay(ms)
}
}
export const runExperiment = (): Promise<void> => {
const runExperimentCommand = getRunExperimentCommand()
return IntegratedTerminal.run(runExperimentCommand)
}