@@ -3,18 +3,19 @@ import path from 'path';
3
3
import { DataProviderManager } from './DataLayer/DataProviderManager' ;
4
4
import { IDataLayers } from './DataLayer/DataLayerTypes' ;
5
5
6
- const reportDefaultPath = path . join (
7
- __dirname ,
8
- '../../../../assets/ReportHTMLTemplate/index.html'
9
- ) ;
10
-
11
6
export class Report {
12
7
private dataProviderManager : DataProviderManager ;
13
8
14
9
private dataLayer : IDataLayers ;
15
10
16
11
private report : string ;
17
12
13
+ private templatePath : string = path . join (
14
+ __dirname ,
15
+ '../../../../assets/ReportHTMLTemplate/index.html'
16
+ ) ;
17
+
18
+ private dataPlaceholder : string = '#DATA' ;
18
19
constructor ( dpm : DataProviderManager = new DataProviderManager ( ) ) {
19
20
this . dataProviderManager = dpm ;
20
21
}
@@ -23,11 +24,26 @@ export class Report {
23
24
this . dataProviderManager = dpm ;
24
25
}
25
26
27
+ public setTemplatePath ( filePath : string ) {
28
+ this . templatePath = filePath ;
29
+ }
30
+
31
+ public getTemplatePath ( ) : string {
32
+ return this . templatePath ;
33
+ }
34
+
26
35
public async getHTML ( ) : Promise < string > {
27
36
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' ) ;
29
38
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
+ ) ;
31
47
return this . report ;
32
48
}
33
49
0 commit comments