-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathextension.js
282 lines (251 loc) · 8.3 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const vscode = require("vscode");
const fs = require("fs");
const os = require("os");
const path = require("path");
const msg = require("./messages").messages;
const uuid = require("uuid");
const fetch = require("node-fetch");
const Url = require("url");
function activate(context) {
const appDir = require.main
? path.dirname(require.main.filename)
: globalThis._VSCODE_FILE_ROOT;
if (!appDir) {
vscode.window.showInformationMessage(msg.unableToLocateVsCodeInstallationPath);
}
const base = path.join(appDir, "vs", "code");
let htmlFile = path.join(base, "electron-sandbox", "workbench", "workbench.html");
// support Cursor IDE
if (!fs.existsSync(htmlFile)) {
htmlFile = path.join(base, "electron-sandbox", "workbench", "workbench-apc-extension.html");
}
if (!fs.existsSync(htmlFile)) {
htmlFile = path.join(base, "electron-sandbox", "workbench", "workbench.esm.html");
}
if (!fs.existsSync(htmlFile)) {
vscode.window.showInformationMessage(msg.unableToLocateVsCodeInstallationPath);
}
const BackupFilePath = uuid =>
path.join(base, "electron-sandbox", "workbench", `workbench.${uuid}.bak-custom-css`);
function resolveVariable(key) {
const variables = {
cwd: () => process.cwd(),
userHome: () => os.homedir(),
execPath: () => process.env.VSCODE_EXEC_PATH ?? process.execPath,
pathSeparator: () => path.sep,
"/": () => path.sep
};
if (key in variables) return variables[key]();
if (key.startsWith("env:")) {
const [_, envKey, optionalDefault] = key.split(":");
return process.env[envKey] ?? optionalDefault ?? "";
}
}
function parsedUrl(url) {
if (/^file:/.test(url)) {
// regex matches any "${<RESOLVE>}" and replaces with resolveVariable(<RESOLVE>)
// eg: "HELLO ${userHome} WORLD" -> "HELLO /home/username WORLD"
return url.replaceAll(
/\$\{([^\{\}]+)\}/g,
(substr, key) => resolveVariable(key) ?? substr
);
} else {
return url;
}
}
async function getContent(url) {
if (/^file:/.test(url.toString())) {
const fp = Url.fileURLToPath(url);
return await fs.promises.readFile(fp);
} else {
const response = await fetch(url);
return response.buffer();
}
}
// #### main commands ######################################################
async function cmdInstall() {
const uuidSession = uuid.v4();
await createBackup(uuidSession);
await performPatch(uuidSession);
}
async function cmdReinstall() {
await uninstallImpl();
await cmdInstall();
}
async function cmdUninstall() {
await uninstallImpl();
disabledRestart();
}
async function uninstallImpl() {
const backupUuid = await getBackupUuid(htmlFile);
if (!backupUuid) return;
const backupPath = BackupFilePath(backupUuid);
await restoreBackup(backupPath);
await deleteBackupFiles();
}
// #### Backup ################################################################
async function getBackupUuid(htmlFilePath) {
try {
const htmlContent = await fs.promises.readFile(htmlFilePath, "utf-8");
const m = htmlContent.match(
/<!-- !! VSCODE-CUSTOM-CSS-SESSION-ID ([0-9a-fA-F-]+) !! -->/
);
if (!m) return null;
else return m[1];
} catch (e) {
vscode.window.showInformationMessage(msg.somethingWrong + e);
throw e;
}
}
async function createBackup(uuidSession) {
try {
let html = await fs.promises.readFile(htmlFile, "utf-8");
html = clearExistingPatches(html);
await fs.promises.writeFile(BackupFilePath(uuidSession), html, "utf-8");
} catch (e) {
vscode.window.showInformationMessage(msg.admin);
throw e;
}
}
async function restoreBackup(backupFilePath) {
try {
if (fs.existsSync(backupFilePath)) {
await fs.promises.unlink(htmlFile);
await fs.promises.copyFile(backupFilePath, htmlFile);
}
} catch (e) {
vscode.window.showInformationMessage(msg.admin);
throw e;
}
}
async function deleteBackupFiles() {
const htmlDir = path.dirname(htmlFile);
const htmlDirItems = await fs.promises.readdir(htmlDir);
for (const item of htmlDirItems) {
if (item.endsWith(".bak-custom-css")) {
await fs.promises.unlink(path.join(htmlDir, item));
}
}
}
// #### Patching ##############################################################
async function performPatch(uuidSession) {
const config = vscode.workspace.getConfiguration("vscode_custom_css");
if (!patchIsProperlyConfigured(config)) {
return vscode.window.showInformationMessage(msg.notConfigured);
}
let html = await fs.promises.readFile(htmlFile, "utf-8");
html = clearExistingPatches(html);
const injectHTML = await patchHtml(config);
html = html.replace(/<meta\s+http-equiv="Content-Security-Policy"[\s\S]*?\/>/, "");
let indicatorJS = "";
if (config.statusbar) indicatorJS = await getIndicatorJs();
html = html.replace(
/(<\/html>)/,
`<!-- !! VSCODE-CUSTOM-CSS-SESSION-ID ${uuidSession} !! -->\n` +
"<!-- !! VSCODE-CUSTOM-CSS-START !! -->\n" +
indicatorJS +
injectHTML +
"<!-- !! VSCODE-CUSTOM-CSS-END !! -->\n</html>"
);
try {
await fs.promises.writeFile(htmlFile, html, "utf-8");
} catch (e) {
vscode.window.showInformationMessage(msg.admin);
disabledRestart();
return;
}
enabledRestart();
}
function clearExistingPatches(html) {
html = html.replace(
/<!-- !! VSCODE-CUSTOM-CSS-START !! -->[\s\S]*?<!-- !! VSCODE-CUSTOM-CSS-END !! -->\n*/,
""
);
html = html.replace(/<!-- !! VSCODE-CUSTOM-CSS-SESSION-ID [\w-]+ !! -->\n*/g, "");
return html;
}
function patchIsProperlyConfigured(config) {
return config && config.imports && config.imports instanceof Array;
}
async function patchHtml(config) {
let res = "";
for (const item of config.imports) {
const imp = await patchHtmlForItem(item);
if (imp) res += imp;
}
return res;
}
async function patchHtmlForItem(url) {
if (!url) return "";
if (typeof url !== "string") return "";
// Copy the resource to a staging directory inside the extension dir
let parsed = new Url.URL(url);
const ext = path.extname(parsed.pathname);
try {
parsed = parsedUrl(url);
const fetched = await getContent(parsed);
if (ext === ".css") {
return `<style>${fetched}</style>`;
} else if (ext === ".js") {
return `<script>${fetched}</script>`;
}
throw new Error(`Unsupported extension type: ${ext}`);
} catch (e) {
console.error(e);
vscode.window.showWarningMessage(msg.cannotLoad(parsed.toString()));
return "";
}
}
async function getIndicatorJs() {
let indicatorJsPath;
let ext = vscode.extensions.getExtension("be5invis.vscode-custom-css");
if (ext && ext.extensionPath) {
indicatorJsPath = path.resolve(ext.extensionPath, "src/statusbar.js");
} else {
indicatorJsPath = path.resolve(__dirname, "statusbar.js");
}
const indicatorJsContent = await fs.promises.readFile(indicatorJsPath, "utf-8");
return `<script>${indicatorJsContent}</script>`;
}
function reloadWindow() {
// reload vscode-window
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
function enabledRestart() {
vscode.window.showInformationMessage(msg.enabled, msg.restartIde).then(btn => {
// if close button is clicked btn is undefined, so no reload window
if (btn === msg.restartIde) {
reloadWindow();
}
});
}
function disabledRestart() {
vscode.window.showInformationMessage(msg.disabled, msg.restartIde).then(btn => {
if (btn === msg.restartIde) {
reloadWindow();
}
});
}
const installCustomCSS = vscode.commands.registerCommand(
"extension.installCustomCSS",
cmdInstall
);
const uninstallCustomCSS = vscode.commands.registerCommand(
"extension.uninstallCustomCSS",
cmdUninstall
);
const updateCustomCSS = vscode.commands.registerCommand(
"extension.updateCustomCSS",
cmdReinstall
);
context.subscriptions.push(installCustomCSS);
context.subscriptions.push(uninstallCustomCSS);
context.subscriptions.push(updateCustomCSS);
console.log("vscode-custom-css is active!");
console.log("Application directory", appDir);
console.log("Main HTML file", htmlFile);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;