diff --git a/README.md b/README.md index 96d54ca..03fd454 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,41 @@ yo bbr:router routername directory/to/put/it yo bbr:view viewname directory/to/put/it ``` +Collection subgenerators also take a third parameter (optional) that allows you to define the path to your model: + +``` +yo bbr:collection collectionname directory/to/put/it path/to/model +``` + +So, in the case that you have the following structure: + +``` +|-- js +| |-- models +| |-- test +| |-- test.js +``` + +And you want to create a collection for TestModel (the name of the model from test.js), you would run this: + +``` +yo bbr:collection test test test # first is name of collection, second is directory to create it in, third is path to model +``` + +This would generate the following: + +``` +|-- js +| |-- collections +| |-- test +| |-- test.js # TestCollection +| |-- models +| |-- test +| |-- test.js # TestModel +``` + +Now `TestCollection` references `TestModel` with the define statement looking for `models/test/test` and passing it in as `TestModel` for reference within the collection. + **NOTE: Do not put beginning or trailing slashes on the directory structure!** diff --git a/app/index.js b/app/index.js index b053215..bd265fe 100644 --- a/app/index.js +++ b/app/index.js @@ -25,8 +25,8 @@ var Generator = module.exports = function Generator(args, options, config) { } }); - this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'index.html')); - + this.sourceRoot(path.join(__dirname, '../templates')); + this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'common/index.html')); this.on('end', function () { if (['app', 'bbr'].indexOf(this.generatorName) >= 0) { this.installDependencies({ skipInstall: this.options['skip-install'] }); @@ -50,36 +50,34 @@ Generator.prototype.askFor = function askFor() { function hasFeature(feat) { return features.indexOf(feat) !== -1; } - // this.includeRequireJS = true; - cb(); }.bind(this)); }; Generator.prototype.git = function git() { - this.copy('gitignore', '.gitignore'); - this.copy('gitattributes', '.gitattributes'); + this.copy('common/gitignore', '.gitignore'); + this.copy('common/gitattributes', '.gitattributes'); }; Generator.prototype.bower = function bower() { - this.copy('bowerrc', '.bowerrc'); - this.copy('_bower.json', 'bower.json'); + this.copy('common/bowerrc', '.bowerrc'); + this.copy('common/_bower.json', 'bower.json'); }; Generator.prototype.gruntfile = function gruntfile() { - this.template('Gruntfile.js'); + this.template('common/Gruntfile.js', 'Gruntfile.js'); }; Generator.prototype.packageJSON = function packageJSON() { - this.template('_package.json', 'package.json'); + this.template('common/_package.json', 'package.json'); }; Generator.prototype.configRB = function packageJSON() { - this.template('config.rb', 'config.rb'); + this.template('common/config.rb', 'config.rb'); }; Generator.prototype.writeIndexWithRequirejs = function writeIndexWithRequirejs() { - this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'index.html')); + this.indexFile = this.readFileAsString(path.join(this.sourceRoot(), 'common/index.html')); this.indexFile = this.engine(this.indexFile, this); this.indexFile = this.appendScripts(this.indexFile, 'js/main.js', [ @@ -97,27 +95,23 @@ Generator.prototype.setupEnv = function setupEnv() { this.mkdir('app/sass'); this.mkdir('app/images'); this.mkdir('app/jade'); - this.template('app/404.html'); - this.template('app/favicon.ico'); - this.template('app/robots.txt'); - this.copy('app/htaccess', 'app/.htaccess'); + this.template('common/app/404.html', 'app/404.html'); + this.template('common/app/favicon.ico', 'app/favicon.ico'); + this.template('common/app/robots.txt', 'app/robots.txt'); + this.copy('common/app/htaccess', 'app/.htaccess'); this.write('app/index.html', this.indexFile); }; Generator.prototype.mainStylesheet = function mainStylesheet() { - this.directory('mixins', 'app/sass/mixins'); - this.template('_normalize.scss', 'app/sass/_normalize.scss'); - this.template('_base.scss', 'app/sass/_base.scss'); - this.template('main.scss', 'app/sass/main.scss'); + this.directory('sass/mixins', 'app/sass/mixins'); + this.template('sass/_normalize.scss', 'app/sass/_normalize.scss'); + this.template('sass/_base.scss', 'app/sass/_base.scss'); + this.template('sass/main.scss', 'app/sass/main.scss'); }; Generator.prototype.mainJs = function mainJs() { - this.sourceRoot(path.join(__dirname, '../templates')); - - var mainJsFile = this.engine(this.read('main.js'), this); - - this.write('app/js/main.js', mainJsFile); - this.template('../templates/app.js', 'app/js/app.js'); - this.template('../templates/router.js', 'app/js/router/router.js'); - this.template('../templates/abstract.js', 'app/js/views/abstract.js'); + this.template('js/main.js', 'app/js/main.js'); + this.template('js/app.js', 'app/js/app.js'); + this.template('js/router-default.js', 'app/js/router/router.js'); + this.template('js/abstract.js', 'app/js/views/abstract.js'); }; \ No newline at end of file diff --git a/collection/index.js b/collection/index.js index 4c5637a..e87cc19 100644 --- a/collection/index.js +++ b/collection/index.js @@ -1,40 +1,22 @@ -/*jshint latedef:false */ -var path = require('path'); -var util = require('util'); -var yeoman = require('yeoman-generator'); -var scriptBase = require('../script-base'); +'use strict'; -module.exports = Generator; +var path = require('path'), + util = require('util'), + yeoman = require('yeoman-generator'), + scriptBase = require('../script-base'); -function Generator() { + +var Generator = module.exports = function Generator() { + yeoman.generators.NamedBase.apply(this, arguments); scriptBase.apply(this, arguments); this.sourceRoot(path.join(__dirname, '../templates')); +}; - // required for collection.js template which uses `appname` -} - -util.inherits(Generator, scriptBase); - -Generator.prototype.createControllerFiles = function createControllerFiles() { - var ext = '.js', - directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : ''; +util.inherits(Generator, yeoman.generators.NamedBase, scriptBase); - var destFile = path.join('app/js/collections' + directory + '/', this.name + ext); - var template = [ - 'define([', - ' \'underscore\',', - ' \'backbone\',', - ' \'models/' + this.name + '\'', - '], function(_, Backbone, ' + this._.classify(this.name) + 'Model' + ') {', - ' \'use strict\';', - '', - ' var ' + this._.classify(this.name) + 'Collection = Backbone.Collection.extend({', - ' ' + 'model: ' + this._.classify(this.name) + 'Model', - ' });', - '', - ' return ' + this._.classify(this.name) + 'Collection;', - '});' - ].join('\n'); +Generator.prototype.createViewFiles = function createViewFiles() { + this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : ''; + this.pathToModel = (typeof this.arguments[2] !== 'undefined') ? this.arguments[2] + '/' : ''; - this.write(destFile, template); -}; + this.copy('js/collection.js', 'app/js/collections' + this.dirPath + '/' + this.name + '.js'); +}; \ No newline at end of file diff --git a/all/index.js b/common/index.js similarity index 100% rename from all/index.js rename to common/index.js diff --git a/model/index.js b/model/index.js index dd6fe09..0fc178c 100644 --- a/model/index.js +++ b/model/index.js @@ -1,53 +1,21 @@ -/*jshint latedef:false */ -var path = require('path'); -var util = require('util'); -var yeoman = require('yeoman-generator'); -var scriptBase = require('../script-base'); +'use strict'; -module.exports = Generator; +var path = require('path'), + util = require('util'), + yeoman = require('yeoman-generator'), + scriptBase = require('../script-base'); -function Generator() { - scriptBase.apply(this, arguments); - - // XXX default and banner to be implemented - this.argument('attributes', { - type: Array, - defaults: [], - banner: 'field[:type] field[:type]' - }); - - // parse back the attributes provided, build an array of attr - this.attrs = this.attributes.map(function (attr) { - var parts = attr.split(':'); - return { - name: parts[0], - type: parts[1] || 'string' - }; - }); - -} -util.inherits(Generator, scriptBase); +var Generator = module.exports = function Generator() { + yeoman.generators.NamedBase.apply(this, arguments); + scriptBase.apply(this, arguments); + this.sourceRoot(path.join(__dirname, '../templates')); +}; -Generator.prototype.createModelFiles = function createModelFiles() { - var ext = '.js', - directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : ''; +util.inherits(Generator, yeoman.generators.NamedBase, scriptBase); - var destFile = path.join('app/js/models' + directory + '/', this.name + ext); - var template = [ - 'define([', - ' \'underscore\',', - ' \'backbone\'', - '], function(_, Backbone) {', - ' \'use strict\';', - '', - ' var ' + this._.classify(this.name) + 'Model = Backbone.Model.extend({', - ' defaults: {}', - ' });', - '', - ' return ' + this._.classify(this.name) + 'Model;', - '});' - ].join('\n'); +Generator.prototype.createViewFiles = function createViewFiles() { + this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : ''; - this.write(destFile, template); -}; + this.copy('js/model.js', 'app/js/models' + this.dirPath + '/' + this.name + '.js'); +}; \ No newline at end of file diff --git a/router/index.js b/router/index.js index b91b0fd..e9656f7 100644 --- a/router/index.js +++ b/router/index.js @@ -1,41 +1,21 @@ -/*jshint latedef:false */ +'use strict'; + var path = require('path'), - util = require('util'), - yeoman = require('yeoman-generator'), - scriptBase = require('../script-base'); + util = require('util'), + yeoman = require('yeoman-generator'), + scriptBase = require('../script-base'); -module.exports = Generator; -function Generator() { +var Generator = module.exports = function Generator() { + yeoman.generators.NamedBase.apply(this, arguments); scriptBase.apply(this, arguments); this.sourceRoot(path.join(__dirname, '../templates')); +}; - // required for router.js template which uses `appname` -} - -util.inherits(Generator, scriptBase); - -Generator.prototype.createControllerFiles = function createControllerFiles() { - var ext = '.js', - directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : ''; - - var destFile = path.join('app/js/router' + directory + '/', this.name + ext); - var template = [ - 'define([', - ' \'jquery\',', - ' \'backbone\'', - '], function($, Backbone) {', - ' \'use strict\';', - '', - ' var ' + this._.classify(this.name) + 'Router = Backbone.Router.extend({', - ' routes: {', - ' }', - ' });', - '', - ' return ' + this._.classify(this.name) + 'Router;', - '});' - ].join('\n'); +util.inherits(Generator, yeoman.generators.NamedBase, scriptBase); - this.write(destFile, template); +Generator.prototype.createViewFiles = function createViewFiles() { + this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : ''; -}; + this.copy('js/router-new.js', 'app/js/router' + this.dirPath + '/' + this.name + '.js'); +}; \ No newline at end of file diff --git a/script-base.js b/script-base.js index 8955483..cbb1e9d 100644 --- a/script-base.js +++ b/script-base.js @@ -21,8 +21,6 @@ var Generator = module.exports = function Generator() { var sourceRoot = '/templates/'; this.scriptSuffix = '.js'; - this.dirPath = arguments[0][1]; - this.sourceRoot(path.join(__dirname, sourceRoot)); }; diff --git a/app/templates/Gruntfile.js b/templates/common/Gruntfile.js similarity index 100% rename from app/templates/Gruntfile.js rename to templates/common/Gruntfile.js diff --git a/app/templates/_bower.json b/templates/common/_bower.json similarity index 91% rename from app/templates/_bower.json rename to templates/common/_bower.json index b3cfedf..1eca9b8 100644 --- a/app/templates/_bower.json +++ b/templates/common/_bower.json @@ -8,7 +8,7 @@ "requirejs": "~2.1.9", "requirejs-text": "~2.0.10", "modernizr": "~2.6.2", - "tweenMax": "~1.11.0" + "greensock": "~1.11.1" }, "devDependencies": {} } diff --git a/app/templates/_package.json b/templates/common/_package.json similarity index 100% rename from app/templates/_package.json rename to templates/common/_package.json diff --git a/app/templates/app/404.html b/templates/common/app/404.html similarity index 100% rename from app/templates/app/404.html rename to templates/common/app/404.html diff --git a/app/templates/app/favicon.ico b/templates/common/app/favicon.ico similarity index 100% rename from app/templates/app/favicon.ico rename to templates/common/app/favicon.ico diff --git a/app/templates/app/htaccess b/templates/common/app/htaccess similarity index 100% rename from app/templates/app/htaccess rename to templates/common/app/htaccess diff --git a/app/templates/app/robots.txt b/templates/common/app/robots.txt similarity index 100% rename from app/templates/app/robots.txt rename to templates/common/app/robots.txt diff --git a/app/templates/bowerrc b/templates/common/bowerrc similarity index 100% rename from app/templates/bowerrc rename to templates/common/bowerrc diff --git a/app/templates/config.rb b/templates/common/config.rb similarity index 100% rename from app/templates/config.rb rename to templates/common/config.rb diff --git a/app/templates/gitattributes b/templates/common/gitattributes similarity index 100% rename from app/templates/gitattributes rename to templates/common/gitattributes diff --git a/app/templates/gitignore b/templates/common/gitignore similarity index 100% rename from app/templates/gitignore rename to templates/common/gitignore diff --git a/app/templates/index.html b/templates/common/index.html similarity index 100% rename from app/templates/index.html rename to templates/common/index.html diff --git a/templates/abstract.js b/templates/js/abstract.js similarity index 100% rename from templates/abstract.js rename to templates/js/abstract.js diff --git a/templates/app.js b/templates/js/app.js similarity index 100% rename from templates/app.js rename to templates/js/app.js diff --git a/templates/js/collection.js b/templates/js/collection.js new file mode 100644 index 0000000..816bcab --- /dev/null +++ b/templates/js/collection.js @@ -0,0 +1,13 @@ +define([ + 'underscore', + 'backbone', + 'models/<%= pathToModel %><%= name %>' +], function(_, Backbone, <%= _.classify(name) %>Model) { + 'use strict'; + + var <%= _.classify(name) %>Collection = Backbone.Collection.extend({ + model: <%= _.classify(name) %>Model + }); + + return <%= _.classify(name) %>Collection; +}); \ No newline at end of file diff --git a/templates/main.js b/templates/js/main.js similarity index 89% rename from templates/main.js rename to templates/js/main.js index c56fc15..1235bd6 100644 --- a/templates/main.js +++ b/templates/js/main.js @@ -20,7 +20,7 @@ require.config({ jquery: '../js/libs/bower/jquery/jquery', backbone: '../js/libs/bower/backbone/backbone', underscore: '../js/libs/bower/underscore/underscore', - tweenmax: '../js/libs/bower/tweenMax/src/minified/TweenMax.min' + tweenmax: '../js/libs/bower/greensock/src/minified/TweenMax.min' } }); diff --git a/templates/js/model.js b/templates/js/model.js new file mode 100644 index 0000000..ea8d0f4 --- /dev/null +++ b/templates/js/model.js @@ -0,0 +1,12 @@ +define([ + 'underscore', + 'backbone' +], function(_, Backbone) { + 'use strict'; + + var <%= _.classify(name) %>Model = Backbone.Model.extend({ + defaults: {} + }); + + return <%= _.classify(name) %>Model; +}); \ No newline at end of file diff --git a/templates/router.js b/templates/js/router-default.js similarity index 100% rename from templates/router.js rename to templates/js/router-default.js diff --git a/templates/js/router-new.js b/templates/js/router-new.js new file mode 100644 index 0000000..d0d8eb5 --- /dev/null +++ b/templates/js/router-new.js @@ -0,0 +1,13 @@ +define([ + 'jquery', + 'backbone' +], function($, Backbone) { + 'use strict'; + + var <%= _.classify(name) %>Router = Backbone.Router.extend({ + routes: { + } + }); + + return <%= _.classify(name) %>Router; +}); \ No newline at end of file diff --git a/templates/js/view.ejs b/templates/js/view.ejs new file mode 100644 index 0000000..bece7db --- /dev/null +++ b/templates/js/view.ejs @@ -0,0 +1 @@ +
View <%= _.classify(name) %>View created.
\ No newline at end of file diff --git a/templates/js/view.js b/templates/js/view.js new file mode 100644 index 0000000..373f434 --- /dev/null +++ b/templates/js/view.js @@ -0,0 +1,41 @@ +define([ + 'jquery', + 'underscore', + 'backbone', + 'views/abstract', + 'templates' +], function($, _, Backbone, AbstractView, JST) { + 'use strict'; + + var <%= _.classify(name) %>View = AbstractView.extend({ + template: JST['<%= jstPath %>'], + + events: {}, + + /* ----------------------------------------------------------------------------- *\ + Public Methods + \* ----------------------------------------------------------------------------- */ + + /** + @method initialize + + @return {null} + **/ + initialize: function(opts) { + AbstractView.prototype.initialize.apply(this, arguments); + }, + + /** + @method render + + @return {<%= _.classify(name) %>View} + **/ + render: function() { + this.$el.html(this.template()); + + return this; + } + }); + + return <%= _.classify(name) %>View; +}); \ No newline at end of file diff --git a/app/templates/_base.scss b/templates/sass/_base.scss similarity index 100% rename from app/templates/_base.scss rename to templates/sass/_base.scss diff --git a/app/templates/_normalize.scss b/templates/sass/_normalize.scss similarity index 100% rename from app/templates/_normalize.scss rename to templates/sass/_normalize.scss diff --git a/app/templates/main.scss b/templates/sass/main.scss similarity index 100% rename from app/templates/main.scss rename to templates/sass/main.scss diff --git a/app/templates/mixins/_box-shadow.scss b/templates/sass/mixins/_box-shadow.scss similarity index 100% rename from app/templates/mixins/_box-shadow.scss rename to templates/sass/mixins/_box-shadow.scss diff --git a/app/templates/mixins/_font-face.scss b/templates/sass/mixins/_font-face.scss similarity index 100% rename from app/templates/mixins/_font-face.scss rename to templates/sass/mixins/_font-face.scss diff --git a/app/templates/mixins/_font-family.scss b/templates/sass/mixins/_font-family.scss similarity index 100% rename from app/templates/mixins/_font-family.scss rename to templates/sass/mixins/_font-family.scss diff --git a/app/templates/mixins/_gradient.scss b/templates/sass/mixins/_gradient.scss similarity index 100% rename from app/templates/mixins/_gradient.scss rename to templates/sass/mixins/_gradient.scss diff --git a/app/templates/mixins/_opacity.scss b/templates/sass/mixins/_opacity.scss similarity index 100% rename from app/templates/mixins/_opacity.scss rename to templates/sass/mixins/_opacity.scss diff --git a/app/templates/mixins/_rem.scss b/templates/sass/mixins/_rem.scss similarity index 100% rename from app/templates/mixins/_rem.scss rename to templates/sass/mixins/_rem.scss diff --git a/app/templates/mixins/_rounded.scss b/templates/sass/mixins/_rounded.scss similarity index 100% rename from app/templates/mixins/_rounded.scss rename to templates/sass/mixins/_rounded.scss diff --git a/templates/view.ejs b/templates/view.ejs deleted file mode 100644 index cd9972c..0000000 --- a/templates/view.ejs +++ /dev/null @@ -1 +0,0 @@ -Your content here.
\ No newline at end of file diff --git a/view/index.js b/view/index.js index 34d42f1..dfd0dd2 100644 --- a/view/index.js +++ b/view/index.js @@ -1,71 +1,23 @@ -/*jshint latedef:false */ +'use strict'; + var path = require('path'), - util = require('util'), - yeoman = require('yeoman-generator'), - scriptBase = require('../script-base'); + util = require('util'), + yeoman = require('yeoman-generator'), + scriptBase = require('../script-base'); -module.exports = Generator; -function Generator() { +var Generator = module.exports = function Generator() { + yeoman.generators.NamedBase.apply(this, arguments); scriptBase.apply(this, arguments); this.sourceRoot(path.join(__dirname, '../templates')); -} +}; -util.inherits(Generator, scriptBase); +util.inherits(Generator, yeoman.generators.NamedBase, scriptBase); Generator.prototype.createViewFiles = function createViewFiles() { - var ext = '.js', - templateExt = '.ejs', - directory = (typeof this.dirPath !== 'undefined') ? '/' + this.dirPath : ''; - - this.jst_path = 'app/js/templates' + directory + '/' + this.name + templateExt; - var destFile = path.join('app/js/views' + directory + '/', this.name + ext); + this.dirPath = (typeof this.arguments[1] !== 'undefined') ? '/' + this.arguments[1] : ''; + this.jstPath = 'app/js/templates' + this.dirPath + '/' + this.name + '.ejs'; - this.template('view.ejs', this.jst_path); - - var template = [ - 'define([', - ' \'jquery\',', - ' \'underscore\',', - ' \'backbone\',', - ' \'views/abstract\',', - ' \'templates\'', - '], function($, _, Backbone, AbstractView, JST) {', - ' \'use strict\';', - '', - ' var ' + this._.classify(this.name) + 'View = AbstractView.extend({', - ' ' + 'template: JST[\'' + this.jst_path + '\'],', - '', - ' events: {},', - '', - ' /* ----------------------------------------------------------------------------- *\\', - ' Public Methods', - ' \\* ----------------------------------------------------------------------------- */', - '', - ' /**', - ' @method initialize', - '', - ' @return {null}', - ' **/', - ' initialize: function(opts) {', - ' AbstractView.prototype.initialize.apply(this, arguments);', - ' },', - '', - ' /**', - ' @method render', - '', - ' @return {' + this._.classify(this.name) + 'View}', - ' **/', - ' render: function() {', - ' this.$el.html(this.template());', - '', - ' return this;', - ' }', - ' });', - '', - ' return ' + this._.classify(this.name) + 'View;', - '});' - ].join('\n'); - - this.write(destFile, template); -}; + this.template('js/view.ejs', this.jstPath); + this.copy('js/view.js', 'app/js/views' + this.dirPath + '/' + this.name + '.js'); +}; \ No newline at end of file