以下是我如何扩展yeoman生成器Base
类
module.exports = yeoman.generators.Base.extend({
prompting: function () {
var done = this.async();
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the cool ' + chalk.red('generator-zeetings') + ' generator!'
));
var prompts = [{
...
}];
this.prompt(prompts, function (props) {
this.props = props;
// ...
done();
}.bind(this));
},
_directory: function (source, destination) {
// Simplified for this question
var src = path.join(source, 'index.html');
var dest = path.join(destination, 'final.html');
console.log('Copy from ' + src + ' to ' + dest);
this.fs.copyTpl(
this.templatePath(src),
this.destinationPath(dest),
this.props
);
},
prepareApp: function () {
this._directory(this.templatePath(), this.destinationRoot('app'));
},
prepareData: function () {
this._directory(this.templatePath(), this.destinationRoot('data'));
},
writing: function () {
this.prepareApp();
this.prepareData();
},
});
我发现由于某种原因,辅助函数_directory
被调用了4次。
我希望通过app
函数只调用两次函数,一个用于data
,另一个用于文件夹writing
。
如果我将_
添加到appendApp
和appendData
,则生成器将按照我的预期运行。
我想了解的是:
1)我似乎没有在自耕农网站上找到这种行为。这种行为('调用子类中的每个公共方法')都是预期的吗?
2)如果这是预期的行为,我如何控制调用这些公共方法的顺序?
答案 0 :(得分:0)
Refer to the documentation: http://yeoman.io/authoring/running-context.html
Each function in a yeoman-generator is a task. Tasks are all run automatically and ordered based on their priorities (priority is defined by the function/object name).
Functions names starting with an underscore are considered private and won't be runned.