【问题标题】:programmically give input to yeoman cmd for CI/CD tool以编程方式为 CI/CD 工具的 yeoman 命令提供输入
【发布时间】:2022-08-11 15:23:32
【问题描述】:

当我们运行 yeoman 时,它要求一个一个地添加输入,是否可以一次性提供所有输入?或以编程方式?

例如 :

yo azuresfguest

它要求添加 5 个输入,我想给一次,所以我可以在 CI/CD 系统中运行

谢谢,

    标签: javascript node.js npm yeoman


    【解决方案1】:

    不幸的是,没有通用的方法。特定的生成器需要允许它。

    我认为它值得对 Yeoman 项目提出功能请求,我已经 logged here

    作为一个繁琐的解决方法,您可以创建自己的生成器来重用现有的生成器。下面的 TypeScript 代码给出了一个例子;我正在使用这种方法来自动化我的 CI 流程。

    向构造函数添加选项:

      constructor(args: string, opts: Generator.GeneratorOptions) {
          super(args, opts);
          ...
          this.option("prompts-json-file", {
              type: String,
              default: undefined,
              description: "Skips prompting; uses file contents. Useful for automation",
          });
      }
    

    使用选项:

    async prompting() {
      if (this.options["prompts-json-file"] !== undefined) {
          this.answers = new Answers(JSON.parse(
                  fs.readFileSync(this.options["prompts-json-file"]).toString()
          ));
      }
      else {
        this.answers = ...
      }
    

    }

    不幸的是,这确实绕过了提示验证,因此您需要单独确保您的文件包含有效值。

    使用起来比较简单:

    yo my-generator --prompts-json-file ./prompts.json
    

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多