-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
90 lines (81 loc) · 2.21 KB
/
extension.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
const vscode = require('vscode');
const exec = require('child_process').exec;
async function executeJSON(command) {
exec(command, function (error, stdout, stderr) {
if (error) {
vscode.window.showInformationMessage(error);
} else if (stdout) {
vscode.workspace.openTextDocument({
content: '// Response:\n\n' + JSON.stringify(JSON.parse(stdout), undefined, 4),
language: 'jsonc'
}).then(document => {
vscode.window.showTextDocument(document, {
viewColumn: vscode.ViewColumn.Beside
});
});
} else {
vscode.window.showInformationMessage(stderr);
}
});
};
async function execute(command) {
exec(command, function (error, stdout, stderr) {
if (error) {
vscode.window.showInformationMessage(error);
} else if (stdout) {
vscode.workspace.openTextDocument({
content: 'Response:\n\n' + stdout,
}).then(document => {
vscode.window.showTextDocument(document, {
viewColumn: vscode.ViewColumn.Beside
});
});
} else {
vscode.window.showInformationMessage(stderr);
}
});
};
async function GetSelectedText() {
let editor = vscode.window.activeTextEditor;
let commandLine = editor.document.lineAt(editor.selection.start.line).text;
if (commandLine.includes('grpc_cli')) {
grpcCliClient(editor, commandLine)
} else if (commandLine.includes('grpcurl')) {
grpcUrlClient(editor, commandLine)
} else {
vscode.window.showInformationMessage('Can\'t recognize a command');
}
}
async function grpcCliClient(editor, cLine) {
var text = editor.document.getText(editor.selection);
let body = text.replace(cLine, '')
if (body) {
let exeCommand = cLine + " '" + JSON.stringify(JSON.parse(body)) + "'"
executeJSON(exeCommand)
} else {
execute(cLine)
}
}
async function grpcUrlClient(editor, cLine) {
var text = editor.document.getText(editor.selection);
if (text.includes('-d')) {
executeJSON(cLine)
} else {
execute(cLine)
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('grpc-client.sendRequest', function () {
GetSelectedText();
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() { }
module.exports = {
activate,
deactivate
}