Skip to content

Commit 5c0b512

Browse files
committed
Restore VSCode settings in uninstall hook
1 parent da5f868 commit 5c0b512

File tree

3 files changed

+138
-14
lines changed

3 files changed

+138
-14
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.52
2+
3+
* Updated uninstall hook to also attempt to restore VSCode config settings
4+
15
# 1.1.51
26

37
* Reduced package size to ~1 MB by not bundling dev dependencies

extension/index.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ function activate(context) {
531531

532532
// Update settings if necessary
533533
let newColorCustomization = {};
534-
if (currentBackground !== "#00000000" || currentGpuAcceleration !== "off") {
534+
if (currentBackground !== "#00000000") {
535535
newColorCustomization = {
536536
...currentColorCustomizations,
537537
"terminal.background": "#00000000"
@@ -571,6 +571,8 @@ function activate(context) {
571571

572572
// Save user customizations
573573
await context.globalState.update('customizations', previousCustomizations);
574+
575+
return previousCustomizations;
574576
}
575577

576578
// Function to restore previous settings on uninstall
@@ -635,22 +637,30 @@ function activate(context) {
635637
);
636638

637639
return configFilePath;
638-
}
640+
}
639641

640-
async function setLocalConfig(state, paths) {
642+
async function setLocalConfig(state, paths, previousCustomizations) {
641643
const configFilePath = await getLocalConfigPath();
642644

645+
// Convert undefined values in previousCustomizations to null
646+
if (previousCustomizations && typeof previousCustomizations === 'object') {
647+
previousCustomizations = Object.fromEntries(
648+
Object.entries(previousCustomizations).map(([key, value]) => [key, value === undefined ? null : value])
649+
);
650+
}
651+
643652
if (state) {
644-
const configData = {
645-
workbenchHtmlPath: paths.workbenchHtmlPath,
646-
jsPath: paths.jsPath,
647-
electronJsPath: paths.electronJsPath
648-
};
649-
await fs.writeFile(configFilePath, JSON.stringify(configData, null, 2), 'utf-8');
653+
const configData = {
654+
workbenchHtmlPath: paths.workbenchHtmlPath,
655+
jsPath: paths.jsPath,
656+
electronJsPath: paths.electronJsPath,
657+
previousCustomizations,
658+
};
659+
await fs.writeFile(configFilePath, JSON.stringify(configData, null, 2), 'utf-8');
650660
} else {
651661
await fs.unlink(configFilePath).catch(() => { });
652662
}
653-
}
663+
}
654664

655665

656666
// #### main commands ######################################################
@@ -679,14 +689,13 @@ async function setLocalConfig(state, paths) {
679689
}
680690
await installJS();
681691
await installHTML();
682-
await changeVSCodeSettings();
683692
await checkColorTheme();
684693
await checkElectronDeprecatedType();
685694
await setLocalConfig(true, {
686695
workbenchHtmlPath: HTMLFile,
687696
jsPath: JSFile,
688-
electronJsPath: ElectronJSFile
689-
});
697+
electronJsPath: ElectronJSFile,
698+
}, await changeVSCodeSettings());
690699
} catch (error) {
691700
if (error && (error.code === 'EPERM' || error.code === 'EACCES')) {
692701
vscode.window.showInformationMessage(localize('messages.admin') + error);

extension/uninstallHook.js

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,116 @@ const { spawn, exec } = require('child_process');
22
const fs = require('fs').promises; // Use fs.promises for Promise-based APIs
33
const fsSync = require('fs'); // Import standard fs for synchronous methods
44
const path = require('path');
5+
const os = require('os');
6+
7+
function getVSCodeSettingsPath() {
8+
const platform = os.platform();
9+
const home = os.homedir();
10+
11+
if (platform === 'win32') {
12+
return path.join(process.env.APPDATA, 'Code', 'User', 'settings.json');
13+
} else if (platform === 'darwin') {
14+
return path.join(home, 'Library', 'Application Support', 'Code', 'User', 'settings.json');
15+
} else {
16+
// Assume Linux
17+
return path.join(home, '.config', 'Code', 'User', 'settings.json');
18+
}
19+
}
20+
21+
// Function to restore previous settings
22+
// Because VSCode uses JSONC, we need to be careful with comments
23+
// and formatting. We will use regex to find and replace the specific settings.
24+
function restorePreviousSettings(previousCustomizations) {
25+
const settingsPath = getVSCodeSettingsPath();
26+
27+
if (!fsSync.existsSync(settingsPath)) {
28+
console.error('VSCode settings.json not found!');
29+
return;
30+
}
31+
32+
let settingsContent = '';
33+
try {
34+
settingsContent = fsSync.readFileSync(settingsPath, 'utf-8');
35+
} catch (err) {
36+
console.error('Failed to read settings.json:', err);
37+
return;
38+
}
39+
40+
// Remove transparent terminal background
41+
settingsContent = settingsContent.replace(
42+
/"terminal\.background"\s*:\s*"#00000000",?\s*/g,
43+
''
44+
);
45+
46+
// Restore saved customizations
47+
if (previousCustomizations?.saved) {
48+
if (previousCustomizations.terminalBackground !== null) {
49+
if (
50+
previousCustomizations.terminalBackground === '#00000000'
51+
) {
52+
settingsContent = settingsContent.replace(
53+
/"terminal\.background"\s*:\s*".*?",?\s*/g,
54+
''
55+
);
56+
} else {
57+
settingsContent = settingsContent.replace(
58+
/"terminal\.background"\s*:\s*".*?",?\s*/g,
59+
`"terminal.background": "${previousCustomizations.terminalBackground}",`
60+
);
61+
}
62+
}
63+
64+
if (previousCustomizations.systemColorTheme !== null) {
65+
settingsContent = settingsContent.replace(
66+
/"window\.systemColorTheme"\s*:\s*".*?",?\s*/g,
67+
previousCustomizations.systemColorTheme === null
68+
? ''
69+
: `"window.systemColorTheme": "${previousCustomizations.systemColorTheme}",`
70+
);
71+
} else {
72+
settingsContent = settingsContent.replace(
73+
/"window\.systemColorTheme"\s*:\s*".*?",?\s*/g,
74+
''
75+
);
76+
}
77+
78+
if (previousCustomizations.autoDetectColorScheme !== null) {
79+
settingsContent = settingsContent.replace(
80+
/"window\.autoDetectColorScheme"\s*:\s*(true|false),?\s*/g,
81+
previousCustomizations.autoDetectColorScheme === null
82+
? ''
83+
: `"window.autoDetectColorScheme": ${previousCustomizations.autoDetectColorScheme},`
84+
);
85+
} else {
86+
settingsContent = settingsContent.replace(
87+
/"window\.autoDetectColorScheme"\s*:\s*(true|false),?\s*/g,
88+
''
89+
);
90+
}
91+
92+
if (previousCustomizations.gpuAcceleration !== null) {
93+
settingsContent = settingsContent.replace(
94+
/"terminal\.integrated\.gpuAcceleration"\s*:\s*".*?",?\s*/g,
95+
previousCustomizations.gpuAcceleration === null
96+
? ''
97+
: `"terminal.integrated.gpuAcceleration": "${previousCustomizations.gpuAcceleration}",`
98+
);
99+
} else {
100+
settingsContent = settingsContent.replace(
101+
/"terminal\.integrated\.gpuAcceleration"\s*:\s*".*?",?\s*/g,
102+
''
103+
);
104+
}
105+
}
106+
107+
// Write updated settings back to disk
108+
try {
109+
fsSync.writeFileSync(settingsPath, settingsContent.trim() + '\n', 'utf-8');
110+
console.log('VSCode settings.json successfully reverted.');
111+
} catch (err) {
112+
console.error('Failed to write settings.json:', err);
113+
}
114+
}
5115

6116
(async () => {
7117
const envPaths = (await import('env-paths')).default;
@@ -58,10 +168,11 @@ const path = require('path');
58168

59169
const config = loadConfig();
60170
if (config) {
61-
const { workbenchHtmlPath, jsPath, electronJsPath } = config;
171+
const { workbenchHtmlPath, jsPath, electronJsPath, previousCustomizations } = config;
62172

63173
await uninstallJS(jsPath, electronJsPath);
64174
await uninstallHTML(workbenchHtmlPath);
175+
restorePreviousSettings(previousCustomizations);
65176

66177
showNotification("Vibrancy Continued has been removed. Please restart VSCode to apply changes.");
67178
}

0 commit comments

Comments
 (0)