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

Commit

Permalink
feat(decorator): Add prompt if file already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
robinboehm committed May 2, 2013
1 parent d111286 commit 7d9b862
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
9 changes: 0 additions & 9 deletions decorator/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,3 @@ Example:

This will create:
app/scripts/decorators/serviceNameDecorator.js


If you want to use multiple decorators for one service,
you are able to use the second argument for the filename

yo angular:decorator serviceName serviceNameLogging [--coffee]

This will create:
app/scripts/decorators/serviceNameLoggingDecorator.js
76 changes: 60 additions & 16 deletions decorator/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');
var fs = require('fs');

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

module.exports = Generator;
util.inherits(Generator, ScriptBase);

function Generator() {
ScriptBase.apply(this, arguments);
var postFix = "Decorator";
Generator.prototype.askForOverwrite = function askForOverwrite() {
var cb = this.async();

// TODO: Any yeoman.util function to handle this?
var fileExists = fs.existsSync(this.env.cwd+'/app/scripts/'+buildRelativePath(this.fileName)+".js");
if (fileExists) {
var prompts = [{
name: 'overwriteDecorator',
message: 'Would you like to overwrite existing decorator?',
default: 'Y/n',
warning: 'Yes: Decorator will be replaced..'
}];

//TODO: Any better way in yeoman to get this value?
var fileName = arguments[0][1];
if(fileName === undefined){
fileName = this.name+postFix;
this.prompt(prompts, function (err, props) {
if (err) {
return this.emit('error', err);
}

this.overwriteDecorator = (/y/i).test(props.overwriteDecorator);

cb();
}.bind(this));
}
else{
fileName += postFix;
cb();
return;
}
this.fileName = fileName;
}
};

util.inherits(Generator, ScriptBase);
Generator.prototype.askForNewName = function askForNewName() {
var cb = this.async();

if(this.overwriteDecorator === undefined || this.overwriteDecorator === true){
cb();
return;
}
else{
var prompts = new Array();
prompts.push({
name: 'decortatorName',
message: 'Alternative name for the decorator:'
});

this.prompt(prompts, function (err, props) {
if (err) {
return this.emit('error', err);
}
this.fileName = props.decortatorName;

cb();
}.bind(this));
}
};

Generator.prototype.createDecoratorFiles = function createDecoratorFiles() {
this.appTemplate('decorator', 'scripts/decorators/' + this.fileName);
this.addScriptToIndex('decorators/' + this.fileName);
this.appTemplate('decorator', 'scripts/'+buildRelativePath(this.fileName));
this.addScriptToIndex(buildRelativePath(this.fileName));
};

function buildRelativePath(fileName){
return 'decorators/' + fileName+"Decorator";
}

0 comments on commit 7d9b862

Please sign in to comment.