Skip to content

Commit 7bc84a5

Browse files
authored
Merge pull request #2 from OpenForgeProject/quality-fixes
Prefer using an optional chain expression instead
2 parents d94d855 + 0449cc1 commit 7bc84a5

File tree

8 files changed

+98
-20
lines changed

8 files changed

+98
-20
lines changed

.eslintrc.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
root: true
2+
parser: '@typescript-eslint/parser'
3+
parserOptions:
4+
project: './tsconfig.json'
5+
plugins:
6+
- '@typescript-eslint'
7+
extends:
8+
- eslint:recommended
9+
- plugin:@typescript-eslint/recommended
10+
env:
11+
node: true
12+
es2022: true
13+
rules:
14+
'@typescript-eslint/no-unsafe-member-access': 'off'

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,62 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
66

77
## [Unreleased]
88

9+
## [1.2.1] - 2024-11-19
10+
11+
### Added
12+
13+
- Added Codacy Code Quality Badge to `README.md`
14+
15+
### Fixed
16+
17+
- Fixed TypeScript ES2022 issues
18+
919
## [1.2.0] - 2024-11-17
20+
1021
### Added
22+
1123
- Comprehensive bug report template
1224
- Detailed feature request template
25+
1326
### Changed
27+
1428
- Settings are now saved in the workspace instead of globally in the USER.
1529

1630
## [1.1.0] - 2024-11-16
31+
1732
### Added
33+
1834
- Improved the user interface of the webview panel.
1935
- Added line number formatting with leading zeros.
2036
- Removed timestamp and dot from log entries in the summary.
37+
2138
### Changed
39+
2240
- Log levels are now displayed in uppercase format (e.g. ERROR, WARN, DEBUG, INFO, ...)
2341

2442
### Fixed
43+
2544
- Fixed potential security issue with non-literal argument in `fs.existsSync`.
2645
- Fixed potential object injection issue in `groupLogEntries` method.
2746

2847
## [1.0.2] - 2024-10-10
48+
2949
### Changed
50+
3051
- Repository URL to `https://github.com/OpenForgeProject/vscode-ext-magento-log-viewer`
3152

3253
## v1.0.1
54+
3355
### Added
56+
3457
- Extension Logo
3558
- Screenshot in the README file.
3659
- Added a "Getting Started" section to the README.
3760

3861
## v1.0.0
62+
3963
### Added
64+
4065
- View log files in the `var/log` directory of your Magento project.
4166
- Open log files directly in the editor by clicking on them in the tree view.
4267
- Expand log files to view individual lines.
@@ -46,5 +71,6 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
4671
- Repository field in `package.json`.
4772

4873
### Changed
74+
4975
- Status bar item now shows the total number of log entries instead of the number of log files.
5076
- Updated README to reflect the change in the status bar item text.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Magento Log Viewer
22

3+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/04d20d74a4bb4f7fb144d320f7008edb)](https://app.codacy.com/gh/OpenForgeProject/vscode-ext-magento-log-viewer/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
4+
35
The Magento Log Viewer extension for Visual Studio Code provides a convenient way to view and manage Magento log files directly in your workspace.
46

57
## Features

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento-log-viewer",
33
"displayName": "Magento Log Viewer",
44
"description": "A Visual Studio Code extension to view and manage Magento log files.",
5-
"version": "1.2.0",
5+
"version": "1.2.1",
66
"publisher": "MathiasElle",
77
"icon": "resources/logo.png",
88
"repository": {
@@ -118,6 +118,7 @@
118118
"webpack": "^5.95.0",
119119
"webpack-cli": "^5.1.4",
120120
"@vscode/test-cli": "^0.0.10",
121-
"@vscode/test-electron": "^2.4.1"
121+
"@vscode/test-electron": "^2.4.1",
122+
"vscode": "^1.78.0"
122123
}
123124
}

src/extension.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ export function activate(context: vscode.ExtensionContext) {
1515
const workspaceFolders = vscode.workspace.workspaceFolders;
1616
const defaultUri = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri : undefined;
1717
vscode.window.showOpenDialog({ defaultUri, canSelectFolders: true, canSelectMany: false, openLabel: 'Select Magento Root Folder' }).then(folderUri => {
18-
if (folderUri && folderUri[0]) {
18+
if (folderUri?.[0]) {
1919
config.update('magentoLogViewer.magentoRoot', folderUri[0].fsPath, vscode.ConfigurationTarget.Workspace).then(() => {
20-
vscode.window.showInformationMessage('Magento root folder successfully saved!');
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+
}
2125
config.update('magentoLogViewer.isMagentoProject', 'Yes', vscode.ConfigurationTarget.Workspace);
2226
activateExtension(context, folderUri[0].fsPath);
2327
});
@@ -32,7 +36,11 @@ export function activate(context: vscode.ExtensionContext) {
3236
} else if (isMagentoProject === 'Yes') {
3337
const magentoRoot = config.get<string>('magentoLogViewer.magentoRoot', '');
3438
if (!magentoRoot || !isValidPath(magentoRoot)) {
35-
vscode.window.showErrorMessage('Magento root path is not set or is not a directory.');
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+
}
3644
return;
3745
}
3846
activateExtension(context, magentoRoot);
@@ -44,17 +52,20 @@ function activateExtension(context: vscode.ExtensionContext, magentoRoot: string
4452
const treeView = vscode.window.createTreeView('logFiles', { treeDataProvider: logViewerProvider });
4553

4654
vscode.commands.registerCommand('magento-log-viewer.refreshLogFiles', () => logViewerProvider.refresh());
47-
vscode.commands.registerCommand('magento-log-viewer.openFile', (filePath, lineNumber) => {
48-
if (lineNumber !== undefined) {
49-
const options: vscode.TextDocumentShowOptions = {
50-
selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0))
51-
};
52-
vscode.window.showTextDocument(vscode.Uri.file(filePath), options);
53-
} else {
54-
vscode.window.showTextDocument(vscode.Uri.file(filePath));
55+
vscode.commands.registerCommand('magento-log-viewer.openFile', (filePath: string, lineNumber?: number) => {
56+
57+
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+
}
5566
}
5667
});
57-
vscode.commands.registerCommand('magento-log-viewer.openFileAtLine', (filePath, lineNumber) => {
68+
vscode.commands.registerCommand('magento-log-viewer.openFileAtLine', (filePath: string, lineNumber: number) => {
5869
const options: vscode.TextDocumentShowOptions = {
5970
selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0))
6071
};
@@ -67,9 +78,17 @@ function activateExtension(context: vscode.ExtensionContext, magentoRoot: string
6778
const files = fs.readdirSync(logPath);
6879
files.forEach(file => fs.unlinkSync(path.join(logPath, file)));
6980
logViewerProvider.refresh();
70-
vscode.window.showInformationMessage('All log files have been cleared.');
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+
}
7186
} else {
72-
vscode.window.showInformationMessage('No log files found to clear.');
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+
}
7392
}
7493
});
7594

src/logViewer.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,28 @@ export class LogViewerProvider implements vscode.TreeDataProvider<LogFile>, vsco
4545
}
4646
}
4747

48+
private isValidLogDirectory(dir: string): boolean {
49+
const normalizedDir = path.normalize(dir);
50+
const normalizedLogPath = path.normalize(path.join(this.workspaceRoot, 'var', 'log'));
51+
return normalizedDir === normalizedLogPath;
52+
}
53+
4854
public getLogFiles(dir: string): LogFile[] {
55+
if (!this.isValidLogDirectory(dir)) {
56+
console.error('Invalid log directory path');
57+
return [];
58+
}
59+
4960
if (this.pathExists(dir)) {
5061
const files = fs.readdirSync(dir);
5162
this.updateBadge();
5263
return files.map(file => {
64+
// Validate file path is within log directory
5365
const filePath = path.join(dir, file);
66+
if (!filePath.startsWith(dir)) {
67+
console.error('Invalid file path detected');
68+
return null;
69+
}
5470
const lineCount = this.getLineCount(filePath);
5571
const logFile = new LogFile(`${file} (${lineCount})`, vscode.TreeItemCollapsibleState.Collapsed, {
5672
command: 'magento-log-viewer.openFile',
@@ -60,7 +76,7 @@ export class LogViewerProvider implements vscode.TreeDataProvider<LogFile>, vsco
6076
logFile.iconPath = new vscode.ThemeIcon('file');
6177
logFile.children = this.getLogFileLines(filePath);
6278
return logFile;
63-
});
79+
}).filter(Boolean) as LogFile[];
6480
} else {
6581
this.updateBadge();
6682
return [];

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"sourceMap": true,
99
"rootDir": "src",
10-
"strict": true /* enable all strict type-checking options */
10+
"strict": true, /* enable all strict type-checking options */
1111
/* Additional Checks */
1212
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
1313
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

0 commit comments

Comments
 (0)