Browse files

redesigning the injector customization

  • Loading branch information...
1 parent d59c33a commit 6384a7d6a6a035a0cbfeccc889d302733ee1e3e9 @cgross committed Mar 3, 2014
Showing with 83 additions and 98 deletions.
  1. +15 −4 CUSTOMIZING.md
  2. +14 −1 app/index.js
  3. +5 −22 directive/index.js
  4. +2 −17 filter/index.js
  5. +1 −18 partial/index.js
  6. +1 −17 service/index.js
  7. +45 −19 utils.js
View
19 CUSTOMIZING.md
@@ -28,10 +28,10 @@ Type: `String`
Directory to load filter template files from
-#### injector
-Type: `String`
+#### inject
+Type: `Object`
-A NodeJS module name (path relative to the `.yo-rc.json`). The module must export one function. That function can extend or override the standard reference injecting logic of the generator (i.e. injecting <script> tags in index.html or injecting @import statements in the app.less). The function takes two arguments: the `filename` of the file that may need injection and a `logger` function that can be used to write output. Return `true` from this function to prevent standard reference injecting logic for the given filename. Otherwise, the standard reference injecting logic will be executed after this function is complete. For example, if you've overriden one or more of the templates for a given sub-generator and included a new `.scss` template, you may need to inject an @import statement into your primary scss file.
+A map of file extensions where each extension key contains another map containing 3 properties (file, template, marker). The sub-generators use these options to inject script tags and import statements. These options can be extended to add new file types, or override to change the existing injection behavior.
## Example Configuration
@@ -49,7 +49,18 @@ Here is an example configuration that matches the default behavior of the sub-ge
directiveComplexTemplates: "templates/complexDirective",
serviceTemplates: "templates/service",
filterTemplates: "templates/filter",
- injector: "lib/myInjector.js"
+ "inject": {
+ "js": {
+ "file": "index.html",
+ "marker": "<!-- Add New Component JS Above -->",
+ "template": "<script src=\"<%= filename %>\"></script>"
+ },
+ "less": {
+ "file": "app.less",
+ "marker": "/* Add Component LESS Above */",
+ "template": "@import \"<%= filename %>\";"
+ }
+ }
}
```
View
15 app/index.js
@@ -2,7 +2,7 @@
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
-
+var cgUtils = require('../utils.js');
var CgangularGenerator = module.exports = function CgangularGenerator(args, options, config) {
yeoman.generators.Base.apply(this, arguments);
@@ -12,6 +12,19 @@ var CgangularGenerator = module.exports = function CgangularGenerator(args, opti
this.config.set('directiveDirectory','directive/');
this.config.set('filterDirectory','filter/');
this.config.set('serviceDirectory','service/');
+ var inject = {
+ js: {
+ file: 'index.html',
+ marker: cgUtils.JS_MARKER,
+ template: '<script src="<%= filename %>"></script>'
+ },
+ less: {
+ file: 'app.less',
+ marker: cgUtils.LESS_MARKER,
+ template: '@import "<%= filename %>";'
+ }
+ };
+ this.config.set('inject',inject);
this.config.save();
this.installDependencies({ skipInstall: options['skip-install'] });
});
View
27 directive/index.js
@@ -60,30 +60,13 @@ DirectiveGenerator.prototype.askFor = function askFor() {
DirectiveGenerator.prototype.files = function files() {
- var templateDirectory = path.join(path.dirname(this.resolved),'templates','simple');
- if(this.config.get('directiveSimpleTemplates')){
- templateDirectory = path.join(process.cwd(),this.config.get('directiveSimpleTemplates'));
- }
-
+ var configName = 'directiveSimpleTemplates';
+ var defaultDir = 'templates/simple';
if (this.needpartial) {
- templateDirectory = path.join(path.dirname(this.resolved),'templates','complex');
- if(this.config.get('directiveComplexTemplates')){
- templateDirectory = path.join(process.cwd(),this.config.get('directiveComplexTemplates'));
- }
+ configName = 'directiveComplexTemplates';
+ defaultDir = 'templates/complex';
}
- var that = this;
- _.chain(fs.readdirSync(templateDirectory))
- .filter(function(template){
- return template[0] !== '.';
- })
- .each(function(template){
- var customTemplateName = template.replace('directive',that.name);
- var templateFile = path.join(templateDirectory,template);
- //create the file
- that.template(templateFile,that.dir + customTemplateName);
- //inject the file reference into index.html/app.less/etc as appropriate
- cgUtils.doInjection(that.dir + customTemplateName,that.log,that.config);
- });
+ cgUtils.processTemplates(this.name,this.dir,'service',this,defaultDir,configName);
};
View
19 filter/index.js
@@ -49,21 +49,6 @@ FilterGenerator.prototype.askFor = function askFor() {
FilterGenerator.prototype.files = function files() {
- var templateDirectory = path.join(path.dirname(this.resolved),'templates');
- if(this.config.get('filterTemplates')){
- templateDirectory = path.join(process.cwd(),this.config.get('filterTemplates'));
- }
- var that = this;
- _.chain(fs.readdirSync(templateDirectory))
- .filter(function(template){
- return template[0] !== '.';
- })
- .each(function(template){
- var customTemplateName = template.replace('filter',that.name);
- var templateFile = path.join(templateDirectory,template);
- //create the file
- that.template(templateFile,that.dir + customTemplateName);
- //inject the file reference into index.html/app.less/etc as appropriate
- cgUtils.doInjection(that.dir + customTemplateName,that.log,that.config);
- });
+ cgUtils.processTemplates(this.name,this.dir,'filter',this);
+
};
View
19 partial/index.js
@@ -57,24 +57,7 @@ PartialGenerator.prototype.files = function files() {
this.ctrlname = _.camelize(_.classify(this.name)) + 'Ctrl';
- var templateDirectory = path.join(path.dirname(this.resolved),'templates');
- if(this.config.get('partialTemplates')){
- templateDirectory = path.join(process.cwd(),this.config.get('partialTemplates'));
- }
- var that = this;
- _.chain(fs.readdirSync(templateDirectory))
- .filter(function(template){
- return template[0] !== '.';
- })
- .each(function(template){
- var customTemplateName = template.replace('partial',that.name);
- var templateFile = path.join(templateDirectory,template);
- //create the file
- that.template(templateFile,that.dir + customTemplateName);
- //inject the file reference into index.html/app.less/etc as appropriate
- cgUtils.doInjection(that.dir + customTemplateName,that.log,that.config);
- });
-
+ cgUtils.processTemplates(this.name,this.dir,'partial',this);
if (this.route && this.route.length > 0){
View
18 service/index.js
@@ -49,22 +49,6 @@ ServiceGenerator.prototype.askFor = function askFor() {
ServiceGenerator.prototype.files = function files() {
- var templateDirectory = path.join(path.dirname(this.resolved),'templates');
- if(this.config.get('serviceTemplates')){
- templateDirectory = path.join(process.cwd(),this.config.get('serviceTemplates'));
- }
- var that = this;
- _.chain(fs.readdirSync(templateDirectory))
- .filter(function(template){
- return template[0] !== '.';
- })
- .each(function(template){
- var customTemplateName = template.replace('service',that.name);
- var templateFile = path.join(templateDirectory,template);
- //create the file
- that.template(templateFile,that.dir + customTemplateName);
- //inject the file reference into index.html/app.less/etc as appropriate
- cgUtils.doInjection(that.dir + customTemplateName,that.log,that.config);
- });
+ cgUtils.processTemplates(this.name,this.dir,'service',this);
};
View
64 utils.js
@@ -12,7 +12,9 @@ exports.addToFile = function(filename,lineToAdd,beforeMarker,spacing){
var fileSrc = fs.readFileSync(fullPath,'utf8');
var indexOf = fileSrc.indexOf(beforeMarker);
- fileSrc = fileSrc.substring(0,indexOf) + lineToAdd + "\n" + spacing + fileSrc.substring(indexOf);
+ var lineStart = fileSrc.substring(0,indexOf).lastIndexOf('\n') + 1;
+ var indent = fileSrc.substring(lineStart,indexOf);
+ fileSrc = fileSrc.substring(0,indexOf) + lineToAdd + "\n" + indent + fileSrc.substring(indexOf);
fs.writeFileSync(fullPath,fileSrc);
@@ -41,23 +43,47 @@ exports.cleanDirectory = function(directoryName) {
return directoryName + '/';
};
-exports.doInjection = function(filename,logger,config) {
- if (config.get('injector')) {
- var overridenInjector = require(path.join(process.cwd(),config.get('injector')));
- if (overridenInjector(filename,logger)) {
- return;
- }
- }
- exports.defaultInjector(filename,logger);
-}
-
-exports.defaultInjector = function(filename,logger) {
- if (path.extname(filename) === '.js' && !_(filename).endsWith('-spec.js')) {
- exports.addToFile('index.html','<script src="' + filename + '"></script>',exports.JS_MARKER,' ');
- logger.writeln(chalk.green(' updating') + ' %s','index.html');
- } else if (path.extname(filename) === '.less') {
- exports.addToFile('app.less','@import "' + filename + '";',exports.LESS_MARKER,'');
- logger.writeln(chalk.green(' updating') + ' %s','app.less');
- }
+exports.processTemplates = function(name,dir,type,that,defaultDir,configName){
+
+ if (!defaultDir) {
+ defaultDir = 'templates'
+ }
+ if (!configName) {
+ configName = type + 'Templates';
+ }
+
+ var templateDirectory = path.join(path.dirname(that.resolved),defaultDir);
+ if(that.config.get(configName)){
+ templateDirectory = path.join(process.cwd(),that.config.get(configName));
+ }
+ _.chain(fs.readdirSync(templateDirectory))
+ .filter(function(template){
+ return template[0] !== '.';
+ })
+ .each(function(template){
+ var customTemplateName = template.replace(type,name);
+ var templateFile = path.join(templateDirectory,template);
+ //create the file
+ that.template(templateFile,dir + customTemplateName);
+ //inject the file reference into index.html/app.less/etc as appropriate
+ exports.inject(dir + customTemplateName,that);
+ });
+};
+
+exports.inject = function(filename,that) {
+ //special case to skip unit tests
+ if (_(filename).endsWith('-spec.js')) {
+ return;
+ }
+ var ext = path.extname(filename);
+ if (ext[0] === '.') {
+ ext = ext.substring(1);
+ }
+ var config = that.config.get('inject')[ext];
+ if (config) {
+ var lineTemplate = _.template(config.template)({filename:filename});
+ exports.addToFile(config.file,lineTemplate,config.marker);
+ that.log.writeln(chalk.green(' updating') + ' %s',config.file);
+ }
};

0 comments on commit 6384a7d

Please sign in to comment.