-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
152 lines (130 loc) · 4.96 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
const vscode = require('vscode');
const LanguageFormatter = require("./src/languageFormatters");
const Config = require('./src/config');
const telemetry = require('./src/telemetry');
const dialogs = require('./src/ui/dialogs');
const { showConfigPanel } = require('./src/ui/configPanel');
let ext = require('./src/extensionHandler');
/**
* This method is called when your extension is activated.
* Your extension is activated the very first time the command is executed.
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
console.log(`ssjs-vsc @ ${Config.getExtensionVersion()} is starting!`);
ext.attachContext(context);
telemetry.init(context);
registerFormatters();
registerCommands(context, [
{ name: 'ssjs-vsc.upload-to-prod', callback: async () => await ext.provider.uploadToProduction() },
{ name: 'ssjs-vsc.upload-script', callback: async () => await ext.provider.uploadScript() },
{ name: 'ssjs-vsc.start', callback: async () => await ext.provider.startServer() },
{ name: 'ssjs-vsc.stop', callback: async () => await ext.provider.stopServer() },
{ name: 'ssjs-vsc.create-config', callback: () => createConfig() },
{ name: 'ssjs-vsc.update-config', callback: () => createConfig(true) },
{ name: 'ssjs-vsc.deploy-any-path', callback: async () => await ext.provider.deployAnyScript() },
{ name: 'ssjs-vsc.update-any-path', callback: async () => await ext.provider.updateAnyScript() },
{ name: 'ssjs-vsc.get-url', callback: async () => await ext.provider.getDevUrl(true) },
{ name: 'ssjs-vsc.run', callback: async () => await ext.provider.getDevUrl() },
{ name: 'ssjs-vsc.show-walkthrough', callback: showWalkthrough },
{ name: 'ssjs-vsc.show-config', callback: async () => await showConfigPanel(context) }
]);
let workspaceOk = await ext.workspaceOk();
console.log(`Workspace is ok: ${workspaceOk}.`);
if (!workspaceOk) {
await launchConfigPanel(context);
return;
}
// IF WORKSPACE EXISTS:
let configOk = await ext.loadConfiguration(context);
if (!configOk) {
await launchConfigPanel(context);
}
await ext.pickCodeProvider(true, true);
if (configOk) {
await ext.checkDevPageVersion();
telemetry.log(`extensionActivated`, { codeProvider: Config.getCodeProvider(), allSet: true });
}
watchForConfigurationChanges();
registerFileActions(context);
}
async function launchConfigPanel(context) {
if (Config.showPanelAutomatically()) {
await showConfigPanel(context);
}
}
function watchForConfigurationChanges() {
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('ssjs-vsc.editor.codeProvider')) {
ext.pickCodeProvider();
telemetry.log(`changeCodeProvider`, { codeProvider: Config.getCodeProvider() });
}
});
}
function registerCommands(context, commands) {
commands.forEach(({ name, callback }) => {
const command = vscode.commands.registerCommand(name, callback);
context.subscriptions.push(command);
});
}
function registerFileActions(context) {
const onSaveFile = vscode.workspace.onDidSaveTextDocument(async (textDocument) => {
let filePath = textDocument.uri.fsPath;
if (!Config.isWorkspaceSet()) {
vscode.window.showWarningMessage(`It seems you are not using workspaces! To use full potential of this extension, please, open a folder and run command: "SSJS: Show Setup Walkthrough".`);
ext.deactivateProviders({});
telemetry.log(`noWorkspace`);
return;
}
if (Config.isConfigFile(filePath)) {
ext.config.loadConfig();
} else if (Config.isAutoSaveEnabled() && Config.isFileInWorkspace(filePath)) {
await ext.provider.uploadScript(true);
} else {
if (!filePath.endsWith('settings.json')) {
console.log(`registerFileActions() called for: ${filePath}, autosave: ${Config.isAutoSaveEnabled()} && within Workspace: ${Config.isFileInWorkspace(filePath)}.`);
}
}
});
context.subscriptions.push(onSaveFile);
}
function registerFormatters() {
const formatters = new LanguageFormatter();
const formatterRegistrations = vscode.languages.registerDocumentFormattingEditProvider(
formatters.getSelectors(),
formatters
);
vscode.Disposable.from(formatterRegistrations);
}
function showWalkthrough() {
telemetry.log('showWalkthrough');
vscode.commands.executeCommand('workbench.action.openWalkthrough', { category: 'FiB.ssjs-vsc#setup-ssjs-manager' }, false);
}
const createConfig = async function(update = false) {
try {
const creds = await dialogs.api.getCredentials(update);
if (!creds) {
console.log(`createConfig(): No creds provided.`);
return;
}
let r = await ext.handleNewSfmcCreds(creds, update);
if (!r.ok) {
vscode.window.showErrorMessage(r.message);
return;
} else {
vscode.window.showInformationMessage(r.message);
}
} catch (e) {
telemetry.error('createConfig', { error: e.message });
}
}
// This method is called when your extension is deactivated
function deactivate() {
console.log(`Deactivating extension!`);
ext.provider.deactivate();
telemetry.dispose();
}
module.exports = {
activate,
deactivate
}