-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathextension.js
144 lines (114 loc) Β· 5.44 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
const path = require('path');
const fs = require('fs');
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
this.extensionName = 'RobbOwen.synthwave-vscode';
this.cntx = context;
const config = vscode.workspace.getConfiguration("synthwave84");
let disableGlow = config && config.disableGlow ? !!config.disableGlow : false;
let brightness = parseFloat(config.brightness) > 1 ? 1 : parseFloat(config.brightness);
brightness = brightness < 0 ? 0 : brightness;
brightness = isNaN(brightness) ? 0.45 : brightness;
const parsedBrightness = Math.floor(brightness * 255).toString(16).toUpperCase();
let neonBrightness = parsedBrightness;
let disposable = vscode.commands.registerCommand('synthwave84.enableNeon', function () {
const appDir = path.dirname(vscode.env.appRoot);
const base = path.join(appDir,'app','out','vs','code');
const electronBase = isVSCodeBelowVersion("1.70.0") ? "electron-browser" : "electron-sandbox";
const workBenchFilename = vscode.version == "1.94.0" ? "workbench.esm.html" : "workbench.html";
const htmlFile = path.join(base, electronBase, "workbench", workBenchFilename);
const templateFile = path.join(base, electronBase, "workbench", "neondreams.js");
try {
// const version = context.globalState.get(`${context.extensionName}.version`);
// generate production theme JS
const chromeStyles = fs.readFileSync(__dirname +'/css/editor_chrome.css', 'utf-8');
const jsTemplate = fs.readFileSync(__dirname +'/js/theme_template.js', 'utf-8');
const themeWithGlow = jsTemplate.replace(/\[DISABLE_GLOW\]/g, disableGlow);
const themeWithChrome = themeWithGlow.replace(/\[CHROME_STYLES\]/g, chromeStyles);
const finalTheme = themeWithChrome.replace(/\[NEON_BRIGHTNESS\]/g, neonBrightness);
fs.writeFileSync(templateFile, finalTheme, "utf-8");
// modify workbench html
const html = fs.readFileSync(htmlFile, "utf-8");
// check if the tag is already there
const isEnabled = html.includes("neondreams.js");
if (!isEnabled) {
// delete synthwave script tag if there
let output = html.replace(/^.*(<!-- SYNTHWAVE 84 --><script src="neondreams.js"><\/script><!-- NEON DREAMS -->).*\n?/mg, '');
// add script tag
output = html.replace(/\<\/html\>/g, ` <!-- SYNTHWAVE 84 --><script src="neondreams.js"></script><!-- NEON DREAMS -->\n`);
output += '</html>';
fs.writeFileSync(htmlFile, output, "utf-8");
vscode.window
.showInformationMessage("Neon Dreams enabled. VS code must reload for this change to take effect. Code may display a warning that it is corrupted, this is normal. You can dismiss this message by choosing 'Don't show this again' on the notification.", { title: "Restart editor to complete" })
.then(function(msg) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
});
} else {
vscode.window
.showInformationMessage('Neon dreams is already enabled. Reload to refresh JS settings.', { title: "Restart editor to refresh settings" })
.then(function(msg) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
});
}
} catch (e) {
if (/ENOENT|EACCES|EPERM/.test(e.code)) {
vscode.window.showInformationMessage("Neon Dreams was unable to modify the core VS code files needed to launch the extension. You may need to run VS code with admin privileges in order to enable Neon Dreams.");
return;
} else {
vscode.window.showErrorMessage('Something went wrong when starting neon dreams');
return;
}
}
});
let disable = vscode.commands.registerCommand('synthwave84.disableNeon', uninstall);
context.subscriptions.push(disposable);
context.subscriptions.push(disable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
// ...
}
function uninstall() {
const appDir = path.dirname(vscode.env.appRoot);
const base = path.join(appDir, 'app', 'out', 'vs', 'code');
const electronBase = isVSCodeBelowVersion("1.70.0") ? "electron-browser" : "electron-sandbox";
const workBenchFilename = vscode.version == "1.94.0" ? "workbench.esm.html" : "workbench.html";
const htmlFile = path.join(base, electronBase, "workbench", workBenchFilename);
// modify workbench html
const html = fs.readFileSync(htmlFile, "utf-8");
// check if the tag is already there
const isEnabled = html.includes("neondreams.js");
if (isEnabled) {
// delete synthwave script tag if there
let output = html.replace(/^.*(<!-- SYNTHWAVE 84 --><script src="neondreams.js"><\/script><!-- NEON DREAMS -->).*\n?/mg, '');
fs.writeFileSync(htmlFile, output, "utf-8");
vscode.window
.showInformationMessage("Neon Dreams disabled. VS code must reload for this change to take effect", { title: "Restart editor to complete" })
.then(function(msg) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
});
} else {
vscode.window.showInformationMessage('Neon dreams isn\'t running.');
}
}
// Returns true if the VS Code version running this extension is below the
// version specified in the "version" parameter. Otherwise returns false.
function isVSCodeBelowVersion(version) {
const vscodeVersion = vscode.version;
const vscodeVersionArray = vscodeVersion.split('.');
const versionArray = version.split('.');
for (let i = 0; i < versionArray.length; i++) {
if (vscodeVersionArray[i] < versionArray[i]) {
return true;
}
}
return false;
}
module.exports = {
activate,
deactivate
}