Yeoman:使用用户提供的参数调用子生成器

2023-12-24

我正在编写我的第一个 Yeoman 生成器,它会提示用户进行各种输入,并根据他们的响应有条件地创建文件。我需要能够根据用户输入调用子例程(可能是 Yeoman 子生成器),并将参数传递给它。

我想使用命名函数(不会自动运行)的原因是,有时用户的响应应该调用多个组合的函数,有时该函数应该单独运行。

我尝试过的:

我认为子生成器是可行的方法,因为只有在用户请求时我才会创建文件集。但我很难打电话给他们有条件的并向它们传递用户提供的输入。我尝试过使用hookFor,但我收到断言错误:hookFor must be used within the constructor only。 (因为我不希望它默认运行,所以我从我的this.prompt(prompts, function (props)).

问题:

如何仅在用户请求时(通过提示)调用例程,并向该例程传递一些用户提供的信息?

如果您愿意回答,请不要认为我已经尝试过一些显而易见的事情;-)。


假设您有一台发电机generator-blog(BlogGenerator) 有两个子生成器(博客服务器和博客客户端):

app\index.js
client\index.js
server\index.js

所以当你跑步时yo blog你应该向用户询问一些选项并运行(可选)子生成器,对吧?

要运行子生成器,您需要调用this.invoke("generator_namespace", {options: {}})。 我们传递的第二个参数可以有options字段 - 它是将传递给生成器的选项对象。

在应用程序\index.js中:

BlogGenerator.prototype.askFor = function askFor() {
  var cb = this.async();

  // have Yeoman greet the user.
  console.log(this.yeoman);

  var prompts = [{
    name: 'appName',
    message: 'Enter your app name',
    default: 'MyBlog'
  }, {
    type: 'confirm',
    name: 'createServer',
    message: 'Would you like to create server project?',
    default: true
  }, {
    type: 'confirm',
    name: 'createClient',
    message: 'Whould you like to create client project?',
    default: true
  }];

  this.prompt(prompts, function (props) {
    this.appName = props.appName;
    this.createServer = props.createServer;
    this.createClient = props.createClient;

    cb();
  }.bind(this));
}

BlogGenerator.prototype.main = function app() {
  if (this.createClient) {
    // Here: we'are calling the nested generator (via 'invoke' with options)
    this.invoke("blog:client", {options: {nested: true, appName: this.appName}});
  }
  if (this.createServer) {
    this.invoke("blog:server", {options: {nested: true, appName: this.appName}});
  }
};

在 client\index.js 中:

var BlogGenerator = module.exports = function BlogGenerator(args, options, config) {
  var that = this;
  yeoman.Base.apply(this, arguments);
  // in this.options we have the object passed to 'invoke' in app/index.js:
  this.appName = that.options.appName;
  this.nested  = that.options.nested;
};

BlogGenerator .prototype.askFor = function askFor() {
  var cb = this.async();

  if (!this.options.nested) {
    console.log(this.yeoman);
  }
}

更新2015-12-21:
Using invoke现已弃用,应替换为composeWith。但这并不那么容易。之间的主要区别invoke and composeWith是现在你没有能力控制子发电机了。您只能声明使用它们。
就是这样main上面的方法应该如下所示:

BlogGenerator.prototype.main = function app() {
  if (this.createClient) {
    this.composeWith("blog:client", { 
        options: { 
          nested: true, 
          appName: this.appName
        } 
      }, {
        local: require.resolve("./../client")
      });
  }
  if (this.createServer) {
    this.composeWith("blog:server", { 
        options: { 
          nested: true, 
          appName: this.appName
        } 
      }, {
        local: require.resolve("./../server")
      });
  }
};

我也删除了替换yeoman.generators.Base with yeoman.Base.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Yeoman:使用用户提供的参数调用子生成器 的相关文章

随机推荐