@@ -10,59 +10,70 @@ export function activate(context: vscode.ExtensionContext) {
10
10
if ( isMagentoProject === 'Please select' ) {
11
11
vscode . window . showInformationMessage ( 'Is this a Magento project?' , 'Yes' , 'No' ) . then ( selection => {
12
12
if ( selection === 'Yes' ) {
13
- vscode . window . showInformationMessage ( 'Please select the Magento root folder.' , 'Select Magento Root Folder' ) . then ( buttonSelection => {
14
- if ( buttonSelection === 'Select Magento Root Folder' ) {
15
- const workspaceFolders = vscode . workspace . workspaceFolders ;
16
- const defaultUri = workspaceFolders && workspaceFolders . length > 0 ? workspaceFolders [ 0 ] . uri : undefined ;
17
- vscode . window . showOpenDialog ( { defaultUri, canSelectFolders : true , canSelectMany : false , openLabel : 'Select Magento Root Folder' } ) . then ( folderUri => {
18
- if ( folderUri ?. [ 0 ] ) {
19
- config . update ( 'magentoLogViewer.magentoRoot' , folderUri [ 0 ] . fsPath , vscode . ConfigurationTarget . Workspace ) . then ( ( ) => {
20
- try {
21
- vscode . window . showInformationMessage ( 'Magento root folder successfully saved!' ) ;
22
- } catch ( error ) {
23
- console . error ( 'Failed to show information message:' , error instanceof Error ? error . message : String ( error ) ) ;
24
- }
25
- config . update ( 'magentoLogViewer.isMagentoProject' , 'Yes' , vscode . ConfigurationTarget . Workspace ) ;
26
- activateExtension ( context , folderUri [ 0 ] . fsPath ) ;
27
- } ) ;
28
- }
29
- } ) ;
30
- }
31
- } ) ;
13
+ selectMagentoRootFolder ( config , context ) ;
32
14
} else {
33
- config . update ( 'magentoLogViewer.isMagentoProject' , selection , vscode . ConfigurationTarget . Workspace ) ;
15
+ updateConfig ( config , 'magentoLogViewer.isMagentoProject' , selection ) ;
34
16
}
35
17
} ) ;
36
18
} else if ( isMagentoProject === 'Yes' ) {
37
19
const magentoRoot = config . get < string > ( 'magentoLogViewer.magentoRoot' , '' ) ;
38
20
if ( ! magentoRoot || ! isValidPath ( magentoRoot ) ) {
39
- try {
40
- vscode . window . showErrorMessage ( 'Magento root path is not set or is not a directory.' ) ;
41
- } catch ( error ) {
42
- console . error ( 'Failed to show error message:' , error instanceof Error ? error . message : String ( error ) ) ;
43
- }
21
+ showErrorMessage ( 'Magento root path is not set or is not a directory.' ) ;
44
22
return ;
45
23
}
46
24
activateExtension ( context , magentoRoot ) ;
47
25
}
48
26
}
49
27
28
+ function selectMagentoRootFolder ( config : vscode . WorkspaceConfiguration , context : vscode . ExtensionContext ) {
29
+ vscode . window . showInformationMessage ( 'Please select the Magento root folder.' , 'Select Magento Root Folder' ) . then ( buttonSelection => {
30
+ if ( buttonSelection === 'Select Magento Root Folder' ) {
31
+ const workspaceFolders = vscode . workspace . workspaceFolders ;
32
+ const defaultUri = workspaceFolders && workspaceFolders . length > 0 ? workspaceFolders [ 0 ] . uri : undefined ;
33
+ vscode . window . showOpenDialog ( { defaultUri, canSelectFolders : true , canSelectMany : false , openLabel : 'Select Magento Root Folder' } ) . then ( folderUri => {
34
+ if ( folderUri ?. [ 0 ] ) {
35
+ updateConfig ( config , 'magentoLogViewer.magentoRoot' , folderUri [ 0 ] . fsPath ) . then ( ( ) => {
36
+ showInformationMessage ( 'Magento root folder successfully saved!' ) ;
37
+ updateConfig ( config , 'magentoLogViewer.isMagentoProject' , 'Yes' ) ;
38
+ activateExtension ( context , folderUri [ 0 ] . fsPath ) ;
39
+ } ) ;
40
+ }
41
+ } ) ;
42
+ }
43
+ } ) ;
44
+ }
45
+
46
+ function updateConfig ( config : vscode . WorkspaceConfiguration , key : string , value : unknown ) {
47
+ return config . update ( key , value , vscode . ConfigurationTarget . Workspace ) ;
48
+ }
49
+
50
+ function showInformationMessage ( message : string ) {
51
+ try {
52
+ vscode . window . showInformationMessage ( message ) ;
53
+ } catch ( error ) {
54
+ console . error ( 'Failed to show information message:' , error instanceof Error ? error . message : String ( error ) ) ;
55
+ }
56
+ }
57
+
58
+ function showErrorMessage ( message : string ) {
59
+ try {
60
+ vscode . window . showErrorMessage ( message ) ;
61
+ } catch ( error ) {
62
+ console . error ( 'Failed to show error message:' , error instanceof Error ? error . message : String ( error ) ) ;
63
+ }
64
+ }
65
+
50
66
function activateExtension ( context : vscode . ExtensionContext , magentoRoot : string ) {
51
67
const logViewerProvider = new LogViewerProvider ( magentoRoot ) ;
52
68
const treeView = vscode . window . createTreeView ( 'logFiles' , { treeDataProvider : logViewerProvider } ) ;
53
69
54
70
vscode . commands . registerCommand ( 'magento-log-viewer.refreshLogFiles' , ( ) => logViewerProvider . refresh ( ) ) ;
55
71
vscode . commands . registerCommand ( 'magento-log-viewer.openFile' , ( filePath : string , lineNumber ?: number ) => {
56
-
57
72
if ( typeof filePath === 'string' ) {
58
- if ( lineNumber !== undefined && typeof lineNumber === 'number' && typeof vscode !== 'undefined' && typeof vscode . window !== 'undefined' ) {
59
- const options : vscode . TextDocumentShowOptions = {
60
- selection : new vscode . Range ( new vscode . Position ( lineNumber , 0 ) , new vscode . Position ( lineNumber , 0 ) )
61
- } ;
62
- vscode . window . showTextDocument ( vscode . Uri . file ( filePath ) , options ) ;
63
- } else {
64
- vscode . window . showTextDocument ( vscode . Uri . file ( filePath ) ) ;
65
- }
73
+ const options : vscode . TextDocumentShowOptions = lineNumber !== undefined && typeof lineNumber === 'number' ? {
74
+ selection : new vscode . Range ( new vscode . Position ( lineNumber , 0 ) , new vscode . Position ( lineNumber , 0 ) )
75
+ } : { } ;
76
+ vscode . window . showTextDocument ( vscode . Uri . file ( filePath ) , options ) ;
66
77
}
67
78
} ) ;
68
79
vscode . commands . registerCommand ( 'magento-log-viewer.openFileAtLine' , ( filePath : string , lineNumber : number ) => {
@@ -78,17 +89,9 @@ function activateExtension(context: vscode.ExtensionContext, magentoRoot: string
78
89
const files = fs . readdirSync ( logPath ) ;
79
90
files . forEach ( file => fs . unlinkSync ( path . join ( logPath , file ) ) ) ;
80
91
logViewerProvider . refresh ( ) ;
81
- try {
82
- vscode . window . showInformationMessage ( 'All log files have been cleared.' ) ;
83
- } catch ( error ) {
84
- console . error ( 'Failed to show information message:' , error instanceof Error ? error . message : String ( error ) ) ;
85
- }
92
+ showInformationMessage ( 'All log files have been cleared.' ) ;
86
93
} else {
87
- try {
88
- vscode . window . showInformationMessage ( 'No log files found to clear.' ) ;
89
- } catch ( error ) {
90
- console . error ( 'Failed to show information message:' , error instanceof Error ? error . message : String ( error ) ) ;
91
- }
94
+ showInformationMessage ( 'No log files found to clear.' ) ;
92
95
}
93
96
} ) ;
94
97
0 commit comments