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

Commit 7d9b862

Browse files
committed
feat(decorator): Add prompt if file already exists
1 parent d111286 commit 7d9b862

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

decorator/USAGE

-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,3 @@ Example:
66

77
This will create:
88
app/scripts/decorators/serviceNameDecorator.js
9-
10-
11-
If you want to use multiple decorators for one service,
12-
you are able to use the second argument for the filename
13-
14-
yo angular:decorator serviceName serviceNameLogging [--coffee]
15-
16-
This will create:
17-
app/scripts/decorators/serviceNameLoggingDecorator.js

decorator/index.js

+60-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,74 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var 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

2767
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));
3070
};
71+
72+
function buildRelativePath(fileName){
73+
return 'decorators/' + fileName+"Decorator";
74+
}

0 commit comments

Comments
 (0)