Calling a yeoman generator after a generator has finished

雨燕双飞 提交于 2019-12-23 12:39:01

问题


I am looking to call another yeoman generator once the first generator has finished installing, this will be based on an answer I give for one of the prompts.

I have tried calling it at the end.

end: function () {
  this.installDependencies({
    callback: function () {

      if( this.generator2 ){
        shell.exec('yo generator2');
      }

    }.bind(this)
  });
},

This runs generator2, but I am unable to answer any prompts.

These are 2 separate generators, so I cannot make the second a sub generator.


回答1:


Use Yeoman composability feature.

About the code, don't use this.installDependencies() callback (that won't work as you expect). Rather use the run loop priorities.

Also, you should review your logic and the way you think about your current problem. When composing generators, the core idea is to keep both decoupled. They shouldn't care about the ordering, they should run in any order and output the same result. Thinking about your code this way will greatly reduce the complexity and make it more robust.




回答2:


I see this is an older question, but I came accross a similar requirement & want to make sure all options are listed. I agree with the other answers that it is the best choice to use the composability feature & keep the order irrelevant. But in case it really is necessary to run generators sequentially: You can also execute another generator using the integration features.

So in generator1 you could call

this.env.run('generator2');

This will also let you answer prompts in generator2.




回答3:


When using .composeWith a priority group function (e.g.: prompting, writing...) will be executed for all the generators, then the next priority group. If you call .composeWith to generatorB from inside a generatorA, then execution will be, e.g.:

generatorA.prompting => generatorB.prompting => generatorA.writing => generatorB.writing

You can cover all possible execution scenarios, condition checking with this concept, also use the options of .composeWith('my-genertor', { 'options' : options })

If you want to control execution between different generators, I advise you to create a "main" generator which composes them together, like written on http://yeoman.io/authoring/composability.html#order:

// In my-generator/generators/turbo/index.js
module.exports = require('yeoman-generator').Base.extend({
  'prompting' : function () {
    console.log('prompting - turbo');
  },

  'writing' : function () {
    console.log('prompting - turbo');
  }
});

// In my-generator/generators/electric/index.js
module.exports = require('yeoman-generator').Base.extend({
  'prompting' : function () {
    console.log('prompting - zap');
  },

  'writing' : function () {
    console.log('writing - zap');
  }
});

// In my-generator/generators/app/index.js
module.exports = require('yeoman-generator').Base.extend({
  'initializing' : function () {
    this.composeWith('my-generator:turbo');
    this.composeWith('my-generator:electric');
  }
});


来源:https://stackoverflow.com/questions/29512751/calling-a-yeoman-generator-after-a-generator-has-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!