-
Notifications
You must be signed in to change notification settings - Fork 122
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
[GPT-565] Dynamic support to oc-templates in oc dev #426
Changes from 9 commits
119b6b1
12e8518
dceabca
f7ecbaf
bef1241
113910e
54b8f24
12fb303
21d79c0
ace3bde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,56 @@ | ||
'use strict'; | ||
|
||
var format = require('stringformat'); | ||
var templateNotSupported = 'Error loading component: template "{0}" not supported'; | ||
var path = require('path'); | ||
var _ = require('underscore'); | ||
|
||
module.exports = function(type) { | ||
var templateNotFound = 'Error requiring oc-template: "{0}" not found'; | ||
var templateNotValid = 'Error requiring oc-template: "{0}" is not a valid oc-template'; | ||
|
||
function isValidTemplate(template){ | ||
if (typeof template !== 'object') { | ||
return false; | ||
} | ||
|
||
var apiMethods = _.keys(template); | ||
if (apiMethods.length !== 4) { | ||
return false; | ||
} | ||
|
||
return _.contains( | ||
apiMethods, | ||
'getInfo', | ||
'getCompiledTemplate', | ||
'compile', | ||
'render' | ||
); | ||
} | ||
|
||
|
||
module.exports = function(template) { | ||
var ocTemplate; | ||
var localTemplate = path.join(__dirname, '../../', 'node_modules', template); | ||
var relativeTemplate = path.resolve('.', 'node_modules', template); | ||
|
||
try { | ||
return require(type); | ||
} catch (err) { | ||
throw format(templateNotSupported, type); | ||
if (require.cache && !!require.cache[localTemplate]) { | ||
delete require.cache[localTemplate]; | ||
} | ||
ocTemplate = require(localTemplate); | ||
} catch(err) { | ||
try { | ||
if (require.cache && !!require.cache[relativeTemplate]) { | ||
delete require.cache[relativeTemplate]; | ||
} | ||
ocTemplate = require(relativeTemplate); | ||
} catch (err) { | ||
throw format(templateNotFound, template); | ||
} | ||
} | ||
}; | ||
|
||
if (!isValidTemplate(ocTemplate)) { | ||
throw format(templateNotValid, template); | ||
} | ||
|
||
return ocTemplate; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,48 @@ | |
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
var _ = require('underscore'); | ||
var format = require('stringformat'); | ||
var settings = require('../../resources'); | ||
|
||
module.exports = function(components){ | ||
var deps = { modules: {}, withVersions: {}, templates: {} }; | ||
|
||
var deps = { modules: [], withVersions: [] }; | ||
|
||
_.forEach(components, function(c){ | ||
var legacyTemplates = { | ||
'jade': true, | ||
'handlebars': true | ||
}; | ||
|
||
components.forEach(function(c){ | ||
var pkg = fs.readJsonSync(path.join(c, 'package.json')); | ||
var type = pkg.oc.files.template.type; | ||
var dependencies = pkg.dependencies; | ||
|
||
_.forEach(_.keys(pkg.dependencies), function(d){ | ||
if (!deps.templates[type] && !legacyTemplates[type]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think var legacyTemplates = ['jade', 'handlebars']
...
if (!deps.templates[type] && !_.includes(legacyTemplates, type)) { would be a bit more readable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nit and very arguable weather an array of 2 elements is more readable then an object with 2 keys.. I've removed the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's why I put it as question. I don't have strong opinions on this. Let's leave it as is if you like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add check that if templatetype is not legacy must be declared as dependency |
||
if (!dependencies[type]) { | ||
throw new Error(format(settings.errors.cli.TEMPLATE_DEP_MISSING, type)); | ||
} | ||
deps.templates[type] = true; | ||
} | ||
|
||
var version = pkg.dependencies[d], | ||
hasVersion = !_.isEmpty(version), | ||
depToInstall = hasVersion ? (d + '@' + version) : d; | ||
_.keys(dependencies).forEach(function(name){ | ||
var version = dependencies[name]; | ||
var depToInstall = version.length > 0 | ||
? (name + '@' + version) | ||
: name; | ||
|
||
if(!_.contains(deps.withVersions, depToInstall)){ | ||
deps.withVersions.push(depToInstall); | ||
if (!deps.withVersions[depToInstall]) { | ||
deps.withVersions[depToInstall] = true; | ||
} | ||
|
||
if(!_.contains(deps.modules, d)){ | ||
deps.modules.push(d); | ||
if (!deps.modules[name]) { | ||
deps.modules[name] = true; | ||
} | ||
}); | ||
}); | ||
|
||
return deps; | ||
return { | ||
modules: _.keys(deps.modules), | ||
withVersions: _.keys(deps.withVersions), | ||
templates: _.keys(deps.templates) | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ var settings = require('../../resources/settings'); | |
var strings = require('../../resources'); | ||
var validator = require('./validators'); | ||
var versionHandler = require('./version-handler'); | ||
var requireTemplate = require('../../utils/require-template'); | ||
|
||
module.exports = function(conf){ | ||
|
||
|
@@ -26,17 +27,21 @@ module.exports = function(conf){ | |
var coreTemplates = ['oc-template-jade', 'oc-template-handlebars']; | ||
var templates = _.union(coreTemplates, conf.templates) | ||
.map(function(template){ | ||
var info; | ||
try { | ||
info = require(template).getInfo(); | ||
var ocTemplate = requireTemplate(template); | ||
var info = ocTemplate.getInfo(); | ||
return { | ||
type: info.type, | ||
version: info.version, | ||
externals: info.externals | ||
}; | ||
} catch (err) { | ||
throw new Error(format(strings.errors.registry.TEMPLATE_NOT_FOUND, template)); | ||
throw err; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's re-add a try catch here for a template that, in case is not valid, may throw as getInfo doesn't exist or is not a function. Error wouldn't be "template not found" anymore but more of a "template not valid" kind? |
||
return { | ||
type: info.type, | ||
version: info.version, | ||
externals: info.externals | ||
}; | ||
|
||
|
||
|
||
|
||
}); | ||
|
||
var local = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,56 @@ | ||
'use strict'; | ||
|
||
var format = require('stringformat'); | ||
var templateNotSupported = 'Error requiring the template "{0}": oc-template not found'; | ||
var path = require('path'); | ||
var _ = require('underscore'); | ||
|
||
module.exports = function(type) { | ||
var templateNotFound = 'Error requiring oc-template: "{0}" not found'; | ||
var templateNotValid = 'Error requiring oc-template: "{0}" is not a valid oc-template'; | ||
|
||
function isValidTemplate(template){ | ||
if (typeof template !== 'object') { | ||
return false; | ||
} | ||
|
||
var apiMethods = _.keys(template); | ||
if (apiMethods.length !== 4) { | ||
return false; | ||
} | ||
|
||
return _.contains( | ||
apiMethods, | ||
'getInfo', | ||
'getCompiledTemplate', | ||
'compile', | ||
'render' | ||
); | ||
} | ||
|
||
module.exports = function(template) { | ||
var ocTemplate; | ||
var localTemplate = path.join(__dirname, '../../', 'node_modules', template); | ||
var relativeTemplate = path.resolve('.', 'node_modules', template); | ||
|
||
|
||
try { | ||
return require(type); | ||
} catch (err) { | ||
throw format(templateNotSupported, type); | ||
if (require.cache && !!require.cache[localTemplate]) { | ||
delete require.cache[localTemplate]; | ||
} | ||
ocTemplate = require(localTemplate); | ||
} catch(err) { | ||
try { | ||
if (require.cache && !!require.cache[relativeTemplate]) { | ||
delete require.cache[relativeTemplate]; | ||
} | ||
ocTemplate = require(relativeTemplate); | ||
} catch (err) { | ||
throw format(templateNotFound, template); | ||
} | ||
} | ||
|
||
if (!isValidTemplate(ocTemplate)) { | ||
throw format(templateNotValid, template); | ||
} | ||
}; | ||
|
||
return ocTemplate; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't have underscore in the client! Made the same mistake when reviewing the previous PR with @mattiaerre and then realised - are you able to go vanilla with client's code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, actually I like it even more without it.