为什么在yeoman.generators.Base子类中调用_directory函数四次?

时间:2016-02-03 02:01:31

标签: yeoman yeoman-generator

以下是我如何扩展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

如果我将_添加到appendAppappendData,则生成器将按照我的预期运行。

我想了解的是:

1)我似乎没有在自耕农网站上找到这种行为。这种行为('调用子类中的每个公共方法')都是预期的吗?

2)如果这是预期的行为,我如何控制调用这些公共方法的顺序?

1 个答案:

答案 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.