11'use strict' ;
2- var path = require ( 'path' ) ;
32var util = require ( 'util' ) ;
43var ScriptBase = require ( '../script-base.js' ) ;
5- var angularUtils = require ( '../util.js ' ) ;
4+ var fs = require ( 'fs ' ) ;
65
6+ var Generator = module . exports = function Generator ( args , options ) {
7+ ScriptBase . apply ( this , arguments ) ;
8+ this . fileName = this . name ;
9+ }
710
8- module . exports = Generator ;
11+ util . inherits ( Generator , ScriptBase ) ;
912
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+ } ] ;
1325
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 ) ) ;
1835 }
1936 else {
20- fileName += postFix ;
37+ cb ( ) ;
38+ return ;
2139 }
22- this . fileName = fileName ;
23- }
40+ } ;
2441
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+ } ;
2666
2767Generator . 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 ) ) ;
3070} ;
71+
72+ function buildRelativePath ( fileName ) {
73+ return 'decorators/' + fileName + "Decorator" ;
74+ }
0 commit comments