From 50d1b7297569f83b6e0e91a3e983dd14e8f830b8 Mon Sep 17 00:00:00 2001 From: Sergey Trotsyuk Date: Sun, 26 Oct 2014 12:41:07 +0200 Subject: [PATCH] Added path and capitalize support for component and mixin generators --- lib/generators/component/index.js | 13 +++- lib/generators/generator-utils.js | 24 ++++++ lib/generators/mixin/index.js | 12 ++- lib/templates/component.js | 8 +- lib/templates/mixin.js | 9 ++- test/test.js | 118 +++++++++++++++++++++++++++++- 6 files changed, 170 insertions(+), 14 deletions(-) create mode 100644 lib/generators/generator-utils.js diff --git a/lib/generators/component/index.js b/lib/generators/component/index.js index deca2d1..ba7f5cf 100644 --- a/lib/generators/component/index.js +++ b/lib/generators/component/index.js @@ -6,6 +6,8 @@ var path = require('path'); var util = require('util'); var yeoman = require('yeoman-generator'); +var generatorUtils = require('../generator-utils'); + /** * Module exports. */ @@ -22,6 +24,8 @@ function Generator() { yeoman.generators.NamedBase.apply(this, arguments); this.sourceRoot(path.join(__dirname, '../../templates/')); + + this.option('capitalize'); } util.inherits(Generator, yeoman.generators.Base); @@ -34,9 +38,12 @@ util.inherits(Generator, yeoman.generators.Base); Generator.prototype.createComponentFiles = function createComponentFiles() { this.name = this.name || 'my_component'; - this.template('component.js', 'app/js/component/' + this.name + '.js'); - this.template('spec.js', 'test/spec/component/' + this.name + '.spec.js', { - 'requirePath': 'component/' + this.name, + + generatorUtils.mixinFullPath(this); + + this.template('component.js', 'app/js/component/' + this.fullPath + '.js'); + this.template('spec.js', 'test/spec/component/' + this.fullPath + '.spec.js', { + 'requirePath': 'component/' + this.fullPath, 'type': 'component' }); }; diff --git a/lib/generators/generator-utils.js b/lib/generators/generator-utils.js new file mode 100644 index 0000000..c74113b --- /dev/null +++ b/lib/generators/generator-utils.js @@ -0,0 +1,24 @@ +/** + * Adds path support + * + * @api public + */ + +function mixinFullPath (generator, fileNamePrefix) { + fileNamePrefix = fileNamePrefix || ''; + + var matches = generator.name.match(new RegExp('^(.*?)([^/]+)$')); + var path = matches[1]; + var fileName = matches[2]; + + generator.name = fileNamePrefix + fileName; + generator.fullPath = path + generator.name; +} + +/** + * Module exports. + */ + +module.exports = { + mixinFullPath: mixinFullPath +}; \ No newline at end of file diff --git a/lib/generators/mixin/index.js b/lib/generators/mixin/index.js index cfd7bc8..c390075 100644 --- a/lib/generators/mixin/index.js +++ b/lib/generators/mixin/index.js @@ -5,6 +5,7 @@ var path = require('path'); var util = require('util'); var yeoman = require('yeoman-generator'); +var generatorUtils = require('../generator-utils'); /** * Module exports. @@ -22,6 +23,8 @@ function Generator() { yeoman.generators.NamedBase.apply(this, arguments); this.sourceRoot(path.join(__dirname, '../../templates/')); + + this.option('capitalize'); } util.inherits(Generator, yeoman.generators.Base); @@ -34,9 +37,12 @@ util.inherits(Generator, yeoman.generators.Base); Generator.prototype.createMixinFiles = function createMixinFiles() { this.name = this.name || 'my_component'; - this.template('mixin.js', 'app/js/component/with_' + this.name + '.js'); - this.template('spec.js', 'test/spec/component/with_' + this.name + '.spec.js', { - 'requirePath': 'component/with_' + this.name, + + generatorUtils.mixinFullPath(this, 'with_'); + + this.template('mixin.js', 'app/js/component/' + this.fullPath + '.js'); + this.template('spec.js', 'test/spec/component/' + this.fullPath + '.spec.js', { + 'requirePath': 'component/' + this.fullPath, 'type': 'mixin' }); }; diff --git a/lib/templates/component.js b/lib/templates/component.js index 0f13738..e15d5dd 100644 --- a/lib/templates/component.js +++ b/lib/templates/component.js @@ -1,5 +1,7 @@ define(function (require) { - +<% + componentName = options['capitalize']? _.capitalize(name): name +%> 'use strict'; /** @@ -12,13 +14,13 @@ define(function (require) { * Module exports */ - return defineComponent(<%= _.camelize(name) %>); + return defineComponent(<%= _.camelize(componentName) %>); /** * Module function */ - function <%= _.camelize(name) %>() { + function <%= _.camelize(componentName) %>() { this.attributes({ }); diff --git a/lib/templates/mixin.js b/lib/templates/mixin.js index 2a7e3cd..7da1b7f 100644 --- a/lib/templates/mixin.js +++ b/lib/templates/mixin.js @@ -1,18 +1,21 @@ define(function (require) { - +<% + mixinName = _.camelize(name); + mixinName = options['capitalize']? _.capitalize(mixinName): mixinName; +%> 'use strict'; /** * Module exports */ - return with<%= _.classify(name) %>; + return <%= mixinName %>; /** * Module function */ - function with<%= _.classify(name) %>() { + function <%= mixinName %>() { this.attributes({ }); diff --git a/test/test.js b/test/test.js index 9e28c6f..714b8bc 100644 --- a/test/test.js +++ b/test/test.js @@ -87,7 +87,7 @@ describe('Flight generator test', function () { it('creates expected files', function (cb) { var expected = [ - ['app/js/component/my_component.js', /function myComponent()/], + ['app/js/component/my_component.js', /function myComponent\(\)/], ['test/spec/component/my_component.spec.js', /describeComponent\('component\/my_component/] ]; @@ -95,6 +95,63 @@ describe('Flight generator test', function () { }); }); + describe('flight:component with capitalize', function () { + var flightComponent; + + beforeEach(function (cb) { + var deps = ['../../lib/generators/component']; + flightComponent = helpers.createGenerator( + 'flight:component', + deps, + ['my_component'], + { + capitalize: true + } + ); + cb(); + }); + + it('runs sucessfully', function () { + flightComponent.run(); + }); + + it('creates expected files', function (cb) { + var expected = [ + ['app/js/component/my_component.js', /function MyComponent\(\)/], + ['test/spec/component/my_component.spec.js', /describeComponent\('component\/my_component/] + ]; + + helpers.assertGeneratorMakesExpected(flightComponent, expected, cb); + }); + }); + + describe('flight:component with path', function () { + var flightComponent; + + beforeEach(function (cb) { + var deps = ['../../lib/generators/component']; + flightComponent = helpers.createGenerator( + 'flight:component', + deps, + ['path/to/my_component'] + ); + cb(); + }); + + it('runs sucessfully', function () { + flightComponent.run(); + }); + + it('creates expected files', function (cb) { + var expected = [ + ['app/js/component/path/to/my_component.js', /function myComponent\(\)/], + ['test/spec/component/path/to/my_component.spec.js', /describeComponent\('component\/path\/to\/my_component/] + ]; + + helpers.assertGeneratorMakesExpected(flightComponent, expected, cb); + }); + }); + describe('flight:mixin', function () { var flightMixin; @@ -110,7 +167,7 @@ describe('Flight generator test', function () { it('creates expected files', function (cb) { var expected = [ - ['app/js/component/with_my_mixin.js', /function withMyMixin()/], + ['app/js/component/with_my_mixin.js', /function withMyMixin\(\)/], ['test/spec/component/with_my_mixin.spec.js', /describeMixin\('component\/with_my_mixin/] ]; @@ -118,6 +175,63 @@ describe('Flight generator test', function () { }); }); + describe('flight:mixin with capitalize', function () { + var flightMixin; + + beforeEach(function (cb) { + var deps = ['../../lib/generators/mixin']; + flightMixin = helpers.createGenerator( + 'flight:mixin', + deps, + ['my_mixin'], + { + capitalize: true + } + ); + cb(); + }); + + it('runs sucessfully', function () { + flightMixin.run(); + }); + + it('creates expected files', function (cb) { + var expected = [ + ['app/js/component/with_my_mixin.js', /function WithMyMixin\(\)/], + ['test/spec/component/with_my_mixin.spec.js', /describeMixin\('component\/with_my_mixin/] + ]; + + helpers.assertGeneratorMakesExpected(flightMixin, expected, cb); + }); + }); + + describe('flight:mixin with path', function () { + var flightMixin; + + beforeEach(function (cb) { + var deps = ['../../lib/generators/mixin']; + flightMixin = helpers.createGenerator( + 'flight:mixin', + deps, + ['path/to/my_mixin'] + ); + cb(); + }); + + it('runs sucessfully', function () { + flightMixin.run(); + }); + + it('creates expected files', function (cb) { + var expected = [ + ['app/js/component/path/to/with_my_mixin.js', /function withMyMixin\(\)/], + ['test/spec/component/path/to/with_my_mixin.spec.js', /describeMixin\('component\/path\/to\/with_my_mixin/] + ]; + + helpers.assertGeneratorMakesExpected(flightMixin, expected, cb); + }); + }); + describe('flight:page', function () { var flightPage;