Skip to content

Commit bb47678

Browse files
committed
Generate the list of supported pattern engines based on the presence to
real plugin files rather than a hard-coded list. Also, better organize the PatternEngine setup functions to be cleaner, more functional and more testable
1 parent bdb0b7b commit bb47678

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

builder/pattern_engines/pattern_engines.js

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,88 @@
1313
'use strict';
1414

1515
var path = require('path');
16+
var diveSync = require('diveSync');
17+
var engineMatcher = /^engine_(.*)\.js/;
18+
var enginesDirectory = __dirname;
1619

17-
// list of supported pattern engines
18-
var supportedPatternEngineNames = [
19-
'mustache',
20-
'handlebars'
21-
];
20+
var PatternEngines; // the main export object
21+
var engineNameForExtension; // generated mapping of extension to engine name
2222

23-
// mapping of file extensions to engine names, for lookup use
24-
var engineNameForExtension = {};
2523

26-
// Object/hash of all loaded pattern engines, empty at first.
27-
// My intention here is to make this return an object that can be used to
28-
// obtain any loaded PatternEngine by addressing them like this:
24+
// free private functions, for internal setup only
25+
26+
function findSupportedPatternEngineNames() {
27+
var foundPatternEngineNames = [];
28+
29+
console.log('patternEngines: begin diveSync ====================');
30+
31+
// find
32+
diveSync(enginesDirectory, {
33+
recursive: false,
34+
filter: function (filePath, dir) {
35+
var baseName = path.basename(filePath),
36+
engineMatch = baseName.match(engineMatcher);
37+
38+
if (dir || engineMatch !== null) { return true; }
39+
return false;
40+
}
41+
}, function (err, filePath) {
42+
if (err) { throw err; }
43+
var baseName = path.basename(filePath),
44+
engineMatch = baseName.match(engineMatcher),
45+
foundEngineName = engineMatch[1];
46+
47+
console.log('patternEngines: FOUND ENGINE', foundEngineName);
48+
foundPatternEngineNames.push(foundEngineName);
49+
});
50+
51+
return foundPatternEngineNames;
52+
}
53+
54+
// try to load all supported engines
55+
function loadAllEngines(enginesObject) {
56+
enginesObject.supportedPatternEngineNames.forEach(function (engineName) {
57+
try {
58+
enginesObject[engineName] = require('./engine_' + engineName);
59+
} catch (err) {
60+
console.log(err, 'pattern engine "' + engineName + '" not loaded. Did you install its dependency with npm?');
61+
}
62+
});
63+
}
64+
65+
// produce a mapping between file extension and engine name for each of the
66+
// loaded engines
67+
function createFileExtensionToEngineNameMap(enginesObject) {
68+
var mapping = {};
69+
70+
Object.keys(enginesObject).forEach(function (engineName) {
71+
var extensionForEngine = enginesObject[engineName].engineFileExtension;
72+
mapping[extensionForEngine] = engineName;
73+
});
74+
75+
return mapping;
76+
}
77+
78+
79+
//
80+
// PatternEngines: the main export of this module
81+
//
82+
// It's an Object/hash of all loaded pattern engines, empty at first. My
83+
// intention here is to make this return an object that can be used to obtain
84+
// any loaded PatternEngine by addressing them like this:
2985
//
30-
// var PatternEngines = require('./pattern_engines/pattern_engines');
31-
// var Mustache = PatternEngines['mustache'];
86+
// var PatternEngines = require('./pattern_engines/pattern_engines');
87+
// var Mustache = PatternEngines['mustache'];
3288
//
33-
var PatternEngines = Object.create({
34-
supportedPatternEngineNames: supportedPatternEngineNames,
89+
// Object.create lets us create an object with a specified prototype. We want
90+
// this here because we would like the object's "own properties" to include
91+
// only the engine names so we can easily iterate over them; all the handy
92+
// methods and properites below should therefore be on its prototype.
93+
94+
PatternEngines = Object.create({
95+
// build the list of supported pattern engines based on what plugins we have
96+
// in the pattern_engines directory
97+
supportedPatternEngineNames: findSupportedPatternEngineNames(),
3598

3699
getEngineNameForPattern: function (pattern) {
37100
// avoid circular dependency by putting this in here. TODO: is this slow?
@@ -93,30 +156,12 @@
93156
}
94157
});
95158

96-
// try to load all supported engines
97-
(function loadAllEngines() {
98-
supportedPatternEngineNames.forEach(function (engineName) {
99-
try {
100-
PatternEngines[engineName] = require('./engine_' + engineName);
101-
} catch (err) {
102-
console.log(err, 'pattern engine "' + engineName + '" not loaded. Did you install its dependency with npm?');
103-
}
104-
});
105-
})();
106-
107-
// produce a mapping between file extension and engine name for each of the
108-
// loaded engines
109-
engineNameForExtension = (function () {
110-
var mapping = {};
111159

112-
Object.keys(PatternEngines).forEach(function (engineName) {
113-
var extensionForEngine = PatternEngines[engineName].engineFileExtension;
114-
mapping[extensionForEngine] = engineName;
115-
});
116-
117-
return mapping;
118-
})();
160+
// load up the engines we found
161+
loadAllEngines(PatternEngines);
119162

163+
// mapping of file extensions to engine names, for lookup use
164+
engineNameForExtension = createFileExtensionToEngineNameMap(PatternEngines);
120165

121166
module.exports = PatternEngines;
122167
})();

0 commit comments

Comments
 (0)