Skip to content

Commit 12f57b4

Browse files
committed
Merge branch 'add-decorator-generator' of git://github.com/robinboehm/generator-angular into robinboehm-add-decorator-generator
2 parents e1f5d67 + a8d3e56 commit 12f57b4

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

decorator/USAGE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Creates a new AngularJS decorator for a specified service
3+
4+
Example:
5+
yo angular:decorator serviceName [--coffee]
6+
7+
This will create:
8+
app/scripts/decorators/serviceNameDecorator.js

decorator/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
var util = require('util');
3+
var ScriptBase = require('../script-base.js');
4+
var fs = require('fs');
5+
6+
var Generator = module.exports = function Generator(args, options) {
7+
ScriptBase.apply(this, arguments);
8+
this.fileName = this.name;
9+
};
10+
11+
util.inherits(Generator, ScriptBase);
12+
13+
Generator.prototype.askForOverwrite = function askForOverwrite() {
14+
var cb = this.async();
15+
16+
// TODO: Any yeoman.util function to handle this?
17+
var fileExists = fs.existsSync(this.env.cwd + '/app/scripts/' + buildRelativePath(this.fileName) + ".js");
18+
if (fileExists) {
19+
var prompts = [{
20+
name : 'overwriteDecorator',
21+
message: 'Would you like to overwrite existing decorator?',
22+
default: 'Y/n',
23+
warning: 'Yes: Decorator will be replaced.'
24+
}];
25+
26+
this.prompt(prompts, function (err, props) {
27+
if (err) {
28+
return this.emit('error', err);
29+
}
30+
31+
this.overwriteDecorator = (/y/i).test(props.overwriteDecorator);
32+
33+
cb();
34+
}.bind(this));
35+
}
36+
else{
37+
cb();
38+
return;
39+
}
40+
};
41+
42+
Generator.prototype.askForNewName = function askForNewName() {
43+
var cb = this.async();
44+
45+
if (this.overwriteDecorator === undefined || this.overwriteDecorator === true) {
46+
cb();
47+
return;
48+
}
49+
else {
50+
var prompts = [];
51+
prompts.push({
52+
name : 'decortatorName',
53+
message: 'Alternative name for the decorator:'
54+
});
55+
56+
this.prompt(prompts, function (err, props) {
57+
if (err) {
58+
return this.emit('error', err);
59+
}
60+
this.fileName = props.decortatorName;
61+
62+
cb();
63+
}.bind(this));
64+
}
65+
};
66+
67+
Generator.prototype.createDecoratorFiles = function createDecoratorFiles() {
68+
this.appTemplate('decorator', 'scripts/' + buildRelativePath(this.fileName));
69+
this.addScriptToIndex(buildRelativePath(this.fileName));
70+
};
71+
72+
function buildRelativePath(fileName){
73+
return 'decorators/' + fileName + "Decorator";
74+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
angular.module("<%= _.camelize(appname) %>App").config ["$provide", ($provide) ->
4+
$provide.decorator "<%= _.camelize(name) %>", ($delegate) ->
5+
# decorate the $delegate
6+
$delegate
7+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
angular.module("<%= _.camelize(appname) %>App").config ($provide) ->
4+
$provide.decorator "<%= _.camelize(name) %>", ($delegate) ->
5+
# decorate the $delegate
6+
$delegate
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
angular.module('<%= _.camelize(appname) %>App')
4+
.config(['$provide', function ($provide) {
5+
$provide.decorator('<%= _.camelize(name) %>', function ($delegate) {
6+
// decorate the $delegate
7+
return $delegate;
8+
});
9+
}]);

templates/javascript/decorator.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
angular.module('<%= _.camelize(appname) %>App')
4+
.config(function ($provide) {
5+
$provide.decorator('<%= _.camelize(name) %>', function ($delegate) {
6+
// decorate the $delegate
7+
return $delegate;
8+
});
9+
});

0 commit comments

Comments
 (0)