酵母菌生成器增加一个新的文件,生成退出项目的文件。

问题描述 投票:3回答:1

我有一个酵母生成器,可以成功地生成一个简单的sproject。

我想要的是 之后 在项目生成的时候,在后面的使用中会有生成新文件的能力。deployment.yaml 根据 app 文件夹,但它需要从主生成器中读取一些数据,例如 appName 因为子生成器需要在生成的应用程序中生成一个新的文件。

例如 yo tdk

该命令将生成一个新的项目

而当我跑 yo tdk:event (或类似的东西)它将在项目中生成一个新的文件。app 夹子

为了说明问题,我创建了这个非常简单的生成器。

const Generator = require("yeoman-generator");

module.exports = class extends Generator {
  prompting() {
    this.props = {
      appName: "my-app",
      srvName: "my-service"
    };

    const prompts = [
      {
        name: "appName",
        message: "Project name: ",
        type: "input",
        default: this.props.appName
      },
      {
        name: "srvName",
        message: "Service name: ",
        type: "input",
        default: this.props.srvName
      }
    ];

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

  writing() {
    this.fs.copyTpl(
      this.templatePath("app"),
      this.destinationPath(this.props.appName),
      this.props
    );
  }
};

这个发电机有两个简单的问题

  1. 应用程序名称
  2. 服务名称

它将生成一个项目,如

myapp   /root
 -app   /folder
  - service.yaml   /single file at the project generation

生成的 service.yaml 看起来像下面这样。

apiVersion: v1
kind: Service
metadata:
  name: <%= appName %>
spec:
  selector:
    app: <%= srvName %>
  ports:
    - protocol: TCP
      port: 80

现在,在项目生成后,用这个 service.yaml 我想在 后世 (项目生成后)添加新文件。deployment.yaml 下的应用程序文件夹

deployment.yaml

apiVersion: v1
kind: Deployment
metadata:
  name: <%= appName %>  //this is the appname from the project generation

spec:
  replicas: <%= replica %>
  selector:
    app: <%= srvName %>

appName &amp。srvName 是来自 主发电机我看到子生成器之间有共享数据的选项。https:/yeoman.ioauthoringstorage.html。 ,不知道如何在发电机之间共享这个)和 replica 应该来自新闻配音生成器

这是生成后的项目结构

myapp   /root
 -app   /folder
  - service.yaml      /single file at the project generation
  - deployment.yaml   / new file added to the project under app folder

喜欢的用户另起炉灶 generator/sub 并有一个新的问题,如 how much replicas do you want? 然后生成文件。

我怎样才能做到这一点?

更新这是我的项目结构

myapp
 - node_modules
 - package.json //here I declare the main-generator command -> tdk
 - generators
 -- app
 ---index.ts
 --deployment
 ---index.ts
 ---package.json    //here I declare the sub-generator command -> deploy
 - node_modules
 - package.json
 -.yo-rc.json       //here I see the data that I keep via config.set api

更新

当我通过程序调用子生成器时,如

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

env.lookup(function () {
    env.run("tdk:deploy", {
        replicas: 100
    }, (err) => {
        console.log("done", err);
    });
});

我得到了错误。

out from config undefined : undefined /the undefind is from the console in the sub-generator(子生成器中的控制台)

done TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:125:11)
    at Object.join (path.js:1037:7)

我在子生成器的代码中放了一个 console.log,比如说

  initializing() {
    this.srvName = this.config.get("srvName");
    this.appName = this.config.get("appName");
    console.log("out from config", this.srvName, ":", this.appName);
  }

当我运行子生成器时,我得到了空的配置(来自于 .yo-rc.json)在检查.yo-rc.json时,我能够看到主生成器的条目,数据被存储了,但当我从程序中运行时,却找不到它。我能够从主生成器中看到条目,数据被存储,但当我从程序中运行它时,它没有找到它......任何想法?

这是这两个项目的链接(非常基本的yeoman生成器,可以证明这一点),只需要运行 npm install 两个项目和发电机的运行也是如此。npm link.

最后:项目应以下列方式生成 两档

1. service.yaml   // generated from the main generator
2. deployment.yaml - // generated from sub generator with the properties from the main & sub generator

目前,该 deployment.yaml 文件不生成

https:/drive.google.comdrivefolders1kBnZxpVcRR9qhGZagVtod7W4wFmt73C6。

1 . generator-tdk -  Generator and sub-generator 
2.  yeomanEnv - The code which is running the sub-generator to create the file inside the generated project

我到底做错了什么 :(

如果有一种方法可以从子生成器中读出 .yo-rc.json 它可以帮助

javascript node.js typescript yeoman yeoman-generator
1个回答
2
投票

你可以在配置中设置值 configuring 读取主生成器的值,就像这样。

configuring() {
  this.config.set('appName', this.props.appName);
  this.config.set('srvName', this.props.srvName);
}

读取子生成器中的值。

initializing() {
  this.srvName = this.config.get("srvName");
  this.appName = this.config.get("appName");
}

所以你可以通过以下方式访问这些数值 this.srvNamethis.appName 写完后。

示例代码。

appindex.js。

const Generator = require("yeoman-generator");

module.exports = class extends Generator {
  prompting() {
    this.props = {
      appName: "my-app",
      srvName: "my-service",
    };

    const prompts = [
      {
        name: "appName",
        message: "Project name: ",
        type: "input",
        default: this.props.appName,
      },
      {
        name: "srvName",
        message: "Service name: ",
        type: "input",
        default: this.props.srvName,
      },
    ];

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

  configuring() {
    this.config.set('appName', this.props.appName);
    this.config.set('srvName', this.props.srvName);
  }

  writing() {
    this.fs.copyTpl(
      this.templatePath("app"),
      this.destinationPath(this.props.appName),
      this.props
    );
  }
};

deployindex.js:

const Generator = require("yeoman-generator");

module.exports = class extends Generator {
  initializing() {
    this.srvName = this.config.get("srvName");
    this.appName = this.config.get("appName");
  }

  prompting() {
    this.props = {
      replicas: 0,
    };

    const prompts = [
      {
        name: "replica",
        message: "how much replicas do you want?",
        type: "input",
        default: this.props.replicas,
      },
    ];

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

  writing() {
    this.fs.copyTpl(
      this.templatePath("deploy"),
      this.destinationPath(this.appName),
      {
        srvName: this.srvName,
        appName: this.appName,
        ...this.props,
      }
    );
  }
};

和命令。

yo <name 为主项目生成

yo <name>:deploy 要求复制和创建 deployment.yaml


要执行子生成器而不使用 yo:

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

env.lookup(function () {
  env.run("<name>:deploy", {
      replicas: 100
  }, (err) => {
    console.log("done", err);
  });
});

和一个示例子生成器,如果通过选项传递值,则跳过问题(deploy/index.js):

const Generator = require("yeoman-generator");

module.exports = class extends Generator {
  initializing() {
    this.srvName = this.config.get("srvName");
    this.appName = this.config.get("appName");
  }

  prompting() {
    this.props = {
      replicas: 0,
    };

    const prompts = [
      {
        name: "replicas",
        message: "which app to generate?",
        type: "input",
        default: this.props.replicas,
        when: !this.options.replicas, // disable the question if it's found in options
      },
    ];

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

      // set values from options (if found)
      this.props.replicas = this.options.replicas || this.props.replicas;
    });
  }


  writing() {
    this.fs.copyTpl(
      this.templatePath("deploy"),
      this.destinationPath(this.appName),
      {
        srvName: this.srvName,
        appName: this.appName,
        ...this.props,
      }
    );
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.