Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
Using composeWith instead of hookFor
Browse files Browse the repository at this point in the history
  • Loading branch information
revathskumar committed Aug 10, 2014
1 parent de3f159 commit 780b87d
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 173 deletions.
57 changes: 22 additions & 35 deletions all/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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('..');
Expand All @@ -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});
}
});

Expand Down
26 changes: 14 additions & 12 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
});
}
}
},

Expand Down
26 changes: 10 additions & 16 deletions collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
28 changes: 10 additions & 18 deletions model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
});
Expand Down
27 changes: 10 additions & 17 deletions router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
});
Expand Down
22 changes: 16 additions & 6 deletions script-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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';

Expand All @@ -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')
});
}
}
});

Expand Down
18 changes: 8 additions & 10 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Loading

0 comments on commit 780b87d

Please sign in to comment.