Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit e8c015d

Browse files
refact(templateFinder): now call getFinder() to get the actual function
This change allows the templateFinder to be configured before getting the actual function.
1 parent 081e8cb commit e8c015d

File tree

2 files changed

+57
-95
lines changed

2 files changed

+57
-95
lines changed

base/services/templateFinder.js

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,72 @@ module.exports = function templateFinder(log) {
1212
return {
1313

1414
/**
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.
1817
*/
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: [],
2025

21-
if ( !templateFolders || !templateFolders.length ) {
22-
throw new Error('You must provide at least one template folder');
23-
}
26+
27+
getFinder: function() {
2428

2529
// 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) {
2731
return {
2832
templateFolder: templateFolder,
2933
templates: _.indexBy(glob.sync('**/*', { cwd: templateFolder }))
3034
};
3135
});
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-
}
4536

4637
// 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) {
4839

4940
// Here we use the lodash micro templating.
5041
// The document will be available to the micro template as a `doc` variable
5142
return _.template(pattern, null, { variable: 'doc' });
5243
});
5344

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;
6452

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+
});
7664

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+
}
9078

91-
return templatePath;
79+
return templatePath;
80+
};
9281
}
9382
};
9483
};

base/spec/services/templateFinder.spec.js

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,10 @@ describe("templateFinder", function() {
1010
templateFinder = templateFinderFactory(mockLog);
1111
});
1212

13-
describe("setTemplateFolders()", function() {
1413

15-
it("should complain if no template folders were provided", function() {
14+
describe("getFinder", function() {
1615

17-
expect(function() {
18-
templateFinder.setTemplateFolders(null);
19-
}).toThrow();
20-
21-
expect(function() {
22-
templateFinder.setTemplateFolders([]);
23-
}).toThrow();
24-
25-
});
26-
});
27-
28-
describe("setTemplatePatterns()", function() {
29-
30-
it("should complain if no template patterns were provided", function() {
31-
32-
expect(function() {
33-
templateFinder.setTemplatePatterns(null);
34-
}).toThrow();
35-
36-
expect(function() {
37-
templateFinder.setTemplatePatterns([]);
38-
}).toThrow();
39-
40-
});
41-
});
42-
43-
describe("findTemplate", function() {
44-
45-
var glob, patterns, templateFolders;
16+
var glob, patterns, templateFolders, findTemplate;
4617

4718
beforeEach(function() {
4819
glob = templateFinderFactory.__get__('glob');
@@ -56,34 +27,36 @@ describe("templateFinder", function() {
5627
];
5728
templateFolders = ['abc'];
5829

59-
templateFinder.setTemplateFolders(templateFolders);
60-
templateFinder.setTemplatePatterns(patterns);
30+
templateFinder.templateFolders = templateFolders;
31+
templateFinder.templatePatterns = patterns;
32+
33+
findTemplate = templateFinder.getFinder();
6134
});
6235

6336

6437
it("should match id followed by doctype if both are provided and the file exists", function() {
65-
expect(templateFinder.findTemplate({ docType: 'a', id: 'c'})).toEqual('c.a.x');
38+
expect(findTemplate({ docType: 'a', id: 'c'})).toEqual('c.a.x');
6639
});
6740

6841

6942
it("should match id before docType", function() {
70-
expect(templateFinder.findTemplate({ docType: 'a', id: 'b' })).toEqual('b.x');
43+
expect(findTemplate({ docType: 'a', id: 'b' })).toEqual('b.x');
7144
});
7245

7346

7447
it("should match docType if id doesn't match", function() {
75-
expect(templateFinder.findTemplate({ docType: 'a', id: 'missing' })).toEqual('a.x');
48+
expect(findTemplate({ docType: 'a', id: 'missing' })).toEqual('a.x');
7649
});
7750

7851

7952
it("should match docType if id is undefined", function() {
80-
expect(templateFinder.findTemplate({ docType: 'a' })).toEqual('a.x');
53+
expect(findTemplate({ docType: 'a' })).toEqual('a.x');
8154
});
8255

8356

8457
it("should throw an error if no template was found", function() {
8558
expect(function() {
86-
templateFinder.findTemplate({docType:'missing'});
59+
findTemplate({docType:'missing'});
8760
}).toThrow();
8861
});
8962

0 commit comments

Comments
 (0)