From 780b87d12ace08cd44e72dc0d5ebec709d851f22 Mon Sep 17 00:00:00 2001 From: Revath S Kumar Date: Wed, 6 Aug 2014 00:40:39 +0530 Subject: [PATCH] Using composeWith instead of hookFor --- all/index.js | 57 ++++++++++++++++------------------------ app/index.js | 26 ++++++++++--------- collection/index.js | 26 +++++++------------ model/index.js | 28 +++++++------------- router/index.js | 27 +++++++------------ script-base.js | 22 +++++++++++----- test/helper.js | 18 ++++++------- test/test-apppath.js | 62 ++++++++++++++++++++++---------------------- view/index.js | 49 +++++++++++++++------------------- 9 files changed, 142 insertions(+), 173 deletions(-) diff --git a/all/index.js b/all/index.js index dc0af0d..a125283 100644 --- a/all/index.js +++ b/all/index.js @@ -17,22 +17,22 @@ var BackboneGenerator = yeoman.generators.Base.extend({ this.option('coffee'); - var args = [this.appname]; + this.args = [this.appname]; this.option('coffee'); this.env.options.appPath = this.options.appPath || 'app'; this.config.set('appPath', this.env.options.appPath); - args = ['application']; + this.args = ['application']; if (this.options.coffee) { - args.push('--coffee'); + this.args.push('--coffee'); } this.option('requirejs'); if (this.options.requirejs) { - args.push('--requirejs'); + this.args.push('--requirejs'); } if (this.options['template-framework']) { @@ -41,32 +41,6 @@ var BackboneGenerator = yeoman.generators.Base.extend({ this.testFramework = this.options['test-framework'] || 'mocha'; - // the api to hookFor and pass arguments may vary a bit. - this.hookFor('backbone:app', { - args: args - }); - this.hookFor('backbone:router', { - args: args - }); - this.hookFor('backbone:view', { - args: args - }); - this.hookFor('backbone:model', { - args: args - }); - this.hookFor('backbone:collection', { - args: args - }); - - this.hookFor(this.testFramework, { - as: 'app', - options: { - options: { - 'skip-install': this.options['skip-install'] - } - } - }); - this.on('end', function () { if (/^.*test$/.test(process.cwd())) { process.chdir('..'); @@ -75,11 +49,24 @@ var BackboneGenerator = yeoman.generators.Base.extend({ }); }, - createDirLayout: function () { - this.dirs.forEach(function (dir) { - this.log.create('app/scripts/' + dir); - this.mkdir(path.join('app/scripts', dir)); - }.bind(this)); + writing: { + createDirLayout: function () { + this.dirs.forEach(function (dir) { + this.log.create('app/scripts/' + dir); + this.mkdir(path.join('app/scripts', dir)); + }.bind(this)); + }, + + composeSubGenerators: function () { + this.composeWith('backbone:app', {arguments: this.args}); + } + }, + + install: function () { + this.composeWith('backbone:router', {arguments: this.args}); + this.composeWith('backbone:view', {arguments: this.args}); + this.composeWith('backbone:model', {arguments: this.args}); + this.composeWith('backbone:collection', {arguments: this.args}); } }); diff --git a/app/index.js b/app/index.js index cf2236d..9cb92ce 100644 --- a/app/index.js +++ b/app/index.js @@ -33,8 +33,10 @@ var BackboneGenerator = yeoman.generators.Base.extend({ defaults: 'mocha' }); - this.testFramework = this.options['test-framework']; - this.templateFramework = this.options['template-framework']; + this.option('skip-install', { + desc: 'Skip the bower and node installations', + defaults: false + }); this.argument('app_name', { type: String, required: false }); this.appname = this.app_name || this.appname; @@ -46,16 +48,6 @@ var BackboneGenerator = yeoman.generators.Base.extend({ this.testFramework = this.options['test-framework'] || 'mocha'; this.templateFramework = this.options['template-framework'] || 'lodash'; - if (['backbone:app', 'backbone'].indexOf(this.options.namespace) >= 0) { - this.hookFor(this.testFramework, { - as: 'app', - options: { - 'skip-install': this.options['skip-install'], - 'ui': this.options.ui - } - }); - } - this.config.defaults({ appName: this.appname, ui: this.options.ui, @@ -248,6 +240,16 @@ var BackboneGenerator = yeoman.generators.Base.extend({ return; } this._writeTemplate('app', this.env.options.appPath + '/scripts/main'); + }, + + composeTest: function () { + if (['backbone:app', 'backbone'].indexOf(this.options.namespace) >= 0) { + this.composeWith(this.testFramework, { + 'skip-install': this.options['skip-install'], + 'ui': this.options.ui, + 'skipMessage': true, + }); + } } }, diff --git a/collection/index.js b/collection/index.js index 8cfb49a..f07de97 100644 --- a/collection/index.js +++ b/collection/index.js @@ -10,28 +10,22 @@ var CollectionGenerator = scriptBase.extend({ var dirPath = this.options.coffee ? '../templates/coffeescript/' : '../templates'; this.sourceRoot(path.join(__dirname, dirPath)); + }, - var testOptions = { - as: 'collection', - args: [this.name], - options: { - coffee: this.config.get('coffee'), - ui: this.config.get('ui') - } - }; + writing: { + createControllerFiles: function () { + this._writeTemplate('collection', path.join(this.env.options.appPath + '/scripts/collections', this.name)); - if (this.generateTests()) { - this.hookFor('backbone-mocha', testOptions); + if (!this.options.requirejs) { + this._addScriptToIndex('collections/' + this.name); + } + }, + composeTest: function () { + this._generateTest('collection'); } }, - createControllerFiles: function () { - this.writeTemplate('collection', path.join(this.env.options.appPath + '/scripts/collections', this.name)); - if (!this.options.requirejs) { - this.addScriptToIndex('collections/' + this.name); - } - } }); module.exports = CollectionGenerator; diff --git a/model/index.js b/model/index.js index 697e3b9..6315732 100644 --- a/model/index.js +++ b/model/index.js @@ -23,27 +23,19 @@ var ModelGenerator = scriptBase.extend({ type: parts[1] || 'string' }; }); - - var testOptions = { - as: 'model', - args: [this.name], - options: { - coffee: this.config.get('coffee'), - ui: this.config.get('ui') - } - }; - - if (this.generateTests()) { - this.hookFor('backbone-mocha', testOptions); - } - }, - createModelFiles: function () { - this.writeTemplate('model', path.join(this.env.options.appPath + '/scripts/models', this.name)); + writing: { + createModelFiles: function () { + this._writeTemplate('model', path.join(this.env.options.appPath + '/scripts/models', this.name)); + + if (!this.options.requirejs) { + this._addScriptToIndex('models/' + this.name); + } + }, - if (!this.options.requirejs) { - this.addScriptToIndex('models/' + this.name); + composeTest: function () { + this._generateTest('model'); } } }); diff --git a/router/index.js b/router/index.js index a8a91e2..057d1f1 100644 --- a/router/index.js +++ b/router/index.js @@ -9,26 +9,19 @@ var RouterGenerator = scriptBase.extend({ scriptBase.apply(this, arguments); var dirPath = this.options.coffee ? '../templates/coffeescript/' : '../templates'; this.sourceRoot(path.join(__dirname, dirPath)); - - var testOptions = { - as: 'router', - args: [this.name], - options: { - coffee: this.config.get('coffee'), - ui: this.config.get('ui') - } - }; - - if (this.generateTests()) { - this.hookFor('backbone-mocha', testOptions); - } }, - createControllerFiles: function () { - this.writeTemplate('router', path.join(this.env.options.appPath + '/scripts/routes', this.name)); + writing: { + routerFiles: function () { + this._writeTemplate('router', path.join(this.env.options.appPath + '/scripts/routes', this.name)); + + if (!this.options.requirejs) { + this._addScriptToIndex('routes/' + this.name); + } + }, - if (!this.options.requirejs) { - this.addScriptToIndex('routes/' + this.name); + composeTest: function () { + this._generateTest('router'); } } }); diff --git a/script-base.js b/script-base.js index 9867233..4f888a4 100644 --- a/script-base.js +++ b/script-base.js @@ -28,7 +28,8 @@ var ScriptBase = yeoman.generators.NamedBase.extend({ this._.mixin({ 'classify': backboneUtils.classify }); }, - addScriptToIndex: function (script) { + + _addScriptToIndex: function (script) { try { var appPath = this.env.options.appPath; var fullPath = path.join(appPath, 'index.html'); @@ -41,11 +42,11 @@ var ScriptBase = yeoman.generators.NamedBase.extend({ ] }); } catch (e) { - this.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow); + this.log('\n Unable to find ' + fullPath + '. Reference to ' + script + '.js ' + 'not added.\n'); } }, - setupSourceRootAndSuffix: function () { + _setupSourceRootAndSuffix: function () { var sourceRoot = '/templates'; this.scriptSuffix = '.js'; @@ -61,14 +62,23 @@ var ScriptBase = yeoman.generators.NamedBase.extend({ this.sourceRoot(path.join(__dirname, sourceRoot)); }, - writeTemplate: function (source, destination, data) { - this.setupSourceRootAndSuffix(); + _writeTemplate: function (source, destination, data) { + this._setupSourceRootAndSuffix(); var ext = this.scriptSuffix; this.template(source + ext, destination + ext, data); }, - generateTests: function () { + _canGenerateTests: function () { return this.config.get('testFramework') === 'mocha' && !this.config.get('includeRequireJS'); + }, + + _generateTest: function (type) { + if (this._canGenerateTests()) { + this.composeWith('backbone-mocha:' + type, { arguments: [this.name] }, { + coffee: this.config.get('coffee'), + ui: this.config.get('ui') + }); + } } }); diff --git a/test/helper.js b/test/helper.js index c952f7f..bb4c58c 100644 --- a/test/helper.js +++ b/test/helper.js @@ -1,17 +1,15 @@ var yeoman = require('yeoman-generator'); var helpers = yeoman.test; +var path = require('path'); exports.createSubGenerator = function (type, asserts) { - var subGenerator = helpers.createGenerator('backbone:' + type, [ - '../../' + type, [ - helpers.createDummyGenerator(), - 'backbone-mocha:' + type - ] - ], ['foo']); - - subGenerator.run([], function () { - asserts(); - }); + var deps = [ + [helpers.createDummyGenerator(), 'backbone-mocha:' + type] + ]; + helpers.run(path.join(__dirname, '../' + type)) + .withArguments(['foo']) + .withGenerators(deps) + .on('end', asserts); }; exports.createAppGenerator = function (args, options) { diff --git a/test/test-apppath.js b/test/test-apppath.js index 580bcdc..cf04602 100644 --- a/test/test-apppath.js +++ b/test/test-apppath.js @@ -9,29 +9,28 @@ var test = require('./helper.js'); describe('backbone generator with appPath option', function () { beforeEach(function (done) { - helpers.testDirectory(path.join(__dirname, './temp'), function (err) { - if (err) { - return done(err); - } - this.backbone = {}; - this.backbone.app = test.createAppGenerator(['temp'], {appPath: 'public'}); + var deps = [ + [helpers.createDummyGenerator(), 'mocha:app'] + ]; + this.backbone = {}; + this.backbone.app = helpers.run(path.join(__dirname, '../app')) + .inDir(path.join(__dirname, 'temp'), function () { + var out = [ + '{', + ' "generator-backbone": {', + ' "appPath": "public",', + ' "appName": "Temp"', + ' }', + '}' + ]; + fs.writeFileSync('.yo-rc.json', out.join('\n')); + }) + .withArguments(['temp']) + .withOptions({'skip-install': true, appPath: 'public'}) + .withPrompt({features: ['compassBootstrap']}) + .withGenerators(deps); - helpers.mockPrompt(this.backbone.app, { - features: ['compassBootstrap'] - }); - - var out = [ - '{', - ' "generator-backbone": {', - ' "appPath": "public",', - ' "appName": "Temp"', - ' }', - '}' - ]; - fs.writeFileSync('.yo-rc.json', out.join('\n')); - - done(); - }.bind(this)); + done(); }); describe('create expected files', function () { @@ -58,17 +57,18 @@ describe('backbone generator with appPath option', function () { 'public/styles/main.scss' ]; - this.backbone.app.run({}, function () { - assert.file(expected); - assert.fileContent(expectedContent); - done(); - }); + this.backbone.app + .on('end', function () { + assert.file(expected); + assert.fileContent(expectedContent); + done(); + }); }); }); describe('creates model', function () { it('without failure', function (done) { - this.backbone.app.run({}, function () { + this.backbone.app.on('end', function () { test.createSubGenerator('model', function () { assert.fileContent( 'public/scripts/models/foo.js', /Models.Foo = Backbone.Model.extend\(\{/ @@ -81,7 +81,7 @@ describe('backbone generator with appPath option', function () { describe('creates collection', function () { it('without failure', function (done) { - this.backbone.app.run({}, function () { + this.backbone.app.on('end', function () { test.createSubGenerator('collection', function () { assert.fileContent( 'public/scripts/collections/foo.js', /Collections.Foo = Backbone.Collection.extend\(\{/ @@ -94,7 +94,7 @@ describe('backbone generator with appPath option', function () { describe('creates router', function () { it('without failure', function (done) { - this.backbone.app.run({}, function () { + this.backbone.app.on('end', function () { test.createSubGenerator('router', function () { assert.fileContent( 'public/scripts/routes/foo.js', /Routers.Foo = Backbone.Router.extend\(\{/ @@ -108,7 +108,7 @@ describe('backbone generator with appPath option', function () { describe('creates view', function () { it('without failure', function (done) { - this.backbone.app.run({}, function () { + this.backbone.app.on('end', function () { test.createSubGenerator('view', function () { assert.fileContent( 'public/scripts/views/foo.js', /Views.Foo = Backbone.View.extend\(\{(.|\n)*public\/scripts\/templates\/foo.ejs/ diff --git a/view/index.js b/view/index.js index 3977201..47fbabe 100644 --- a/view/index.js +++ b/view/index.js @@ -10,40 +10,33 @@ var ViewGenerator = scriptBase.extend({ var dirPath = this.options.coffee ? '../templates/coffeescript/' : '../templates'; this.sourceRoot(path.join(__dirname, dirPath)); + }, - var testOptions = { - as: 'view', - args: [this.name], - options: { - coffee: this.config.get('coffee'), - ui: this.config.get('ui') + writing: { + createViewFiles: function () { + var templateFramework = this.config.get('templateFramework') || 'lodash'; + var templateExt = '.ejs'; + if (templateFramework === 'mustache') { + templateExt = '-template.mustache'; + } else if (templateFramework === 'handlebars') { + templateExt = '.hbs'; } - }; - - if (this.generateTests()) { - this.hookFor('backbone-mocha', testOptions); - } - }, + this.jst_path = this.env.options.appPath + '/scripts/templates/' + this.name + templateExt; - createViewFiles: function () { - var templateFramework = this.config.get('templateFramework') || 'lodash'; - var templateExt = '.ejs'; - if (templateFramework === 'mustache') { - templateExt = '-template.mustache'; - } else if (templateFramework === 'handlebars') { - templateExt = '.hbs'; - } - this.jst_path = this.env.options.appPath + '/scripts/templates/' + this.name + templateExt; + this.template('view.ejs', this.jst_path); + if (templateFramework === 'mustache') { + this.jst_path = this.name + '-template'; + } - this.template('view.ejs', this.jst_path); - if (templateFramework === 'mustache') { - this.jst_path = this.name + '-template'; - } + this._writeTemplate('view', path.join(this.env.options.appPath + '/scripts/views', this.name)); - this.writeTemplate('view', path.join(this.env.options.appPath + '/scripts/views', this.name)); + if (!this.options.requirejs) { + this._addScriptToIndex('views/' + this.name); + } + }, - if (!this.options.requirejs) { - this.addScriptToIndex('views/' + this.name); + composeTest: function () { + this._generateTest('view'); } } });