Skip to content

Commit 062788b

Browse files
committed
bug fix:temp
1 parent 715e008 commit 062788b

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vs-code-custom-ui-font",
33
"displayName": "VS Code Custom UI Font",
44
"description": "An extension for replacing default UI font of VS Code.",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"publisher": "animsh",
77
"engines": {
88
"vscode": "^1.83.0"

src/extension.ts

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
2-
const fs = require('fs');
32
const path = require('path');
3+
import { readFile, writeFile, promises as fsPromises } from 'fs';
44

55
let extensionContext: vscode.ExtensionContext;
66
let workbenchCSS = path.join(process.env.APPDATA, '..\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\vs\\workbench\\workbench.desktop.main.css');
@@ -31,57 +31,94 @@ export function activate(context: vscode.ExtensionContext) {
3131
return;
3232
}
3333

34-
let oldCSS = context.globalState.get('oldCSS');
35-
let oldJS = context.globalState.get('oldJS');
34+
let oldCSS: (string | undefined) = context.globalState.get('oldCSS');
35+
let oldJS: (string | undefined) = context.globalState.get('oldJS');
36+
37+
console.log(oldCSS);
38+
console.log(oldJS);
3639

3740
vscode.window.showInputBox({
3841
prompt: 'Enter your font name',
3942
placeHolder: 'Font Name - e.g. SF Pro Display'
40-
}).then((userFont) => {
43+
}).then(async (userFont) => {
4144

4245
if (!oldCSS) {
43-
context.globalState.update('oldCSS', '.windows{font-family:Segoe WPC,Segoe UI,sans-serif}');
44-
context.globalState.update('defaultCSS', '.windows{font-family:Segoe WPC,Segoe UI,sans-serif}');
46+
context.globalState.update('oldCSS', 'Segoe WPC,Segoe UI');
47+
context.globalState.update('defaultCSS', 'Segoe WPC,Segoe UI');
4548
oldCSS = context.globalState.get('oldCSS');
4649
}
4750

4851
if (!oldJS) {
49-
context.globalState.update('oldJS', ':host-context(.windows) { font-family: "Segoe WPC", "Segoe UI", sans-serif; }');
50-
context.globalState.update('defaultJS', ':host-context(.windows) { font-family: "Segoe WPC", "Segoe UI", sans-serif; }');
52+
context.globalState.update('oldJS', '"Segoe WPC", "Segoe UI"');
53+
context.globalState.update('defaultJS', '"Segoe WPC", "Segoe UI"');
5154
oldJS = context.globalState.get('oldJS');
5255
}
5356

54-
let newCSS = '.windows {font-family: ' + userFont + ', Segoe WPC, Segoe UI, sans-serif;text-rendering: optimizeLegibility;-webkit-font-smoothing: antialiased; }';
55-
let newJS = ":host-context(.windows) { font-family: '" + userFont + "', 'Segoe WPC', 'Segoe UI', sans-serif;}";
57+
let isJS = await checkIfContainsAsync(workbenchJS, oldJS);
58+
let isCSS = await checkIfContainsAsync(workbenchCSS, oldCSS);
59+
60+
if (!isJS) {
61+
context.globalState.update('oldJS', '"Segoe WPC", "Segoe UI"');
62+
context.globalState.update('defaultJS', '"Segoe WPC", "Segoe UI"');
63+
oldJS = context.globalState.get('oldJS');
64+
isJS = true;
65+
}
66+
67+
if (!isCSS) {
68+
context.globalState.update('oldCSS', 'Segoe WPC,Segoe UI');
69+
context.globalState.update('defaultCSS', 'Segoe WPC,Segoe UI');
70+
oldCSS = context.globalState.get('oldCSS');
71+
isCSS = true;
72+
}
73+
74+
if (isJS && isCSS) {
75+
let newCSS = `${userFont},Segoe WPC,Segoe UI`;
76+
let newJS = `${userFont}, "Segoe WPC", "Segoe UI"`;
5677

57-
updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS');
78+
updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS');
5879

59-
updateFile(workbenchJS, oldJS, newJS, context, 'oldJS');
80+
updateFile(workbenchJS, oldJS, newJS, context, 'oldJS');
6081

61-
vscode.window.showInformationMessage('Restart VS Code to see the changes', 'Reload').then(selection => {
62-
if (selection === 'Reload') {
63-
vscode.commands.executeCommand('workbench.action.reloadWindow');
64-
}
65-
});
82+
vscode.window.showInformationMessage('Restart VS Code to see the changes', 'Reload').then(selection => {
83+
if (selection === 'Reload') {
84+
vscode.commands.executeCommand('workbench.action.reloadWindow');
85+
}
86+
});
87+
}
6688
});
6789
});
6890

6991
context.subscriptions.push(disposable);
7092
}
7193

94+
async function checkIfContainsAsync(filename: string, str: string | undefined) {
95+
try {
96+
const contents = await fsPromises.readFile(filename, 'utf-8');
97+
98+
if (str !== undefined) {
99+
const result: boolean = contents.includes(str);
100+
console.log(result);
101+
return result;
102+
}
103+
return false;
104+
} catch (err) {
105+
console.log(err);
106+
}
107+
}
108+
72109
function updateFile(filePath: string, oldText: any, newText: any, context: vscode.ExtensionContext, type: string) {
73-
fs.readFile(filePath, 'utf8', (err: any, data: any) => {
110+
readFile(filePath, 'utf8', (err: any, data: string | any) => {
74111
if (err) {
75112
console.error(err);
76113
return;
77114
}
78115

79-
let updatedData = data.replace(
116+
let updatedData = data.replaceAll(
80117
oldText,
81118
newText
82119
);
83120

84-
fs.writeFile(filePath, updatedData, 'utf8', (err: any) => {
121+
writeFile(filePath, updatedData, 'utf8', (err: any) => {
85122
if (err) {
86123
console.error(err);
87124
} else {

0 commit comments

Comments
 (0)