Yeoman invoke generator by code with args

六月ゝ 毕业季﹏ 提交于 2020-07-06 19:40:08

问题


I've having yeoman generator with sub generator. I need to invoke the sub generator via code and I use the code below which is working, I see that the sub generator is invoked and I got the question in the terminal.

docs: https://yeoman.io/authoring/integrating-yeoman.html

var yeoman = require('yeoman-environment');
var env = yeoman.createEnv();


env.lookup(function () {
    env.run('main:sub',err => {
        console.log('done' ,err);
    });
});

The sub generator have only one question

 prompting() {

    const prompts = [
      {
        name: "app",
        message: "which app to generate?",
        type: "input",
        default: this.props.app,
      },
    ];

...

I want to call it silently, which means to pass the value for app question via code and not using terminal and I try this which doesn't works, (I see the question in the terminal)

env.lookup(function () {
    env.run('main:sub',{"app":"nodejs"}, err => {
        console.log('done' ,err);
    });
});

and also tried this which doesnt works

env.lookup(function () {
    env.run('main:sub --app nodejs', err => {
        console.log('done' ,err);
    });
});

How can I do it ? pass the values using code (maybe like it's done on unit test but this code is not unit test... when the terminal is not invoked) From the docs im not sure how to pass the values https://yeoman.io/authoring/integrating-yeoman.html

I've also found this but didn't quite understand how to use it to pass parameter to generator http://yeoman.github.io/environment/Environment.html#.lookupGenerator is it possible?


回答1:


You can just do:

env.lookup(function () {
    env.run('main:sub',{"app":"nodejs"}, err => {
        console.log('done' ,err);
    });
});

and inside the sub sub-generator, you can find the value via this.options.app.

To disable the question prompt, defined when field inside the Question Object like this:

prompting() {

    const prompts = [
      {
        name: "app",
        message: "which app to generate?",
        type: "input",
        default: this.props.app,
        when: !this.options.app
      },
    ];

    . . .

    return this.prompt(prompts).then((props) => {
      this.props = props;

      this.props.app = this.options.app || this.props.app;

    });
}


来源:https://stackoverflow.com/questions/62437949/yeoman-invoke-generator-by-code-with-args

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