|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
| 4 | +import { Logger } from '@shared/utils/logger' |
| 5 | +import { SavedContext } from '@/types/context' |
| 6 | + |
| 7 | +const MIGRATION_ID = 'saved-contexts-to-global-storage-migration-202601051' |
| 8 | +// NOTE: This key is a guess, as the original value of SAVED_CONTEXTS_STATE_KEY was not provided. |
| 9 | +const OLD_KEY = 'savedContexts' |
| 10 | +const GLOBAL_CONTEXTS_FILENAME = 'saved-contexts.json' |
| 11 | + |
| 12 | +type GlobalContextsData = Record<string, SavedContext[]> |
| 13 | + |
| 14 | +const get_global_contexts_file_path = ( |
| 15 | + context: vscode.ExtensionContext |
| 16 | +): string => { |
| 17 | + return path.join(context.globalStorageUri.fsPath, GLOBAL_CONTEXTS_FILENAME) |
| 18 | +} |
| 19 | + |
| 20 | +const load_all_global_contexts = ( |
| 21 | + context: vscode.ExtensionContext |
| 22 | +): GlobalContextsData => { |
| 23 | + const file_path = get_global_contexts_file_path(context) |
| 24 | + try { |
| 25 | + if (fs.existsSync(file_path)) { |
| 26 | + const content = fs.readFileSync(file_path, 'utf8') |
| 27 | + return JSON.parse(content) |
| 28 | + } |
| 29 | + } catch (error) { |
| 30 | + Logger.warn({ |
| 31 | + function_name: 'load_all_global_contexts', |
| 32 | + message: 'Error loading global contexts during migration', |
| 33 | + data: error |
| 34 | + }) |
| 35 | + } |
| 36 | + return {} |
| 37 | +} |
| 38 | + |
| 39 | +const save_all_global_contexts = ( |
| 40 | + context: vscode.ExtensionContext, |
| 41 | + data: GlobalContextsData |
| 42 | +): void => { |
| 43 | + const file_path = get_global_contexts_file_path(context) |
| 44 | + const dir_path = path.dirname(file_path) |
| 45 | + |
| 46 | + if (!fs.existsSync(dir_path)) { |
| 47 | + fs.mkdirSync(dir_path, { recursive: true }) |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + fs.writeFileSync(file_path, JSON.stringify(data, null, 2), 'utf8') |
| 52 | + } catch (error) { |
| 53 | + Logger.error({ |
| 54 | + function_name: 'save_all_global_contexts', |
| 55 | + message: 'Error saving global contexts during migration', |
| 56 | + data: error |
| 57 | + }) |
| 58 | + throw new Error('Failed to save contexts to global storage.') |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Migration to move saved contexts from workspaceState to a file-based global storage. |
| 64 | + * This makes the saved contexts persistent per workspace folder across different VS Code windows. |
| 65 | + */ |
| 66 | +export async function migrate_saved_contexts_to_global_storage( |
| 67 | + context: vscode.ExtensionContext |
| 68 | +): Promise<void> { |
| 69 | + try { |
| 70 | + if (context.globalState.get(MIGRATION_ID)) { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + const old_contexts = context.workspaceState.get<SavedContext[]>(OLD_KEY) |
| 75 | + |
| 76 | + if ( |
| 77 | + old_contexts && |
| 78 | + Array.isArray(old_contexts) && |
| 79 | + old_contexts.length > 0 |
| 80 | + ) { |
| 81 | + const workspace_folders = vscode.workspace.workspaceFolders |
| 82 | + if (workspace_folders && workspace_folders.length > 0) { |
| 83 | + const primary_workspace_root = workspace_folders[0].uri.fsPath |
| 84 | + |
| 85 | + const all_global_contexts = load_all_global_contexts(context) |
| 86 | + |
| 87 | + const existing_contexts = |
| 88 | + all_global_contexts[primary_workspace_root] || [] |
| 89 | + const existing_names = new Set(existing_contexts.map((c) => c.name)) |
| 90 | + const contexts_to_migrate = old_contexts.filter( |
| 91 | + (c) => c.name && !existing_names.has(c.name) |
| 92 | + ) |
| 93 | + |
| 94 | + if (contexts_to_migrate.length > 0) { |
| 95 | + all_global_contexts[primary_workspace_root] = [ |
| 96 | + ...contexts_to_migrate, |
| 97 | + ...existing_contexts |
| 98 | + ] |
| 99 | + save_all_global_contexts(context, all_global_contexts) |
| 100 | + |
| 101 | + Logger.info({ |
| 102 | + function_name: 'migrate_saved_contexts_to_global_storage', |
| 103 | + message: `Successfully migrated ${contexts_to_migrate.length} saved contexts for workspace: ${primary_workspace_root}` |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + await context.workspaceState.update(OLD_KEY, undefined) |
| 109 | + Logger.info({ |
| 110 | + function_name: 'migrate_saved_contexts_to_global_storage', |
| 111 | + message: 'Removed old saved contexts from workspaceState.' |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + await context.globalState.update(MIGRATION_ID, true) |
| 116 | + } catch (error) { |
| 117 | + Logger.error({ |
| 118 | + function_name: 'migrate_saved_contexts_to_global_storage', |
| 119 | + message: 'Error migrating saved contexts to global storage', |
| 120 | + data: error instanceof Error ? error.message : String(error) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments