在Yeoman生成器中仅运行单个函数

时间:2015-08-12 10:19:12

标签: javascript node.js yeoman 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

1 个答案:

答案 0 :(得分:0)

也许你在想这个?基于http://yeoman.io/authoring/running-context.html,我建议你的生成器中只有一个函数(默认?)。阅读名为"助手和私人方法"如果你想将你的生成器分解成其他方法。

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;

如果未设置标志,则生成器将退出。