运行 Yeoman 生成器中只有一个函数

Run only a single function in a Yeoman generator

如果设置了标志,我如何才能 运行 生成器中只有一个函数(并结束生成器)?实现此目标的首选方法是什么?

var MyGenerator = yeoman.generators.Base.extend({

  constructor: function () {
    yeoman.generators.Base.apply(this, arguments);

    this.option('flag', {
      desc: 'Do something',
      type: String,
      required: false,
      defaults: null
    });
  },

  runOnlyThisIfFlagIsSet: function() {
    if(this.options.flag) {
      // do stuff and end the generator so that it does all the things defined here
    }   
  },

  doNotRunThis: function() {
    // I don't want this to run if the flag is set
  },

  iCouldDoThisButItIsTooRepetitive: function() {
    if(!this.options.flag) {
      // do stuff
    } 
  }
});

module.exports = MyGenerator;

yo myGeneratorName --flag

也许你想多了?基于 http://yeoman.io/authoring/running-context.html,我建议你的生成器中可能只包含一个函数(默认?)。如果您想将生成器分解为其他方法,请阅读名为 "Helper and private methods" 的部分。

var MyGenerator = yeoman.generators.Base.extend({

  constructor: function () {
    yeoman.generators.Base.apply(this, arguments);

    this.option('flag', {
      desc: 'Do something',
      type: String,
      required: false,
      defaults: null
    });
  },

  default: function() {
    if(this.options.flag) {
      // do stuff and end the generator so that it does all the things defined here. Use the documentation link above to figure out how to create private methods you can call from here. 
    }    
  }


});

module.exports = MyGenerator;

如果没有设置标志,生成器将直接退出。