Skip to content

Commit 65f55fc

Browse files
committed
feat: refactor log item retrieval and report title parsing into helper functions
1 parent 5eeb977 commit 65f55fc

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed

src/helpers.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,69 @@ export function getIconForLogLevel(level: string): vscode.ThemeIcon {
208208
default: return new vscode.ThemeIcon('circle-outline');
209209
}
210210
}
211+
212+
export function getLogItems(dir: string, parseTitle: (filePath: string) => string, getIcon: (filePath: string) => vscode.ThemeIcon): LogItem[] {
213+
if (!pathExists(dir)) {
214+
return [];
215+
}
216+
217+
const items: LogItem[] = [];
218+
const files = fs.readdirSync(dir);
219+
220+
files.forEach(file => {
221+
const filePath = path.join(dir, file);
222+
if (fs.lstatSync(filePath).isDirectory()) {
223+
const subItems = getLogItems(filePath, parseTitle, getIcon);
224+
if (subItems.length > 0) {
225+
items.push(...subItems);
226+
}
227+
} else if (fs.lstatSync(filePath).isFile()) {
228+
const title = parseTitle(filePath);
229+
const logFile = new LogItem(title, vscode.TreeItemCollapsibleState.None, {
230+
command: 'magento-log-viewer.openFile',
231+
title: 'Open Log File',
232+
arguments: [filePath]
233+
});
234+
logFile.iconPath = getIcon(filePath);
235+
items.push(logFile);
236+
}
237+
});
238+
239+
return items;
240+
}
241+
242+
export function parseReportTitle(filePath: string): string {
243+
try {
244+
const fileContent = fs.readFileSync(filePath, 'utf-8');
245+
const report = JSON.parse(fileContent);
246+
247+
if (filePath.includes('/api/')) {
248+
const folderName = path.basename(path.dirname(filePath));
249+
const capitalizedFolderName = folderName.charAt(0).toUpperCase() + folderName.slice(1);
250+
return `${capitalizedFolderName}: ${report}`;
251+
}
252+
253+
return report['0'] || path.basename(filePath);
254+
} catch (error) {
255+
return path.basename(filePath);
256+
}
257+
}
258+
259+
export function getIconForReport(filePath: string): vscode.ThemeIcon {
260+
try {
261+
const fileContent = fs.readFileSync(filePath, 'utf-8');
262+
const report = JSON.parse(fileContent);
263+
264+
if (filePath.includes('/api/')) {
265+
return new vscode.ThemeIcon('warning');
266+
}
267+
268+
if (report['0'] && report['0'].toLowerCase().includes('error')) {
269+
return new vscode.ThemeIcon('error');
270+
}
271+
272+
return new vscode.ThemeIcon('file');
273+
} catch (error) {
274+
return new vscode.ThemeIcon('file');
275+
}
276+
}

src/logViewer.ts

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as path from 'path';
4-
import { pathExists, getLineCount, getIconForLogLevel } from './helpers';
4+
import { pathExists, getLineCount, getIconForLogLevel, getLogItems, parseReportTitle, getIconForReport } from './helpers';
55

66
export class LogViewerProvider implements vscode.TreeDataProvider<LogItem>, vscode.Disposable {
77
private _onDidChangeTreeData: vscode.EventEmitter<LogItem | undefined | void> = new vscode.EventEmitter<LogItem | undefined | void>();
@@ -235,69 +235,7 @@ export class ReportViewerProvider implements vscode.TreeDataProvider<LogItem>, v
235235
}
236236

237237
private getLogItems(dir: string, label: string): LogItem[] {
238-
if (!pathExists(dir)) {
239-
return [];
240-
}
241-
242-
const items: LogItem[] = [];
243-
const files = fs.readdirSync(dir);
244-
245-
files.forEach(file => {
246-
const filePath = path.join(dir, file);
247-
if (fs.lstatSync(filePath).isDirectory()) {
248-
const subItems = this.getLogItems(filePath, label);
249-
if (subItems.length > 0) {
250-
items.push(...subItems);
251-
}
252-
} else if (fs.lstatSync(filePath).isFile()) {
253-
const title = this.parseReportTitle(filePath);
254-
const logFile = new LogItem(title, vscode.TreeItemCollapsibleState.None, {
255-
command: 'magento-log-viewer.openFile',
256-
title: 'Open Log File',
257-
arguments: [filePath]
258-
});
259-
logFile.iconPath = this.getIconForReport(filePath);
260-
items.push(logFile);
261-
}
262-
});
263-
264-
return items;
265-
}
266-
267-
private parseReportTitle(filePath: string): string {
268-
try {
269-
const fileContent = fs.readFileSync(filePath, 'utf-8');
270-
const report = JSON.parse(fileContent);
271-
272-
if (filePath.includes('/api/')) {
273-
const folderName = path.basename(path.dirname(filePath));
274-
const capitalizedFolderName = folderName.charAt(0).toUpperCase() + folderName.slice(1);
275-
return `${capitalizedFolderName}: ${report}`;
276-
}
277-
278-
return report['0'] || path.basename(filePath);
279-
} catch (error) {
280-
return path.basename(filePath);
281-
}
282-
}
283-
284-
private getIconForReport(filePath: string): vscode.ThemeIcon {
285-
try {
286-
const fileContent = fs.readFileSync(filePath, 'utf-8');
287-
const report = JSON.parse(fileContent);
288-
289-
if (filePath.includes('/api/')) {
290-
return new vscode.ThemeIcon('warning');
291-
}
292-
293-
if (report['0'] && report['0'].toLowerCase().includes('error')) {
294-
return new vscode.ThemeIcon('error');
295-
}
296-
297-
return new vscode.ThemeIcon('file');
298-
} catch (error) {
299-
return new vscode.ThemeIcon('file');
300-
}
238+
return getLogItems(dir, parseReportTitle, getIconForReport);
301239
}
302240

303241
getLogFilesWithoutUpdatingBadge(dir: string): LogItem[] {

0 commit comments

Comments
 (0)