Update FileListRenderer to class

This commit is contained in:
Jordan Welch 2023-09-18 18:41:51 -05:00
parent 14989f1ddd
commit 4e6bb49788
No known key found for this signature in database
GPG key ID: 8C1872FF2F7710B3
3 changed files with 40 additions and 29 deletions

View file

@ -1,4 +1,4 @@
import { render } from '../file-list-renderer'; import { FileListRenderer } from '../file-list-renderer';
import HoganJsUtils from '../hoganjs-utils'; import HoganJsUtils from '../hoganjs-utils';
describe('FileListRenderer', () => { describe('FileListRenderer', () => {
@ -10,6 +10,7 @@ describe('FileListRenderer', () => {
'file-summary-line': '{{oldName}}, {{newName}}, {{fileName}}', 'file-summary-line': '{{oldName}}, {{newName}}, {{fileName}}',
}, },
}); });
const fileListRenderer = new FileListRenderer(hoganUtils);
const files = [ const files = [
{ {
isCombined: false, isCombined: false,
@ -55,7 +56,7 @@ describe('FileListRenderer', () => {
}, },
]; ];
const fileHtml = render(files, hoganUtils); const fileHtml = fileListRenderer.render(files);
expect(fileHtml).toMatchInlineSnapshot(` expect(fileHtml).toMatchInlineSnapshot(`
"my/file/name.js, my/file/name.js, my/file/name.js "my/file/name.js, my/file/name.js, my/file/name.js
@ -67,6 +68,8 @@ describe('FileListRenderer', () => {
it('should work for all kinds of files', () => { it('should work for all kinds of files', () => {
const hoganUtils = new HoganJsUtils({}); const hoganUtils = new HoganJsUtils({});
const fileListRenderer = new FileListRenderer(hoganUtils);
const files = [ const files = [
{ {
isCombined: false, isCombined: false,
@ -111,7 +114,7 @@ describe('FileListRenderer', () => {
isDeleted: true, isDeleted: true,
}, },
]; ];
const fileHtml = render(files, hoganUtils); const fileHtml = fileListRenderer.render(files);
expect(fileHtml).toMatchInlineSnapshot(` expect(fileHtml).toMatchInlineSnapshot(`
"<div class="d2h-file-list-wrapper"> "<div class="d2h-file-list-wrapper">
<div class="d2h-file-list-header"> <div class="d2h-file-list-header">

View file

@ -1,5 +1,5 @@
import * as DiffParser from './diff-parser'; import * as DiffParser from './diff-parser';
import * as fileListPrinter from './file-list-renderer'; import { FileListRenderer } from './file-list-renderer';
import LineByLineRenderer, { LineByLineRendererConfig, defaultLineByLineRendererConfig } from './line-by-line-renderer'; import LineByLineRenderer, { LineByLineRendererConfig, defaultLineByLineRendererConfig } from './line-by-line-renderer';
import SideBySideRenderer, { SideBySideRendererConfig, defaultSideBySideRendererConfig } from './side-by-side-renderer'; import SideBySideRenderer, { SideBySideRendererConfig, defaultSideBySideRendererConfig } from './side-by-side-renderer';
import { DiffFile, OutputFormatType } from './types'; import { DiffFile, OutputFormatType } from './types';
@ -32,7 +32,7 @@ export function html(diffInput: string | DiffFile[], configuration: Diff2HtmlCon
const hoganUtils = new HoganJsUtils(config); const hoganUtils = new HoganJsUtils(config);
const fileList = config.drawFileList ? fileListPrinter.render(diffJson, hoganUtils) : ''; const fileList = config.drawFileList ? new FileListRenderer(hoganUtils).render(diffJson) : '';
const diffOutput = const diffOutput =
config.outputFormat === 'side-by-side' config.outputFormat === 'side-by-side'

View file

@ -5,10 +5,17 @@ import { DiffFile } from './types';
const baseTemplatesPath = 'file-summary'; const baseTemplatesPath = 'file-summary';
const iconsBaseTemplatesPath = 'icon'; const iconsBaseTemplatesPath = 'icon';
export function render(diffFiles: DiffFile[], hoganUtils: HoganJsUtils): string { export class FileListRenderer {
private readonly hoganUtils: HoganJsUtils;
constructor(hoganUtils: HoganJsUtils) {
this.hoganUtils = hoganUtils;
}
render(diffFiles: DiffFile[]): string {
const files = diffFiles const files = diffFiles
.map(file => .map(file =>
hoganUtils.render( this.hoganUtils.render(
baseTemplatesPath, baseTemplatesPath,
'line', 'line',
{ {
@ -20,14 +27,15 @@ export function render(diffFiles: DiffFile[], hoganUtils: HoganJsUtils): string
addedLines: '+' + file.addedLines, addedLines: '+' + file.addedLines,
}, },
{ {
fileIcon: hoganUtils.template(iconsBaseTemplatesPath, renderUtils.getFileIcon(file)), fileIcon: this.hoganUtils.template(iconsBaseTemplatesPath, renderUtils.getFileIcon(file)),
}, },
), ),
) )
.join('\n'); .join('\n');
return hoganUtils.render(baseTemplatesPath, 'wrapper', { return this.hoganUtils.render(baseTemplatesPath, 'wrapper', {
filesNumber: diffFiles.length, filesNumber: diffFiles.length,
files: files, files: files,
}); });
} }
}