1
1
'use strict' ;
2
- var path = require ( 'path' ) ;
3
2
var util = require ( 'util' ) ;
4
3
var ScriptBase = require ( '../script-base.js' ) ;
5
- var angularUtils = require ( '../util.js ' ) ;
4
+ var fs = require ( 'fs ' ) ;
6
5
6
+ var Generator = module . exports = function Generator ( args , options ) {
7
+ ScriptBase . apply ( this , arguments ) ;
8
+ this . fileName = this . name ;
9
+ }
7
10
8
- module . exports = Generator ;
11
+ util . inherits ( Generator , ScriptBase ) ;
9
12
10
- function Generator ( ) {
11
- ScriptBase . apply ( this , arguments ) ;
12
- var postFix = "Decorator" ;
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
+ } ] ;
13
25
14
- //TODO: Any better way in yeoman to get this value?
15
- var fileName = arguments [ 0 ] [ 1 ] ;
16
- if ( fileName === undefined ) {
17
- fileName = this . name + postFix ;
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 ) ) ;
18
35
}
19
36
else {
20
- fileName += postFix ;
37
+ cb ( ) ;
38
+ return ;
21
39
}
22
- this . fileName = fileName ;
23
- }
40
+ } ;
24
41
25
- util . inherits ( Generator , ScriptBase ) ;
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 = new Array ( ) ;
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
+ } ;
26
66
27
67
Generator . prototype . createDecoratorFiles = function createDecoratorFiles ( ) {
28
- this . appTemplate ( 'decorator' , 'scripts/decorators/' + this . fileName ) ;
29
- this . addScriptToIndex ( 'decorators/' + this . fileName ) ;
68
+ this . appTemplate ( 'decorator' , 'scripts/' + buildRelativePath ( this . fileName ) ) ;
69
+ this . addScriptToIndex ( buildRelativePath ( this . fileName ) ) ;
30
70
} ;
71
+
72
+ function buildRelativePath ( fileName ) {
73
+ return 'decorators/' + fileName + "Decorator" ;
74
+ }
0 commit comments