@@ -31,14 +31,14 @@ export const kPathTemplateComponentRE = /__(.+?)__/g;
31
31
export const kPathTemplatePipeRE = / @ ( [ ^ @ ] + ) / ;
32
32
33
33
34
- export type TemplateValue = boolean | string | number | undefined ;
35
- export type TemplatePipeFunction = ( x : string ) => TemplateValue ;
36
- export type TemplateOptions = {
37
- [ key : string ] : TemplateValue | TemplateOptions | TemplatePipeFunction ,
34
+ export type PathTemplateValue = boolean | string | number | undefined ;
35
+ export type PathTemplatePipeFunction = ( x : string ) => PathTemplateValue ;
36
+ export type PathTemplateOptions = {
37
+ [ key : string ] : PathTemplateValue | PathTemplateOptions | PathTemplatePipeFunction ,
38
38
} ;
39
39
40
40
41
- export function applyContentTemplate < T extends TemplateOptions > ( options : T ) : FileOperator {
41
+ export function applyContentTemplate < T > ( options : T ) : FileOperator {
42
42
return ( entry : FileEntry ) => {
43
43
const { path, content} = entry ;
44
44
if ( isBinary ( content ) ) {
@@ -53,12 +53,12 @@ export function applyContentTemplate<T extends TemplateOptions>(options: T): Fil
53
53
}
54
54
55
55
56
- export function contentTemplate < T extends TemplateOptions > ( options : T ) : Rule {
56
+ export function contentTemplate < T > ( options : T ) : Rule {
57
57
return forEach ( applyContentTemplate ( options ) ) ;
58
58
}
59
59
60
60
61
- export function applyPathTemplate < T extends TemplateOptions > ( options : T ) : FileOperator {
61
+ export function applyPathTemplate < T extends PathTemplateOptions > ( options : T ) : FileOperator {
62
62
return ( entry : FileEntry ) => {
63
63
let path = entry . path ;
64
64
const content = entry . content ;
@@ -67,9 +67,11 @@ export function applyPathTemplate<T extends TemplateOptions>(options: T): FileOp
67
67
// Path template.
68
68
path = normalize ( path . replace ( kPathTemplateComponentRE , ( _ , match ) => {
69
69
const [ name , ...pipes ] = match . split ( kPathTemplatePipeRE ) ;
70
- const value = typeof options [ name ] == 'function'
71
- ? ( options [ name ] as TemplatePipeFunction ) . call ( options , original )
72
- : options [ name ] ;
70
+ let value = options [ name ] ;
71
+
72
+ if ( typeof value == 'function' ) {
73
+ value = value . call ( options , original ) ;
74
+ }
73
75
74
76
if ( value === undefined ) {
75
77
throw new OptionIsNotDefinedException ( name ) ;
@@ -87,7 +89,7 @@ export function applyPathTemplate<T extends TemplateOptions>(options: T): FileOp
87
89
}
88
90
89
91
// Coerce to string.
90
- return '' + ( options [ pipe ] as TemplatePipeFunction ) ( acc ) ;
92
+ return '' + ( options [ pipe ] as PathTemplatePipeFunction ) ( acc ) ;
91
93
} , '' + value ) ;
92
94
} ) ) ;
93
95
@@ -96,14 +98,17 @@ export function applyPathTemplate<T extends TemplateOptions>(options: T): FileOp
96
98
}
97
99
98
100
99
- export function pathTemplate < T extends TemplateOptions > ( options : T ) : Rule {
101
+ export function pathTemplate < T extends PathTemplateOptions > ( options : T ) : Rule {
100
102
return forEach ( applyPathTemplate ( options ) ) ;
101
103
}
102
104
103
105
104
- export function template < T extends TemplateOptions > ( options : T ) : Rule {
106
+ export function template < T > ( options : T ) : Rule {
105
107
return chain ( [
106
108
contentTemplate ( options ) ,
107
- pathTemplate ( options ) ,
109
+ // Force cast to PathTemplateOptions. We need the type for the actual pathTemplate() call,
110
+ // but in this case we cannot do anything as contentTemplate are more permissive.
111
+ // Since values are coerced to strings in PathTemplates it will be fine in the end.
112
+ pathTemplate ( options as { } as PathTemplateOptions ) ,
108
113
] ) ;
109
114
}
0 commit comments