-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
executable file
·146 lines (125 loc) · 3.8 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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path');
const watchers = [];
const OLD_PATH = '**/.sfdx/sfdx-config.json';
const NEW_PATH = '**/.sf/config.json';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
async function activate() {
const handleFileContent = (uri) => {
const file = requireUncached(path.resolve(uri.fsPath));
const conf = vscode.workspace.getConfiguration();
const targets = conf.get('sf-colorg.rules') || [];
let targetColor = null;
for (let target of targets) {
const attribute = uri.fsPath.includes('/.sfdx/')
? 'defaultusername'
: 'target-org';
if (new RegExp(target.regex).test(file[attribute])) {
targetColor = target.color;
break;
}
}
setColor(targetColor);
};
const init = async () => {
// Initial setup
try {
await initialCleanup();
let files = await vscode.workspace.findFiles(NEW_PATH, null, 1);
if (files.length > 0) {
handleFileContent(files[0]);
} else {
files = await vscode.workspace.findFiles(OLD_PATH, null, 1);
if (files.length > 0) {
handleFileContent(files[0]);
}
}
} catch (err) {
console.error(err);
}
};
init();
// Config change listener
vscode.workspace.onDidChangeConfiguration((changeConfigEvent) => {
if (changeConfigEvent.affectsConfiguration('sf-colorg')) {
init();
}
});
// File change watcher
let oldPathFile = await vscode.workspace.findFiles(OLD_PATH, null, 1);
if (oldPathFile.length > 0) {
watchers.push(
vscode.workspace.createFileSystemWatcher(
path.resolve(oldPathFile[0].fsPath)
)
);
}
let newPathFiles = await vscode.workspace.findFiles(NEW_PATH, null, 1);
if (newPathFiles.length > 0) {
watchers.push(
vscode.workspace.createFileSystemWatcher(
path.resolve(newPathFiles[0].fsPath)
)
);
}
// Event listener
watchers.forEach((watcher) => {
watcher.onDidChange((uri) => handleFileContent(uri));
watcher.onDidCreate((uri) => handleFileContent(uri));
});
}
async function setColor(color) {
const config = vscode.workspace.getConfiguration();
const colorCustomizations = config.get('workbench.colorCustomizations');
const statusBar = config.get('sf-colorg.target.statusBar');
const activityBar = config.get('sf-colorg.target.activityBar');
if (statusBar || color == null) {
colorCustomizations['statusBar.background'] = color;
} else {
colorCustomizations['statusBar.background'] = undefined;
}
if (activityBar || color == null) {
colorCustomizations['activityBar.background'] = color;
} else {
colorCustomizations['activityBar.background'] = undefined;
}
await vscode.workspace
.getConfiguration()
.update(
'workbench.colorCustomizations',
colorCustomizations,
vscode.ConfigurationTarget.Workspace
);
}
// Remove previous color customizations to avoid color blinking when switching to an unknown config
async function initialCleanup() {
const config = vscode.workspace.getConfiguration();
const colorCustomizations = config.get('workbench.colorCustomizations');
colorCustomizations['statusBar.background'] = undefined;
colorCustomizations['activityBar.background'] = undefined;
return vscode.workspace
.getConfiguration()
.update(
'workbench.colorCustomizations',
colorCustomizations,
vscode.ConfigurationTarget.Global
);
}
function requireUncached(module) {
delete require.cache[require.resolve(path.resolve(module))];
return require(path.resolve(module));
}
// This method is called when your extension is deactivated
async function deactivate() {
watchers.forEach((watcher) => {
watcher.dispose();
});
setColor(null);
}
module.exports = {
activate,
deactivate,
};