|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import { LogViewerProvider } from './logViewer'; |
| 5 | + |
| 6 | +// Prompts the user to confirm if the current project is a Magento project. |
| 7 | +export function promptMagentoProjectSelection(config: vscode.WorkspaceConfiguration, context: vscode.ExtensionContext): void { |
| 8 | + vscode.window.showInformationMessage('Is this a Magento project?', 'Yes', 'No').then(selection => { |
| 9 | + if (selection === 'Yes') { |
| 10 | + selectMagentoRootFolder(config, context); |
| 11 | + } else { |
| 12 | + updateConfig(config, 'magentoLogViewer.isMagentoProject', selection); |
| 13 | + } |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +// Prompts the user to select the Magento root folder and updates the configuration. |
| 18 | +export function selectMagentoRootFolder(config: vscode.WorkspaceConfiguration, context: vscode.ExtensionContext): void { |
| 19 | + vscode.window.showInformationMessage('Please select the Magento root folder.', 'Select Magento Root Folder').then(buttonSelection => { |
| 20 | + if (buttonSelection === 'Select Magento Root Folder') { |
| 21 | + const workspaceFolders = vscode.workspace.workspaceFolders; |
| 22 | + const defaultUri = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri : undefined; |
| 23 | + vscode.window.showOpenDialog({ defaultUri, canSelectFolders: true, canSelectMany: false, openLabel: 'Select Magento Root Folder' }).then(folderUri => { |
| 24 | + if (folderUri?.[0]) { |
| 25 | + updateConfig(config, 'magentoLogViewer.magentoRoot', folderUri[0].fsPath).then(() => { |
| 26 | + showInformationMessage('Magento root folder successfully saved!'); |
| 27 | + updateConfig(config, 'magentoLogViewer.isMagentoProject', 'Yes'); |
| 28 | + activateExtension(context, folderUri[0].fsPath); |
| 29 | + }); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +// Updates the specified configuration key with the given value. |
| 37 | +export function updateConfig(config: vscode.WorkspaceConfiguration, key: string, value: unknown): Thenable<void> { |
| 38 | + return config.update(key, value, vscode.ConfigurationTarget.Workspace); |
| 39 | +} |
| 40 | + |
| 41 | +// Displays an information message to the user. |
| 42 | +export function showInformationMessage(message: string): void { |
| 43 | + try { |
| 44 | + vscode.window.showInformationMessage(message); |
| 45 | + } catch (error) { |
| 46 | + console.error('Failed to show information message:', error instanceof Error ? error.message : String(error)); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Displays an error message to the user. |
| 51 | +export function showErrorMessage(message: string): void { |
| 52 | + try { |
| 53 | + vscode.window.showErrorMessage(message); |
| 54 | + } catch (error) { |
| 55 | + console.error('Failed to show error message:', error instanceof Error ? error.message : String(error)); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Activates the extension by setting up the log viewer and file system watcher. |
| 60 | +export function activateExtension(context: vscode.ExtensionContext, magentoRoot: string): void { |
| 61 | + const logViewerProvider = new LogViewerProvider(magentoRoot); |
| 62 | + const treeView = vscode.window.createTreeView('logFiles', { treeDataProvider: logViewerProvider }); |
| 63 | + |
| 64 | + registerCommands(context, logViewerProvider, magentoRoot); |
| 65 | + context.subscriptions.push(treeView); |
| 66 | + |
| 67 | + updateBadge(treeView, logViewerProvider, magentoRoot); |
| 68 | + |
| 69 | + const logPath = path.join(magentoRoot, 'var', 'log'); |
| 70 | + const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(logPath, '*')); |
| 71 | + watcher.onDidChange(() => logViewerProvider.refresh()); |
| 72 | + watcher.onDidCreate(() => logViewerProvider.refresh()); |
| 73 | + watcher.onDidDelete(() => logViewerProvider.refresh()); |
| 74 | + |
| 75 | + context.subscriptions.push(watcher); |
| 76 | +} |
| 77 | + |
| 78 | +// Registers commands for the extension. |
| 79 | +export function registerCommands(context: vscode.ExtensionContext, logViewerProvider: LogViewerProvider, magentoRoot: string): void { |
| 80 | + vscode.commands.registerCommand('magento-log-viewer.refreshLogFiles', () => logViewerProvider.refresh()); |
| 81 | + vscode.commands.registerCommand('magento-log-viewer.openFile', (filePath: string, lineNumber?: number) => { |
| 82 | + openFile(filePath, lineNumber); |
| 83 | + }); |
| 84 | + vscode.commands.registerCommand('magento-log-viewer.openFileAtLine', (filePath: string, lineNumber: number) => { |
| 85 | + openFile(filePath, lineNumber); |
| 86 | + }); |
| 87 | + vscode.commands.registerCommand('magento-log-viewer.clearAllLogFiles', () => { |
| 88 | + clearAllLogFiles(logViewerProvider, magentoRoot); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +// Opens a file in the editor at the specified line number. |
| 93 | +export function openFile(filePath: string, lineNumber?: number): void { |
| 94 | + if (typeof filePath === 'string') { |
| 95 | + const options: vscode.TextDocumentShowOptions = lineNumber !== undefined && typeof lineNumber === 'number' ? { |
| 96 | + selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0)) |
| 97 | + } : {}; |
| 98 | + vscode.window.showTextDocument(vscode.Uri.file(filePath), options); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Clears all log files in the Magento log directory. |
| 103 | +export function clearAllLogFiles(logViewerProvider: LogViewerProvider, magentoRoot: string): void { |
| 104 | + vscode.window.showWarningMessage('Are you sure you want to delete all log files?', 'Yes', 'No').then(selection => { |
| 105 | + if (selection === 'Yes') { |
| 106 | + const logPath = path.join(magentoRoot, 'var', 'log'); |
| 107 | + if (logViewerProvider.pathExists(logPath)) { |
| 108 | + const files = fs.readdirSync(logPath); |
| 109 | + files.forEach(file => fs.unlinkSync(path.join(logPath, file))); |
| 110 | + logViewerProvider.refresh(); |
| 111 | + showInformationMessage('All log files have been cleared.'); |
| 112 | + } else { |
| 113 | + showInformationMessage('No log files found to clear.'); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +// Updates the badge count for the tree view based on the number of log entries. |
| 120 | +export function updateBadge(treeView: vscode.TreeView<unknown>, logViewerProvider: LogViewerProvider, magentoRoot: string): void { |
| 121 | + const updateBadgeCount = () => { |
| 122 | + const logFiles = logViewerProvider.getLogFilesWithoutUpdatingBadge(path.join(magentoRoot, 'var', 'log')); |
| 123 | + const totalEntries = logFiles.reduce((count, file) => count + parseInt(file.description?.match(/\d+/)?.[0] || '0', 10), 0); |
| 124 | + treeView.badge = { value: totalEntries, tooltip: `${totalEntries} log entries` }; |
| 125 | + |
| 126 | + vscode.commands.executeCommand('setContext', 'magentoLogViewer.hasLogFiles', totalEntries > 0); |
| 127 | + }; |
| 128 | + |
| 129 | + logViewerProvider.onDidChangeTreeData(updateBadgeCount); |
| 130 | + updateBadgeCount(); |
| 131 | + |
| 132 | + vscode.commands.executeCommand('setContext', 'magentoLogViewerBadge', 0); |
| 133 | +} |
| 134 | + |
| 135 | +// Checks if the given path is a valid directory. |
| 136 | +export function isValidPath(filePath: string): boolean { |
| 137 | + try { |
| 138 | + return fs.existsSync(filePath) && fs.lstatSync(filePath).isDirectory(); |
| 139 | + } catch (error) { |
| 140 | + return false; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Checks if the given path exists. |
| 145 | +export function pathExists(p: string): boolean { |
| 146 | + try { |
| 147 | + fs.accessSync(p); |
| 148 | + } catch (err) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
| 154 | +// Returns the number of lines in the specified file. |
| 155 | +export function getLineCount(filePath: string): number { |
| 156 | + const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 157 | + return fileContent.split('\n').length; |
| 158 | +} |
| 159 | + |
| 160 | +// Returns the appropriate icon for the given log level. |
| 161 | +export function getIconForLogLevel(level: string): vscode.ThemeIcon { |
| 162 | + switch (level) { |
| 163 | + case 'ERROR': return new vscode.ThemeIcon('error'); |
| 164 | + case 'WARN': return new vscode.ThemeIcon('warning'); |
| 165 | + case 'DEBUG': return new vscode.ThemeIcon('debug'); |
| 166 | + case 'INFO': return new vscode.ThemeIcon('info'); |
| 167 | + default: return new vscode.ThemeIcon('circle-outline'); |
| 168 | + } |
| 169 | +} |
0 commit comments