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+ }
0 commit comments