|
1 | | -import * as fs from 'fs' |
2 | | -import * as os from 'os' |
3 | | -import * as path from 'path' |
4 | 1 | import * as vscode from 'vscode' |
5 | 2 | import { UpdateBrowserProfilesMessage } from '@/views/settings/types/messages' |
6 | | -import { SettingsViewProvider } from '@/views/settings/backend/settings-view-provider' |
7 | | -import { Logger } from '@shared/utils/logger' |
8 | | -import { t } from '@/i18n' |
9 | | -import { handle_get_browser_profiles } from './handle-get-browser-profiles' |
10 | 3 |
|
11 | 4 | export const handle_update_browser_profiles = async ( |
12 | | - settings_provider: SettingsViewProvider, |
13 | 5 | message: UpdateBrowserProfilesMessage |
14 | 6 | ): Promise<void> => { |
15 | 7 | const config = vscode.workspace.getConfiguration('codeWebChat') |
16 | | - const existing_profiles = config.get<string[]>('browserProfiles', []) || [] |
17 | | - |
18 | | - const updated_profiles_set = new Set( |
19 | | - message.browser_profiles.map((p) => p.trim()) |
20 | | - ) |
21 | | - const removed_profiles = Array.from( |
22 | | - new Set( |
23 | | - existing_profiles |
24 | | - .map((p) => p.trim()) |
25 | | - .filter( |
26 | | - (p) => |
27 | | - p.length > 0 && |
28 | | - p.toLowerCase() !== 'default' && |
29 | | - !updated_profiles_set.has(p) |
30 | | - ) |
31 | | - ) |
32 | | - ) |
33 | | - |
34 | | - if (removed_profiles.length > 0) { |
35 | | - const delete_button = t('common.delete') |
36 | | - const result = await vscode.window.showWarningMessage( |
37 | | - t('settings.browser-profiles.confirm-delete.message', { |
38 | | - name: removed_profiles.join(', ') |
39 | | - }), |
40 | | - { |
41 | | - modal: true, |
42 | | - detail: t('settings.browser-profiles.confirm-delete.detail') |
43 | | - }, |
44 | | - delete_button |
45 | | - ) |
46 | | - |
47 | | - if (result !== delete_button) { |
48 | | - await handle_get_browser_profiles(settings_provider) |
49 | | - return |
50 | | - } |
51 | | - } |
52 | | - |
53 | 8 | await config.update( |
54 | 9 | 'browserProfiles', |
55 | 10 | message.browser_profiles, |
56 | 11 | vscode.ConfigurationTarget.Global |
57 | 12 | ) |
58 | | - |
59 | | - if (removed_profiles.length === 0) { |
60 | | - return |
61 | | - } |
62 | | - |
63 | | - const profiles_dir = path.join(os.homedir(), '.cwc', 'browser', 'profiles') |
64 | | - |
65 | | - for (const profile of removed_profiles) { |
66 | | - const profile_dir = path.resolve(profiles_dir, profile) |
67 | | - const relative = path.relative(profiles_dir, profile_dir) |
68 | | - if ( |
69 | | - !relative || |
70 | | - relative === '..' || |
71 | | - relative.startsWith(`..${path.sep}`) || |
72 | | - path.isAbsolute(relative) |
73 | | - ) { |
74 | | - continue |
75 | | - } |
76 | | - |
77 | | - if (fs.existsSync(profile_dir)) { |
78 | | - try { |
79 | | - await fs.promises.rm(profile_dir, { |
80 | | - recursive: true, |
81 | | - force: true |
82 | | - }) |
83 | | - } catch (error) { |
84 | | - Logger.error({ |
85 | | - function_name: 'handle_update_browser_profiles', |
86 | | - message: `Failed to remove browser profile folder "${profile}"`, |
87 | | - data: error |
88 | | - }) |
89 | | - } |
90 | | - } |
91 | | - } |
92 | 13 | } |
0 commit comments