-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.js
398 lines (373 loc) · 13.9 KB
/
apps.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const { ipcMain } = require("electron");
const { spawn } = require("node:child_process");
/**
* Logs a message to the console.
* @param {string} message - The message to log.
* @param {string} [level="info"] - The log level ('info', 'warn', 'error').
*/
function log(message, level = "info") {
const timestamp = new Date().toISOString();
const consoleMethod = { info: console.info, warn: console.warn, error: console.error }[level] || console.log;
consoleMethod(`[${timestamp}] ${level.toUpperCase()}: ${message}`);
}
const removeAppsOptions = [
{
id: "apps_calc",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsCalculator -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_onedrive",
command: "Get-AppxPackage -AllUsers -Name *OneDrive* -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_xbox",
command: "Get-AppxPackage -AllUsers -Name *Xbox* -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_newoutlook",
command: "Get-AppxPackage -AllUsers -Name Microsoft.OutlookForWindows -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_recall",
command: "Get-AppxPackage -AllUsers -Name *Recall* -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_groovemusic",
command: "Get-AppxPackage -AllUsers -Name Microsoft.ZuneMusic -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_teams",
command: "Get-AppxPackage -AllUsers -Name MSTeams -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_mstore",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsStore -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_photos",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Windows.Photos -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_snipsketch",
command: "Get-AppxPackage -AllUsers -Name Microsoft.ScreenSketch -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_paint",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MSPaint -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_movietv",
command: "Get-AppxPackage -AllUsers -Name Microsoft.ZuneVideo -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_edge",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MicrosoftEdge -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_xboxcore",
command: "Get-AppxPackage -AllUsers -Name Microsoft.XboxApp -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
];
const uselessBloatwareOptions = [
{
id: "apps_ub_mail",
command: "Get-AppxPackage -AllUsers -Name *communi* -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_maps",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsMaps -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_skype",
command: "Get-AppxPackage -AllUsers -Name Microsoft.SkypeApp -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_getstarted",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Getstarted -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_meetnow",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsMeetNow -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_phonelink",
command: "Get-AppxPackage -AllUsers -Name Microsoft.YourPhone -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_gethelp",
command: "Get-AppxPackage -AllUsers -Name Microsoft.GetHelp -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_family",
command: "Get-AppxPackage -AllUsers -Name MicrosoftCorporationII.MicrosoftFamily -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_paint3d",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Paint3D -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_feedback",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsFeedbackHub -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_copilotstore",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Copilot_8wekyb3d8bbwe -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_clipchamp",
command: "Get-AppxPackage -AllUsers -Name Clipchamp.Clipchamp -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_onenote",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Office.OneNote -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_quickassist",
command: "Get-AppxPackage -AllUsers -Name MicrosoftCorporationII.QuickAssist -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_solitaire",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MicrosoftSolitaireCollection -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_powerautomate",
command: "Get-AppxPackage -AllUsers -Name Microsoft.PowerAutomateDesktop -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_alarms",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsAlarms -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_copilotprov",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Windows.Ai.Copilot.Provider -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_camera",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsCamera -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_weather",
command: "Get-AppxPackage -AllUsers -Name Microsoft.BingWeather -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_3dviewer",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Microsoft3DViewer -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_stickynotes",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MicrosoftStickyNotes -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_bing",
command: "Get-AppxPackage -AllUsers -Name Microsoft.BingSearch -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_todo",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Todos -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_mixedreality",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MixedReality.Portal -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_officehub",
command: "Get-AppxPackage -AllUsers -Name Microsoft.MicrosoftOfficeHub -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_people",
command: "Get-AppxPackage -AllUsers -Name Microsoft.People -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_copilot",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Copilot -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_devhome",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Windows.DevHome -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_soundrecorder",
command: "Get-AppxPackage -AllUsers -Name Microsoft.WindowsSoundRecorder -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_cortana",
command: "Get-AppxPackage -AllUsers -Name Microsoft.549981C3F5F10 -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_news",
command: "Get-AppxPackage -AllUsers -Name Microsoft.BingNews -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_xboxgamebar",
command: "Get-AppxPackage -AllUsers -Name Microsoft.XboxGamingOverlay -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_3dbuilder",
command: "Get-AppxPackage -AllUsers -Name Microsoft.3DBuilder -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
{
id: "apps_ub_whiteboard",
command: "Get-AppxPackage -AllUsers -Name Microsoft.Whiteboard -ErrorAction SilentlyContinue | Remove-AppxPackage",
},
];
/**
* Replaces logical AND operators (&&) with semicolons (;) for PowerShell compatibility.
* @param {string} cmd - The command string.
* @returns {string} The command string with replacements.
*/
function wrapCommand(cmd) {
return cmd.replace(/&&/g, ";");
}
/**
* Executes a given command in PowerShell.
* @param {string} command - The command to execute.
* @returns {Promise<{success: boolean, command: string, message: string}>} A promise resolving with the execution result.
*/
function executeCommand(command) {
return new Promise((resolve) => {
log(`Executing command: ${command}`);
let outputData = "";
let errorData = "";
const psProcess = spawn("powershell.exe", ["-NoProfile", "-Command", command]);
psProcess.stdout.on("data", (data) => {
const output = data.toString().trim();
outputData += `${output}\n`;
log(`Output: ${output}`);
});
psProcess.stderr.on("data", (data) => {
const errorOutput = data.toString().trim();
errorData += `${errorOutput}\n`;
log(`Error Output: ${errorOutput}`, "warn");
});
psProcess.on("error", (error) => {
log(`Process spawn error: ${error.message}`, "error");
resolve({
success: false,
command,
message: `Process spawn error: ${error.message}`,
});
});
psProcess.on("close", (code) => {
log(`Process closed with code: ${code}`);
const success = code === 0;
let message = outputData.trim();
if (errorData.trim()) {
message += `\nERRORS:\n${errorData.trim()}`;
}
if (!message && !success) {
message = `Process exited with code ${code}.`;
}
resolve({ success, command, message });
});
});
}
/**
* Executes a list of commands sequentially.
* @param {string[]} commands - An array of command strings.
* @param {Electron.IpcMainEvent} event - The IPC event object.
* @param {string} responseChannel - The channel to send the response back on.
*/
async function executeCommands(commands, event, responseChannel) {
const results = [];
for (const command of commands) {
try {
const result = await executeCommand(command);
results.push(result);
} catch (err) {
log(`Error executing command "${command}": ${err.message}`, "error");
results.push({ success: false, command, message: err.toString() });
}
}
if (!event.sender.isDestroyed()) {
event.reply(responseChannel, results);
} else {
log(`Window destroyed before sending response on ${responseChannel}`, "warn");
}
}
/**
* Gets the commands to execute based on selected IDs and options array.
* @param {string[]} selectedIds - Array of selected option IDs.
* @param {Array<object>} optionsArray - Array of option objects.
* @returns {string[]} Array of command strings to execute.
*/
function getCommandsToExecute(selectedIds, optionsArray) {
const commands = [];
for (const opt of optionsArray) {
const isSelected = selectedIds.includes(opt.id);
let cmd = null;
if (opt.command && isSelected) {
cmd = opt.command;
}
if (cmd) {
const wrappedCmd = wrapCommand(cmd);
if (wrappedCmd) {
commands.push(wrappedCmd);
}
}
}
return commands;
}
ipcMain.on("apply-remove-apps", (event, selectedIds) => {
log(`Received apply-remove-apps with data: ${JSON.stringify(selectedIds)}`);
const commands = getCommandsToExecute(selectedIds, removeAppsOptions);
executeCommands(commands, event, "remove-apps-response");
});
ipcMain.on("apply-useless-bloatware", (event, selectedIds) => {
log(`Received apply-useless-bloatware with data: ${JSON.stringify(selectedIds)}`);
const commands = getCommandsToExecute(selectedIds, uselessBloatwareOptions);
executeCommands(commands, event, "useless-bloatware-response");
});
/**
* Checks if an AppX package matching a pattern is installed.
* @param {string} appId - The internal ID used to find the command.
* @param {Array<object>} optionsArray - The array of app options to search within.
* @returns {Promise<{installed: boolean}>} Promise resolving with installation status.
*/
async function checkAppInstallationStatus(appId, optionsArray) {
let option = null;
for (const opt of optionsArray) {
if (opt.id === appId) {
option = opt;
break;
}
}
if (!option || !option.command) {
log(`Option or command not found for app ID: ${appId}`, "warn");
return { installed: false };
}
const match = option.command.match(/-Name\s+(['"])?([^\s'"]+)\1/);
if (!match || !match[2]) {
log(`Could not extract package name from command for app ID: ${appId}`, "warn");
return { installed: false };
}
const pattern = match[2];
const checkCmd = `Get-AppxPackage -Name ${pattern} -ErrorAction SilentlyContinue`;
return new Promise((resolve) => {
let outputData = "";
let errorData = "";
const psProcess = spawn("powershell.exe", ["-NoProfile", "-Command", checkCmd]);
psProcess.stdout.on("data", (data) => {
outputData += data.toString();
});
psProcess.stderr.on("data", (data) => {
errorData += data.toString();
});
psProcess.on("close", (code) => {
const installed = code === 0 && outputData.trim().length > 0;
if (code !== 0) {
log(`App check command for "${pattern}" failed with code ${code}. Stderr: ${errorData.trim()}`, "warn");
}
resolve({ installed });
});
psProcess.on("error", (err) => {
log(`Error spawning process for app check "${pattern}": ${err.message}`, "error");
resolve({ installed: false });
});
});
}
ipcMain.handle("check-remove-app-status", async (event, appId) => {
return checkAppInstallationStatus(appId, removeAppsOptions);
});
ipcMain.handle("check-app-status", async (event, appId) => {
return checkAppInstallationStatus(appId, uselessBloatwareOptions);
});