|
1 | 1 | import * as vscode from 'vscode'; |
2 | | -const fs = require('fs'); |
3 | 2 | const path = require('path'); |
| 3 | +import { readFile, writeFile, promises as fsPromises } from 'fs'; |
4 | 4 |
|
5 | 5 | let extensionContext: vscode.ExtensionContext; |
6 | 6 | 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) { |
31 | 31 | return; |
32 | 32 | } |
33 | 33 |
|
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); |
36 | 39 |
|
37 | 40 | vscode.window.showInputBox({ |
38 | 41 | prompt: 'Enter your font name', |
39 | 42 | placeHolder: 'Font Name - e.g. SF Pro Display' |
40 | | - }).then((userFont) => { |
| 43 | + }).then(async (userFont) => { |
41 | 44 |
|
42 | 45 | 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'); |
45 | 48 | oldCSS = context.globalState.get('oldCSS'); |
46 | 49 | } |
47 | 50 |
|
48 | 51 | 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"'); |
51 | 54 | oldJS = context.globalState.get('oldJS'); |
52 | 55 | } |
53 | 56 |
|
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"`; |
56 | 77 |
|
57 | | - updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS'); |
| 78 | + updateFile(workbenchCSS, oldCSS, newCSS, context, 'oldCSS'); |
58 | 79 |
|
59 | | - updateFile(workbenchJS, oldJS, newJS, context, 'oldJS'); |
| 80 | + updateFile(workbenchJS, oldJS, newJS, context, 'oldJS'); |
60 | 81 |
|
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 | + } |
66 | 88 | }); |
67 | 89 | }); |
68 | 90 |
|
69 | 91 | context.subscriptions.push(disposable); |
70 | 92 | } |
71 | 93 |
|
| 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 | + |
72 | 109 | 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) => { |
74 | 111 | if (err) { |
75 | 112 | console.error(err); |
76 | 113 | return; |
77 | 114 | } |
78 | 115 |
|
79 | | - let updatedData = data.replace( |
| 116 | + let updatedData = data.replaceAll( |
80 | 117 | oldText, |
81 | 118 | newText |
82 | 119 | ); |
83 | 120 |
|
84 | | - fs.writeFile(filePath, updatedData, 'utf8', (err: any) => { |
| 121 | + writeFile(filePath, updatedData, 'utf8', (err: any) => { |
85 | 122 | if (err) { |
86 | 123 | console.error(err); |
87 | 124 | } else { |
|
0 commit comments