-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
218 lines (170 loc) · 7.18 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const vscode = require('vscode');
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
/* $PATH needed for shelljs to run with vscode */
const shell = require("shelljs");
const { updateStatusBarItem } = require('./commands/display-updateset');
const { versionCommand } = require('./commands/version');
const { searchCMD } = require('./commands/search');
const { downloadCMD } = require('./commands/download');
const { openOnInstanceCMD } = require('./commands/open-on-instance');
const { runCommand } = require("./helpers/commands");
const { tryParseJSON } = require('./helpers/json');
const { setStatusbarMessage } = require('./helpers/statusbar');
const { getFieldName, getTable, getScope } = require("./helpers/core");
const { initExtension } = require('./core/config');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const { subscriptions } = context;
/* Download */
subscriptions.push(vscode.commands.registerCommand('sn-edit.download', downloadCMD));
/* Open on Instance */
subscriptions.push(vscode.commands.registerCommand('sn-edit.openOnInstance', openOnInstanceCMD));
// upload onsave
vscode.workspace.onDidSaveTextDocument(async (editor) => {
const engine = initExtension();
if (engine.error) {
return;
}
// get the sys_id of the current file
// get the table of the currently opened file
// call upload command
if (editor.fileName.indexOf(engine.workspacePath) === -1 || editor.fileName.indexOf(".sn-edit.yaml") >= 0) {
// run this only when we are in our workspacepath
// todo: make this more reliable and read it from yaml config instead
return
}
setStatusbarMessage(`$(loading~spin) Uploading file to instance`, 3000);
console.log("EDITOR => ");
console.log(editor);
console.log("FILENAME::: = ", editor.fileName);
// get scopename
let scopeName = getScope(editor);
console.log("GETSCOPENAME", scopeName);
let table = getTable(editor);
console.log("GETTABLENAME", table);
let currentFileName = editor.fileName;
// split the file path based on the current OS's separator, windows and unix like differ
currentFileName = currentFileName.split(path.sep);
let name = "";
if (currentFileName[currentFileName.length - 4].length > 0) {
name = currentFileName[currentFileName.length - 2];
}
let folderPath = currentFileName;
folderPath.splice(folderPath.length -1, 1);
folderPath = folderPath.join(path.sep);
let sysIDPath = folderPath + path.sep + "sys_id.txt";
// TODO, introduce some validation if path exists and there was any error
let sysID = fs.readFileSync(sysIDPath).toString();
// scope is not needed for the upload, maybe sooner for the update set from the --list output
console.log("Scope name", scopeName);
console.log("Table", table);
console.log("Sys ID", sysID);
let field = getFieldName(editor);;
console.log("BASENAME::", field);
const command = ["upload", "--table", table, "--sys_id", sysID, "--fields", field, "--json"];
let commandResult = await runCommand(command);
commandResult.data.forEach((res) => {
let resJSON = tryParseJSON(res);
if (resJSON.level === "error" && resJSON.error) {
return vscode.window.showErrorMessage(
"There was an error while downloading your entry! (" + resJSON.error + ")",
);
}
if (resJSON && resJSON.table === table && resJSON.sys_id === sysID) {
return vscode.window.showInformationMessage(
"The changes for file " + name + " [" + field + "] in table " + table + " were successfully uploaded!",
);
}
});
});
context.subscriptions.push(vscode.commands.registerCommand('sn-edit.configure', async () => {
vscode.env.openExternal(vscode.Uri.parse('https://conf.sn-edit.com'));
}));
// search command
subscriptions.push(vscode.commands.registerCommand('sn-edit.search', searchCMD));
// status bar notification of the current update set
// displays the currently selected default update set for the actual scope
const updateSetDisplayCMD = 'snedit.changeUpdateSet';
let updatesetStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 10);
updatesetStatusBarItem.command = updateSetDisplayCMD;
subscriptions.push(updatesetStatusBarItem);
// register some listener that make sure the status bar
// item always up-to-date
subscriptions.push(vscode.window.onDidChangeActiveTextEditor((editor) => updateStatusBarItem(editor, updatesetStatusBarItem)));
// update set picker part
subscriptions.push(vscode.commands.registerCommand("snedit.changeUpdateSet", async () => {
// truncate update set data and force reload of it
const truncateCommand = ["updateset", "--truncate", "--json"];
let truncateCommandResult = await runCommand(truncateCommand);
if (truncateCommandResult.error) {
// hide statusbar since no useful information will be provided anyway
return vscode.window.showErrorMessage(
"Could not truncate update set data!"
);
}
// set scopename to a default of global
let scopeName = getScope(vscode.window.activeTextEditor);
const command = ["updateset", "--list", "--scope", scopeName, "--json"];
let commandResult = await runCommand(command);
if (commandResult.error) {
// hide statusbar since no useful information will be provided anyway
return vscode.window.showErrorMessage(
"Could not list update sets!"
);
}
let updateSetsToChoose = [];
commandResult.data.forEach((res) => {
let resJSON = tryParseJSON(res);
if (resJSON && resJSON.current) {
// take the others elements, these are all the other update sets. except of the currently selected one
// this others contains an array
resJSON.others.forEach((el) => {
el.label = el.name;
delete(el.name);
updateSetsToChoose.push(el);
});
}
});
if (updateSetsToChoose.length === 0) {
return vscode.window.showInformationMessage(
"There are no update sets to choose from except of the currently selected one! Please go to your instance and create some and try again!"
)
}
// options
let placeholder = "Please pick the update set we should set as your default!";
const pickerOptions = {
placeHolder: placeholder ? placeholder : "",
};
let choice = await vscode.window.showQuickPick(updateSetsToChoose, pickerOptions);
if (choice) {
const command = ["updateset", "--set", "--scope", scopeName, "--update_set", choice.sys_id, "--json"];
// ignore output for now
let commandResult = await runCommand(command);
if (commandResult.error) {
return vscode.window.showErrorMessage(
"There was an error while setting the update set!"
);
}
vscode.window.showInformationMessage(
"We successfully updated your default update set"
);
// update the status bar to reflect new changes if any
await updateStatusBarItem(vscode.window.activeTextEditor, updatesetStatusBarItem);
}
}));
// update status bar item once at start
updateStatusBarItem(vscode.window.activeTextEditor, updatesetStatusBarItem);
// version command
subscriptions.push(vscode.commands.registerCommand("sn-edit.version", async () => versionCommand()));
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}