@@ -12,83 +12,72 @@ module.exports = function templateFinder(log) {
12
12
return {
13
13
14
14
/**
15
- * Set the list of glob that will be searched for matching templates
16
- * @param {string[] } templateFolders A collection of folders to search for templates. The templateFinder
17
- * will also search all subfolders.
15
+ * A collection of folders to search for templates. The templateFinder will also search all
16
+ * subfolders.
18
17
*/
19
- setTemplateFolders : function ( templateFolders ) {
18
+ templateFolders : [ ] ,
19
+
20
+ /**
21
+ * A collection of patterns to use to match templates against documents. The patterns are
22
+ * expanded using lodash template interpolation, by passing in the document to match as `doc`.
23
+ */
24
+ templatePatterns : [ ] ,
20
25
21
- if ( ! templateFolders || ! templateFolders . length ) {
22
- throw new Error ( 'You must provide at least one template folder' ) ;
23
- }
26
+
27
+ getFinder : function ( ) {
24
28
25
29
// Traverse each templateFolder and store an index of the files found for later
26
- this . templateSets = _ . map ( templateFolders , function ( templateFolder ) {
30
+ var templateSets = _ . map ( this . templateFolders , function ( templateFolder ) {
27
31
return {
28
32
templateFolder : templateFolder ,
29
33
templates : _ . indexBy ( glob . sync ( '**/*' , { cwd : templateFolder } ) )
30
34
} ;
31
35
} ) ;
32
- } ,
33
-
34
- /**
35
- * Set the list of patterns that will be used to match documents to templates
36
- * @param {string[] } templatePatterns A collection of patterns to use to match templates against
37
- * documents. The patterns are expanded using lodash template
38
- * interpolation, by passing in the document to match as `doc`.
39
- */
40
- setTemplatePatterns : function ( templatePatterns ) {
41
-
42
- if ( ! templatePatterns || ! templatePatterns . length ) {
43
- throw new Error ( 'You must provide at least one template pattern' ) ;
44
- }
45
36
46
37
// Compile each of the patterns and store them for later
47
- this . patternMatchers = _ . map ( templatePatterns , function ( pattern ) {
38
+ var patternMatchers = _ . map ( this . templatePatterns , function ( pattern ) {
48
39
49
40
// Here we use the lodash micro templating.
50
41
// The document will be available to the micro template as a `doc` variable
51
42
return _ . template ( pattern , null , { variable : 'doc' } ) ;
52
43
} ) ;
53
44
54
- } ,
55
-
56
-
57
- /**
58
- * Find the path to a template for the specified documents
59
- * @param {Object } doc The document for which to find a template
60
- * @return {string } The path to the matched template
61
- */
62
- findTemplate : function ( doc ) {
63
- var templatePath ;
45
+ /**
46
+ * Find the path to a template for the specified documents
47
+ * @param {Object } doc The document for which to find a template
48
+ * @return {string } The path to the matched template
49
+ */
50
+ return function findTemplate ( doc ) {
51
+ var templatePath ;
64
52
65
- // Search the template sets for a matching pattern for the given doc
66
- _ . any ( this . templateSets , function ( templateSet ) {
67
- return _ . any ( this . patternMatchers , function ( patternMatcher ) {
68
- log . silly ( 'looking for ' , patternMatcher ( doc ) ) ;
69
- templatePath = templateSet . templates [ patternMatcher ( doc ) ] ;
70
- if ( templatePath ) {
71
- log . debug ( 'template found' , path . resolve ( templateSet . templateFolder , templatePath ) ) ;
72
- return true ;
73
- }
74
- } , this ) ;
75
- } , this ) ;
53
+ // Search the template sets for a matching pattern for the given doc
54
+ _ . any ( templateSets , function ( templateSet ) {
55
+ return _ . any ( patternMatchers , function ( patternMatcher ) {
56
+ log . silly ( 'looking for ' , patternMatcher ( doc ) ) ;
57
+ templatePath = templateSet . templates [ patternMatcher ( doc ) ] ;
58
+ if ( templatePath ) {
59
+ log . debug ( 'template found' , path . resolve ( templateSet . templateFolder , templatePath ) ) ;
60
+ return true ;
61
+ }
62
+ } ) ;
63
+ } ) ;
76
64
77
- if ( ! templatePath ) {
78
- throw new Error (
79
- 'No template found for "' + ( doc . id || doc . name || doc . docType ) + '" document.\n' +
80
- 'The following template patterns were tried:\n' +
81
- _ . reduce ( this . patternMatchers , function ( str , pattern ) {
82
- return str + ' "' + pattern ( doc ) + '"\n' ;
83
- } , '' ) +
84
- 'The following folders were searched:\n' +
85
- _ . reduce ( this . templateSets , function ( str , templateSet ) {
86
- return str + ' "' + templateSet . templateFolder + '"\n' ;
87
- } , '' )
88
- ) ;
89
- }
65
+ if ( ! templatePath ) {
66
+ throw new Error (
67
+ 'No template found for "' + ( doc . id || doc . name || doc . docType ) + '" document.\n' +
68
+ 'The following template patterns were tried:\n' +
69
+ _ . reduce ( this . patternMatchers , function ( str , pattern ) {
70
+ return str + ' "' + pattern ( doc ) + '"\n' ;
71
+ } , '' ) +
72
+ 'The following folders were searched:\n' +
73
+ _ . reduce ( this . templateSets , function ( str , templateSet ) {
74
+ return str + ' "' + templateSet . templateFolder + '"\n' ;
75
+ } , '' )
76
+ ) ;
77
+ }
90
78
91
- return templatePath ;
79
+ return templatePath ;
80
+ } ;
92
81
}
93
82
} ;
94
83
} ;
0 commit comments