Search code examples
yeomanyeoman-generator

YEOMAN run multiple sub generators in one function


I have an angular based generator with three sub generators for controller directive and service. I want to run one function that takes the NAME variable/input and runs all three of these at the same time instead of having to run each one separately.


Solution

  • Yeoman offers composeWith() (docs) which you can use to invoke other (sub)generators.

    'use strict';
    
    var generators = require('yeoman-generator');
    
    module.exports = generators.Base.extend({
      constructor: function () {
        generators.Base.apply(this, arguments);
        this.argument('theName', {
          type: String,
          required: true
        });
      },
    
      initializing: function() {
        this.composeWith('my-generator:mysubgen1', { args: [this.theName] });
        this.composeWith('my-generator:mysubgen2', { args: [this.theName] });
      }
    });
    

    In the example above I assume your generator is named my-generator and has two subgenerators (mysubgen1 and mysubgen2).

    The code above would go into a third subgenerator, e.g. doall. If you call $ yo my-generator:doall foobar it would now run both of the composed sub-generators, passing foobar as first argument.