invoke.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var path = require('path');
  2. /**
  3. * Receives a `namespace`, and an Hash of `options` to invoke a given
  4. * generator. The usual list of arguments can be set with `options.args`
  5. * (ex. nopt's argv.remain array)
  6. *
  7. * @param {String} namespace
  8. * @param {Object} options
  9. * @param {Function} cb
  10. */
  11. module.exports = function invoke(namespace, options, cb) {
  12. cb = cb || function () {};
  13. options = options || {};
  14. options.args = options.args || [];
  15. var generator = this.env.create(namespace, options);
  16. if (!generator.sourceRoot()) {
  17. generator.sourceRoot(path.join(path.dirname(generator.resolved), 'templates'));
  18. }
  19. // validate the generator (show help on missing arguments/options)
  20. // also show help if --help was specifically passed
  21. var requiredArgs = generator._arguments.some(function (arg) {
  22. return arg.config && arg.config.required;
  23. });
  24. if (!options.args.length && requiredArgs) {
  25. return console.log(generator.help());
  26. }
  27. if (options.help) {
  28. return console.log(generator.help());
  29. }
  30. this.log.emit('up');
  31. this.log.invoke(namespace);
  32. this.log.emit('up');
  33. generator.on('end', this.log.emit.bind(this.log, 'down'));
  34. generator.on('end', this.log.emit.bind(this.log, 'down'));
  35. return generator.run(cb);
  36. };