This repository has been archived by the owner on Mar 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorator): Add prompt if file already exists
- Loading branch information
1 parent
d111286
commit 7d9b862
Showing
2 changed files
with
60 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |