|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { Extension } from '../constants'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Utilities for managing workspace extension recommendations. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Check if workspace contains Dev Proxy config files. |
| 11 | + */ |
| 12 | +export async function hasDevProxyConfig(): Promise<boolean> { |
| 13 | + const files = await vscode.workspace.findFiles( |
| 14 | + '{devproxyrc.json,devproxyrc.jsonc}' |
| 15 | + ); |
| 16 | + return files.length > 0; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Check if the Dev Proxy Toolkit extension is already in workspace recommendations. |
| 21 | + */ |
| 22 | +export async function isExtensionRecommended(): Promise<boolean> { |
| 23 | + if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + const workspaceFolder = vscode.workspace.workspaceFolders[0]; |
| 28 | + const extensionsJsonPath = path.join(workspaceFolder.uri.fsPath, Extension.extensionsJsonPath); |
| 29 | + |
| 30 | + try { |
| 31 | + const uri = vscode.Uri.file(extensionsJsonPath); |
| 32 | + const document = await vscode.workspace.openTextDocument(uri); |
| 33 | + const content = document.getText(); |
| 34 | + const json = JSON.parse(content); |
| 35 | + |
| 36 | + if (json.recommendations && Array.isArray(json.recommendations)) { |
| 37 | + return json.recommendations.includes(Extension.id); |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + // File doesn't exist or can't be parsed |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Add the Dev Proxy Toolkit extension to workspace recommendations. |
| 49 | + */ |
| 50 | +export async function addExtensionToRecommendations(): Promise<boolean> { |
| 51 | + if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + const workspaceFolder = vscode.workspace.workspaceFolders[0]; |
| 56 | + const vscodeFolderPath = path.join(workspaceFolder.uri.fsPath, '.vscode'); |
| 57 | + const extensionsJsonPath = path.join(workspaceFolder.uri.fsPath, Extension.extensionsJsonPath); |
| 58 | + |
| 59 | + try { |
| 60 | + let json: { recommendations?: string[] } = {}; |
| 61 | + |
| 62 | + // Try to read existing file |
| 63 | + try { |
| 64 | + const uri = vscode.Uri.file(extensionsJsonPath); |
| 65 | + const document = await vscode.workspace.openTextDocument(uri); |
| 66 | + json = JSON.parse(document.getText()); |
| 67 | + } catch { |
| 68 | + // File doesn't exist or can't be parsed, create new structure |
| 69 | + json = { recommendations: [] }; |
| 70 | + } |
| 71 | + |
| 72 | + // Ensure recommendations array exists |
| 73 | + if (!json.recommendations) { |
| 74 | + json.recommendations = []; |
| 75 | + } |
| 76 | + |
| 77 | + // Add extension if not already present |
| 78 | + if (!json.recommendations.includes(Extension.id)) { |
| 79 | + json.recommendations.push(Extension.id); |
| 80 | + } |
| 81 | + |
| 82 | + // Create .vscode directory if it doesn't exist |
| 83 | + try { |
| 84 | + await vscode.workspace.fs.createDirectory(vscode.Uri.file(vscodeFolderPath)); |
| 85 | + } catch { |
| 86 | + // Directory might already exist |
| 87 | + } |
| 88 | + |
| 89 | + // Write the updated file |
| 90 | + const uri = vscode.Uri.file(extensionsJsonPath); |
| 91 | + const content = JSON.stringify(json, null, 2); |
| 92 | + await vscode.workspace.fs.writeFile(uri, Buffer.from(content, 'utf8')); |
| 93 | + |
| 94 | + return true; |
| 95 | + } catch (error) { |
| 96 | + console.error('Error adding extension to recommendations:', error); |
| 97 | + return false; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Prompt user to add the extension to workspace recommendations. |
| 103 | + */ |
| 104 | +export async function promptForWorkspaceRecommendation(context: vscode.ExtensionContext): Promise<void> { |
| 105 | + // Check if we've already prompted for this workspace |
| 106 | + const workspaceKey = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ''; |
| 107 | + // Use a safe storage key by replacing path separators with underscores |
| 108 | + const storageKey = `recommendation-prompted-${workspaceKey.replace(/[/\\:]/g, '_')}`; |
| 109 | + |
| 110 | + if (context.globalState.get(storageKey)) { |
| 111 | + // Already prompted for this workspace |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // Check if workspace has Dev Proxy config |
| 116 | + const hasConfig = await hasDevProxyConfig(); |
| 117 | + if (!hasConfig) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + // Check if extension is already recommended |
| 122 | + const isRecommended = await isExtensionRecommended(); |
| 123 | + if (isRecommended) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // Show prompt |
| 128 | + const message = 'This workspace contains Dev Proxy configuration files. Would you like to add the Dev Proxy Toolkit extension to workspace recommendations?'; |
| 129 | + const result = await vscode.window.showInformationMessage(message, 'Yes', 'No', 'Don\'t ask again'); |
| 130 | + |
| 131 | + if (result === 'Yes') { |
| 132 | + const success = await addExtensionToRecommendations(); |
| 133 | + if (success) { |
| 134 | + vscode.window.showInformationMessage('Dev Proxy Toolkit added to workspace recommendations.'); |
| 135 | + } else { |
| 136 | + vscode.window.showErrorMessage('Failed to add extension to workspace recommendations.'); |
| 137 | + } |
| 138 | + } else if (result === 'Don\'t ask again') { |
| 139 | + await context.globalState.update(storageKey, true); |
| 140 | + } |
| 141 | +} |
0 commit comments