Skip to content

Commit ec932f1

Browse files
committed
DT-1095 Add method to replace HTML template on report
1 parent 4b83b52 commit ec932f1

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

Diff for: src/sdk/Report/Report.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import path from 'path';
33
import { DataProviderManager } from './DataLayer/DataProviderManager';
44
import { IDataLayers } from './DataLayer/DataLayerTypes';
55

6-
const reportDefaultPath = path.join(
7-
__dirname,
8-
'../../../../assets/ReportHTMLTemplate/index.html'
9-
);
10-
116
export class Report {
127
private dataProviderManager: DataProviderManager;
138

149
private dataLayer: IDataLayers;
1510

1611
private report: string;
1712

13+
private templatePath: string = path.join(
14+
__dirname,
15+
'../../../../assets/ReportHTMLTemplate/index.html'
16+
);
17+
18+
private dataPlaceholder: string = '#DATA';
1819
constructor(dpm: DataProviderManager = new DataProviderManager()) {
1920
this.dataProviderManager = dpm;
2021
}
@@ -23,11 +24,26 @@ export class Report {
2324
this.dataProviderManager = dpm;
2425
}
2526

27+
public setTemplatePath(filePath: string) {
28+
this.templatePath = filePath;
29+
}
30+
31+
public getTemplatePath(): string {
32+
return this.templatePath;
33+
}
34+
2635
public async getHTML(): Promise<string> {
2736
this.dataLayer = await this.dataProviderManager.generateData();
28-
const html = await fs.promises.readFile(reportDefaultPath, 'utf-8');
37+
const html = await fs.promises.readFile(this.getTemplatePath(), 'utf-8');
2938
if (!html) throw new Error('Invalid template path');
30-
this.report = html.replace('#DATA', JSON.stringify(this.dataLayer));
39+
if (!html.includes(this.dataPlaceholder))
40+
throw new Error(
41+
`Placeholder ${this.dataPlaceholder} not found, cannot insert the data`
42+
);
43+
this.report = html.replace(
44+
this.dataPlaceholder,
45+
JSON.stringify(this.dataLayer)
46+
);
3147
return this.report;
3248
}
3349

0 commit comments

Comments
 (0)