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

add support for angular component #1342

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added component/USAGE
Empty file.
18 changes: 18 additions & 0 deletions component/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
var util = require('util');
var ScriptBase = require('../script-base.js');

var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
};

util.inherits(Generator, ScriptBase);

Generator.prototype.createComponentFiles = function createComponentFiles() {
this.generateSourceAndTest(
'component',
'spec/component',
'components',
this.options['skip-add'] || false
);
};
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Available generators:
* [angular](#app) (aka [angular:app](#app))
* [angular:controller](#controller)
* [angular:directive](#directive)
* [angular:component](#component)
* [angular:filter](#filter)
* [angular:route](#route)
* [angular:service](#service)
Expand Down Expand Up @@ -127,6 +128,24 @@ angular.module('myMod').directive('myDirective', function () {
});
```

### Component
Generates a component in `app/scripts/components`.

Example:
```bash
yo angular:component myComponent
```

Produces `app/scripts/components/myComponent.js`:
```javascript
angular.module('myMod').component('myComponent', {
template: '<div>{{$ctrl.message}}</div>',
controller: function () {
this.message = 'this is the myComponent component';
}
});
```

### Filter
Generates a filter in `app/scripts/filters`.

Expand Down
14 changes: 14 additions & 0 deletions templates/coffeescript/component.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

###*
# @ngdoc component
# @name <%= scriptAppName %>.component:<%= cameledName %>
# @description
# # <%= cameledName %>
###
angular.module '<%= scriptAppName %>'
.component '<%= cameledName %>',
template: '<div>{{$ctrl.message}}</div>'
controller: ->
this.message = 'this is the <%= cameledName %> component'
return
18 changes: 18 additions & 0 deletions templates/coffeescript/spec/component.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

'use strict'

describe 'Component: <%= cameledName %>', ->

# load the component's module
beforeEach module '<%= scriptAppName %>'

scope = {}

beforeEach inject ($rootScope) ->
scope = $rootScope.$new()

it 'should make hidden element visible', inject ($compile) ->
element = angular.element '<<%= _.dasherize(name) %>></<%= _.dasherize(name) %>>'
element = $compile(element) scope
scope.$apply()
expect(element.text()).toBe 'this is the <%= cameledName %> component'
2 changes: 1 addition & 1 deletion templates/common/root/_bower.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{<% var ngVer = "1.4.0" %>
{<% var ngVer = "1.5.7" %>
"name": "<%= _.slugify(_.humanize(appname)) %>",
"version": "0.0.0",
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions templates/javascript/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/**
* @ngdoc component
* @name <%= scriptAppName %>.component:<%= cameledName %>
* @description
* # <%= cameledName %>
*/
angular.module('<%= scriptAppName %>')
.component('<%= cameledName %>', {
template: '<div>{{$ctrl.message}}</div>',
controller: function () {
this.message = 'this is the <%= cameledName %> component';
}
});
20 changes: 20 additions & 0 deletions templates/javascript/spec/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

describe('Component: <%= cameledName %>', function () {

// load the components's module
beforeEach(module('<%= scriptAppName %>'));

var element, scope;

beforeEach(inject(function ($rootScope) {
scope = $rootScope.$new();
}));

it('should make hidden element visible', inject(function ($compile) {
element = angular.element('<<%= _.dasherize(name) %>></<%= _.dasherize(name) %>>');
element = $compile(element)(scope);
scope.$apply();
expect(element.text()).toBe('this is the <%= cameledName %> component');
}));
});
22 changes: 22 additions & 0 deletions test/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
var fs = require('fs');
var path = require('path');
var helpers = require('yeoman-test');
var assert = require('yeoman-assert');

describe('angular:component', function () {
beforeEach(function (done) {
helpers
.run(require.resolve('../component'))
.withArguments('foo')
.on('end', done);
});

it('generates a new component', function () {
assert.file('test/spec/components/foo.js');
assert.fileContent(
path.join('app/scripts/components/foo.js'),
/component\('foo'/
);
});
});