From 062788b7e92c5cbffc4b9fb78b1bd6e10864e5fd Mon Sep 17 00:00:00 2001 From: Sagar More Date: Thu, 14 Dec 2023 14:25:47 +0530 Subject: [PATCH] bug fix:temp --- package-lock.json | 4 +-- package.json | 2 +- src/extension.ts | 77 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 194acd0..8191752 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vs-code-custom-ui-font", - "version": "0.0.1", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vs-code-custom-ui-font", - "version": "0.0.1", + "version": "1.0.1", "devDependencies": { "@types/mocha": "^10.0.2", "@types/node": "18.x", diff --git a/package.json b/package.json index f615011..aa42d0a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vs-code-custom-ui-font", "displayName": "VS Code Custom UI Font", "description": "An extension for replacing default UI font of VS Code.", - "version": "1.0.0", + "version": "1.0.1", "publisher": "animsh", "engines": { "vscode": "^1.83.0" diff --git a/src/extension.ts b/src/extension.ts index 8ce7100..c8f6753 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; -const fs = require('fs'); const path = require('path'); +import { readFile, writeFile, promises as fsPromises } from 'fs'; let extensionContext: vscode.ExtensionContext; 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) { return; } - let oldCSS = context.globalState.get('oldCSS'); - let oldJS = context.globalState.get('oldJS'); + let oldCSS: (string | undefined) = context.globalState.get('oldCSS'); + let oldJS: (string | undefined) = context.globalState.get('oldJS'); + + console.log(oldCSS); + console.log(oldJS); vscode.window.showInputBox({ prompt: 'Enter your font name', placeHolder: 'Font Name - e.g. SF Pro Display' - }).then((userFont) => { + }).then(async (userFont) => { if (!oldCSS) { - context.globalState.update('oldCSS', '.windows{font-family:Segoe WPC,Segoe UI,sans-serif}'); - context.globalState.update('defaultCSS', '.windows{font-family:Segoe WPC,Segoe UI,sans-serif}'); + context.globalState.update('oldCSS', 'Segoe WPC,Segoe UI'); + context.globalState.update('defaultCSS', 'Segoe WPC,Segoe UI'); oldCSS = context.globalState.get('oldCSS'); } if (!oldJS) { - context.globalState.update('oldJS', ':host-context(.windows) { font-family: "Segoe WPC", "Segoe UI", sans-serif; }'); - context.globalState.update('defaultJS', ':host-context(.windows) { font-family: "Segoe WPC", "Segoe UI", sans-serif; }'); + context.globalState.update('oldJS', '"Segoe WPC", "Segoe UI"'); + context.globalState.update('defaultJS', '"Segoe WPC", "Segoe UI"'); oldJS = context.globalState.get('oldJS'); } - let newCSS = '.windows {font-family: ' + userFont + ', Segoe WPC, Segoe UI, sans-serif;text-rendering: optimizeLegibility;-webkit-font-smoothing: antialiased; }'; - let newJS = ":host-context(.windows) { font-family: '" + userFont + "', 'Segoe WPC', 'Segoe UI', sans-serif;}"; + let isJS = await checkIfContainsAsync(workbenchJS, oldJS); + let isCSS = await checkIfContainsAsync(workbenchCSS, oldCSS); + + if (!isJS) { + context.globalState.update('oldJS', '"Segoe WPC", "Segoe UI"'); + context.globalState.update('defaultJS', '"Segoe WPC", "Segoe UI"'); + oldJS = context.globalState.get('oldJS'); + isJS = true; + } + + if (!isCSS) { + context.globalState.update('oldCSS', 'Segoe WPC,Segoe UI'); + context.globalState.update('defaultCSS', 'Segoe WPC,Segoe UI'); + oldCSS = context.globalState.get('oldCSS'); + isCSS = true; + } + + if (isJS && isCSS) { + let newCSS = `${userFont},Segoe WPC,Segoe UI`; + let newJS = `${userFont}, "Segoe WPC", "Segoe UI"`; - updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS'); + updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS'); - updateFile(workbenchJS, oldJS, newJS, context, 'oldJS'); + updateFile(workbenchJS, oldJS, newJS, context, 'oldJS'); - vscode.window.showInformationMessage('Restart VS Code to see the changes', 'Reload').then(selection => { - if (selection === 'Reload') { - vscode.commands.executeCommand('workbench.action.reloadWindow'); - } - }); + vscode.window.showInformationMessage('Restart VS Code to see the changes', 'Reload').then(selection => { + if (selection === 'Reload') { + vscode.commands.executeCommand('workbench.action.reloadWindow'); + } + }); + } }); }); context.subscriptions.push(disposable); } +async function checkIfContainsAsync(filename: string, str: string | undefined) { + try { + const contents = await fsPromises.readFile(filename, 'utf-8'); + + if (str !== undefined) { + const result: boolean = contents.includes(str); + console.log(result); + return result; + } + return false; + } catch (err) { + console.log(err); + } +} + function updateFile(filePath: string, oldText: any, newText: any, context: vscode.ExtensionContext, type: string) { - fs.readFile(filePath, 'utf8', (err: any, data: any) => { + readFile(filePath, 'utf8', (err: any, data: string | any) => { if (err) { console.error(err); return; } - let updatedData = data.replace( + let updatedData = data.replaceAll( oldText, newText ); - fs.writeFile(filePath, updatedData, 'utf8', (err: any) => { + writeFile(filePath, updatedData, 'utf8', (err: any) => { if (err) { console.error(err); } else {