Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow engines to support multiple extensions #694

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions core/lib/pattern_engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,19 @@ const PatternEngines = Object.create({
const of = require('./object_factory');
if (pattern instanceof of.Pattern && typeof pattern.fileExtension === 'string' && pattern.fileExtension) {
//loop through known engines and find the one that supports the pattern's fileExtension
//TODO: support multiple extensions someday, such as .handlebars and .hbs
const engineNames = Object.keys(this);
for (let i = 0; i < engineNames.length; i++) {
const engine = this[engineNames[i]];
if (engine.engineFileExtension === pattern.fileExtension) {
return engine.engineName;

if (Array.isArray(engine.engineFileExtension)) {
if (engine.engineFileExtension.includes(pattern.fileExtension)) {
return engine.engineName;
}
} else {
//this likely means the users engines are out of date. todo: tell them to upgrade
if (engine.engineFileExtension === pattern.fileExtension) {
return engine.engineName;
}
}
}
}
Expand All @@ -147,11 +154,13 @@ const PatternEngines = Object.create({
}
},

// combine all found engines into a single array of supported extensions
getSupportedFileExtensions: function () {
const engineNames = Object.keys(PatternEngines);
return engineNames.map(function (engineName) {
const allEnginesExtensions = engineNames.map((engineName) => {
return PatternEngines[engineName].engineFileExtension;
});
return [].concat.apply([], allEnginesExtensions);
},

isFileExtensionSupported: function (fileExtension) {
Expand Down
25 changes: 23 additions & 2 deletions test/pattern_engines_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function testProps(object, propTests, test) {
});

test.ok(object.hasOwnProperty(propName), '"' + propName + '" prop should be present');
test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes);
test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes + ' but was instead ' + typeof propName);
}

// go over each property test and run it
Expand Down Expand Up @@ -154,7 +154,7 @@ engineNames.forEach(function (engineName) {
var propertyTests = {
'engine': ['object', 'function'],
'engineName': 'string',
'engineFileExtension': 'string',
'engineFileExtension': ['string', 'object'],
'renderPattern': 'function',
'findPartials': 'function'
};
Expand All @@ -164,3 +164,24 @@ engineNames.forEach(function (engineName) {
test.end();
});
});

tap.test('patternEngines getSupportedFileExtensions flattens known engine extensions into a single array', function (test) {

//arrange
patternEngines.fooEngine = {
engineFileExtension : ['.foo1', '.foo2']
};
patternEngines.barEngine = {
engineFileExtension : '.bar'
};

const exts = patternEngines.getSupportedFileExtensions();
test.ok(exts.includes('.foo1'));
test.ok(exts.includes('.foo2'));
test.ok(exts.includes('.bar'));

delete patternEngines.fooEngine;
delete patternEngines.barEngine;

test.end();
});