Calling `ember g component` within a blueprint's index.js

Not sure if this is even possible, but I want a blueprint to automatically create a corresponding component in its afterInstall/afterUninstall hook. The thing is, I can’t simply:

afterInstall: function(options) {
  ember g component options.entity.name;
}

But maybe there’s a way to hook into the actual command? Or some more elegant/build-in solution?

Nevermind, just found an excellent example to follow here.

For anyone who wants to do this, here’s how I pulled it off:

// blueprints/my-blueprint/index.js

var Blueprint = require('ember-cli/lib/models/blueprint');
var blueprints = ['component', 'component-addon'].map(function(name) {
  return Blueprint.lookup(name);
});

module.exports = {
  description: '',

  beforeInstall: function(options) {
    blueprints.forEach(function(blueprint) {
      blueprint.install(options);
    });
  },

  beforeUninstall: function(options) {
    blueprints.forEach(function(blueprint) {
      blueprint.uninstall(options);
    });
  }
};

Then in my corresponding test blueprint:

// blueprints/my-blueprint-test/index.js

var Blueprint = require('ember-cli/lib/models/blueprint');
var componentBlueprint = Blueprint.lookup('component-test');

module.exports = {
  description: '',

  beforeInstall: function(options) {
    return componentBlueprint.install(options);
  },

  beforeUninstall: function(options) {
    return componentBlueprint.uninstall(options);
  }
};

@nullnullnull One of those beforeUninstall keys should be afterInstall, right?

Also, thanks for the thread! I found it helpful.

I’ve posted an updated answer to this question at ember.js - Generate another blueprint from an Ember CLI blueprint - Stack Overflow. The approach here was a step in the right direction, but it didn’t work for me out-of-the-box.

1 Like