forked from saurabhdaware/vscode-terminal-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
133 lines (118 loc) · 3.96 KB
/
commands.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const fs = require('fs');
const vscode = require('vscode');
const path = require('path');
const process = require('process');
class TerminalTree{
constructor(terminals){
this.terminals = terminals;
this.i = -1;
}
getChildren(element){
console.log(element);
return this.terminals;
}
getTreeItem(element){
console.log(element);
this.i++;
let terminal = this.terminals[this.i];
return {
label:terminal.label,
tooltip:`Open ${terminal.label}`,
command:{command:terminal.command,title:terminal.commandTitle}
}
}
getParent(element){
console.log(element);
return 'd';
}
}
class TerminalManager{
constructor(context){
this.context = context;
this.terminalsConfig = '';
// Adjust configurations as per the OS
if(process.platform == 'linux'){
this.terminalsConfig = `[
{
"label":"Login bash",
"shellPath":"/bin/bash",
"shellArgs":["-l"]
},
{
"label":"sh",
"shellPath":"/bin/sh"
}
]`
}else if(process.platform == 'win32'){
this.terminalsConfig = `[
{
"label":"Windows Powershell",
"shellPath":"C://Windows//System32//WindowsPowerShell//v1.0//powershell.exe"
},
{
"label":"CMD",
"shellPath":"C://Windows//System32//cmd.exe",
"shellArgs":["/K", "echo Heya!"]
}
]`
}else{
this.terminalsConfig = `[
{
"label":"Bash",
"shellPath":"/bin/bash"
}
]`
}
}
getTerminals(){
let terminals;
console.log("Platform");
console.log(process.platform);
try{
const fileContent = fs.readFileSync(this.context.globalStoragePath+'/terminals.json');
console.log(fileContent);
terminals = JSON.parse(fileContent);
terminals = terminals.map(terminal => {
terminal.command = 'extension.'+terminal.label.toLowerCase().replace(/ /g,'');
terminal.commandTitle = terminal.label;
return terminal;
})
}catch(e){
console.log(e);
fs.promises.mkdir(this.context.globalStoragePath,{recursive:true})
.then(() => {
return fs.promises.writeFile(this.context.globalStoragePath+'/terminals.json',this.terminalsConfig)
})
.catch((e) => {
console.log(e);
})
terminals = [];
}
return terminals;
}
loadTerminals(){
let terminals = this.getTerminals(this.context);
// vscode.workspace.onDidChangeTextDocument()
let tree = vscode.window.createTreeView('terminal-manager', {treeDataProvider: new TerminalTree(terminals)});
this.context.subscriptions.push(tree);
// vscode.window.showInformationMessage('Congratulations, your extension "terminal-manager" is now active!');
for(let terminal of terminals){
let cmd = vscode.commands.registerCommand(terminal.command, function () {
let terminalInstance = vscode.window.createTerminal(terminal.commandTitle,terminal.shellPath,terminal.shellArgs);
terminalInstance.show();
});
this.context.subscriptions.push(cmd);
}
return Promise.resolve(this.context.subscriptions)
}
loadCommands(){
let editTerminals = vscode.commands.registerCommand('extension.editTerminals',() => {
let terminalsJsonPath = path.join(this.context.globalStoragePath,'terminals.json');
vscode.window.showTextDocument(vscode.Uri.file(terminalsJsonPath))
.catch(console.log);
})
this.context.subscriptions.push(editTerminals);
return Promise.resolve(this.context.subscriptions);
}
}
module.exports = TerminalManager;