Skip to content

Commit

Permalink
Merge pull request #2 from OpenForgeProject/quality-fixes
Browse files Browse the repository at this point in the history
Prefer using an optional chain expression instead
  • Loading branch information
dermatz authored Nov 19, 2024
2 parents d94d855 + 0449cc1 commit 7bc84a5
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 20 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root: true
parser: '@typescript-eslint/parser'
parserOptions:
project: './tsconfig.json'
plugins:
- '@typescript-eslint'
extends:
- eslint:recommended
- plugin:@typescript-eslint/recommended
env:
node: true
es2022: true
rules:
'@typescript-eslint/no-unsafe-member-access': 'off'
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,62 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

## [1.2.1] - 2024-11-19

### Added

- Added Codacy Code Quality Badge to `README.md`

### Fixed

- Fixed TypeScript ES2022 issues

## [1.2.0] - 2024-11-17

### Added

- Comprehensive bug report template
- Detailed feature request template

### Changed

- Settings are now saved in the workspace instead of globally in the USER.

## [1.1.0] - 2024-11-16

### Added

- Improved the user interface of the webview panel.
- Added line number formatting with leading zeros.
- Removed timestamp and dot from log entries in the summary.

### Changed

- Log levels are now displayed in uppercase format (e.g. ERROR, WARN, DEBUG, INFO, ...)

### Fixed

- Fixed potential security issue with non-literal argument in `fs.existsSync`.
- Fixed potential object injection issue in `groupLogEntries` method.

## [1.0.2] - 2024-10-10

### Changed

- Repository URL to `https://github.com/OpenForgeProject/vscode-ext-magento-log-viewer`

## v1.0.1

### Added

- Extension Logo
- Screenshot in the README file.
- Added a "Getting Started" section to the README.

## v1.0.0

### Added

- View log files in the `var/log` directory of your Magento project.
- Open log files directly in the editor by clicking on them in the tree view.
- Expand log files to view individual lines.
Expand All @@ -46,5 +71,6 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Repository field in `package.json`.

### Changed

- Status bar item now shows the total number of log entries instead of the number of log files.
- Updated README to reflect the change in the status bar item text.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Magento Log Viewer

[![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)

The Magento Log Viewer extension for Visual Studio Code provides a convenient way to view and manage Magento log files directly in your workspace.

## Features
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magento-log-viewer",
"displayName": "Magento Log Viewer",
"description": "A Visual Studio Code extension to view and manage Magento log files.",
"version": "1.2.0",
"version": "1.2.1",
"publisher": "MathiasElle",
"icon": "resources/logo.png",
"repository": {
Expand Down Expand Up @@ -118,6 +118,7 @@
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4",
"@vscode/test-cli": "^0.0.10",
"@vscode/test-electron": "^2.4.1"
"@vscode/test-electron": "^2.4.1",
"vscode": "^1.78.0"
}
}
47 changes: 33 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ export function activate(context: vscode.ExtensionContext) {
const workspaceFolders = vscode.workspace.workspaceFolders;
const defaultUri = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri : undefined;
vscode.window.showOpenDialog({ defaultUri, canSelectFolders: true, canSelectMany: false, openLabel: 'Select Magento Root Folder' }).then(folderUri => {
if (folderUri && folderUri[0]) {
if (folderUri?.[0]) {
config.update('magentoLogViewer.magentoRoot', folderUri[0].fsPath, vscode.ConfigurationTarget.Workspace).then(() => {
vscode.window.showInformationMessage('Magento root folder successfully saved!');
try {
vscode.window.showInformationMessage('Magento root folder successfully saved!');
} catch (error) {
console.error('Failed to show information message:', error instanceof Error ? error.message : String(error));
}
config.update('magentoLogViewer.isMagentoProject', 'Yes', vscode.ConfigurationTarget.Workspace);
activateExtension(context, folderUri[0].fsPath);
});
Expand All @@ -32,7 +36,11 @@ export function activate(context: vscode.ExtensionContext) {
} else if (isMagentoProject === 'Yes') {
const magentoRoot = config.get<string>('magentoLogViewer.magentoRoot', '');
if (!magentoRoot || !isValidPath(magentoRoot)) {
vscode.window.showErrorMessage('Magento root path is not set or is not a directory.');
try {
vscode.window.showErrorMessage('Magento root path is not set or is not a directory.');
} catch (error) {
console.error('Failed to show error message:', error instanceof Error ? error.message : String(error));
}
return;
}
activateExtension(context, magentoRoot);
Expand All @@ -44,17 +52,20 @@ function activateExtension(context: vscode.ExtensionContext, magentoRoot: string
const treeView = vscode.window.createTreeView('logFiles', { treeDataProvider: logViewerProvider });

vscode.commands.registerCommand('magento-log-viewer.refreshLogFiles', () => logViewerProvider.refresh());
vscode.commands.registerCommand('magento-log-viewer.openFile', (filePath, lineNumber) => {
if (lineNumber !== undefined) {
const options: vscode.TextDocumentShowOptions = {
selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0))
};
vscode.window.showTextDocument(vscode.Uri.file(filePath), options);
} else {
vscode.window.showTextDocument(vscode.Uri.file(filePath));
vscode.commands.registerCommand('magento-log-viewer.openFile', (filePath: string, lineNumber?: number) => {

if (typeof filePath === 'string') {
if (lineNumber !== undefined && typeof lineNumber === 'number' && typeof vscode !== 'undefined' && typeof vscode.window !== 'undefined') {
const options: vscode.TextDocumentShowOptions = {
selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0))
};
vscode.window.showTextDocument(vscode.Uri.file(filePath), options);
} else {
vscode.window.showTextDocument(vscode.Uri.file(filePath));
}
}
});
vscode.commands.registerCommand('magento-log-viewer.openFileAtLine', (filePath, lineNumber) => {
vscode.commands.registerCommand('magento-log-viewer.openFileAtLine', (filePath: string, lineNumber: number) => {
const options: vscode.TextDocumentShowOptions = {
selection: new vscode.Range(new vscode.Position(lineNumber, 0), new vscode.Position(lineNumber, 0))
};
Expand All @@ -67,9 +78,17 @@ function activateExtension(context: vscode.ExtensionContext, magentoRoot: string
const files = fs.readdirSync(logPath);
files.forEach(file => fs.unlinkSync(path.join(logPath, file)));
logViewerProvider.refresh();
vscode.window.showInformationMessage('All log files have been cleared.');
try {
vscode.window.showInformationMessage('All log files have been cleared.');
} catch (error) {
console.error('Failed to show information message:', error instanceof Error ? error.message : String(error));
}
} else {
vscode.window.showInformationMessage('No log files found to clear.');
try {
vscode.window.showInformationMessage('No log files found to clear.');
} catch (error) {
console.error('Failed to show information message:', error instanceof Error ? error.message : String(error));
}
}
});

Expand Down
18 changes: 17 additions & 1 deletion src/logViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,28 @@ export class LogViewerProvider implements vscode.TreeDataProvider<LogFile>, vsco
}
}

private isValidLogDirectory(dir: string): boolean {
const normalizedDir = path.normalize(dir);
const normalizedLogPath = path.normalize(path.join(this.workspaceRoot, 'var', 'log'));
return normalizedDir === normalizedLogPath;
}

public getLogFiles(dir: string): LogFile[] {
if (!this.isValidLogDirectory(dir)) {
console.error('Invalid log directory path');
return [];
}

if (this.pathExists(dir)) {
const files = fs.readdirSync(dir);
this.updateBadge();
return files.map(file => {
// Validate file path is within log directory
const filePath = path.join(dir, file);
if (!filePath.startsWith(dir)) {
console.error('Invalid file path detected');
return null;
}
const lineCount = this.getLineCount(filePath);
const logFile = new LogFile(`${file} (${lineCount})`, vscode.TreeItemCollapsibleState.Collapsed, {
command: 'magento-log-viewer.openFile',
Expand All @@ -60,7 +76,7 @@ export class LogViewerProvider implements vscode.TreeDataProvider<LogFile>, vsco
logFile.iconPath = new vscode.ThemeIcon('file');
logFile.children = this.getLogFileLines(filePath);
return logFile;
});
}).filter(Boolean) as LogFile[];
} else {
this.updateBadge();
return [];
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"sourceMap": true,
"rootDir": "src",
"strict": true /* enable all strict type-checking options */
"strict": true, /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
Expand Down

0 comments on commit 7bc84a5

Please sign in to comment.