Jen*_*y M 8 javascript node.js yeoman typescript yeoman-generator
我有 yeoman 生成器,它成功地生成了一个简单的 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
);
}
};
Run Code Online (Sandbox Code Playgroud)
这个生成器有两个简单的问题
它会生成一个像
myapp /root
-app /folder
- service.yaml /single file at the project generation
Run Code Online (Sandbox Code Playgroud)
生成的service.yaml内容如下:
apiVersion: v1
kind: Service
metadata:
name: <%= appName %>
spec:
selector:
app: <%= srvName %>
ports:
- protocol: TCP
port: 80
Run Code Online (Sandbox Code Playgroud)
现在用这个service.yaml文件生成项目后,我想在后期(项目生成后)deployment.yaml在 app 文件夹下添加新文件
deployment.yaml
apiVersion: v1
kind: Deployment
metadata:
name: <%= appName %> //this is the appname from the project generation
spec:
replicas: <%= replica %>
selector:
app: <%= srvName %>
Run Code Online (Sandbox Code Playgroud)
该appName&srvName从未来的主发电机,(我看到有选项子之间共享数据发生器
https://yeoman.io/authoring/storage.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
Run Code Online (Sandbox Code Playgroud)
就像用户开始另一个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
Run Code Online (Sandbox Code Playgroud)
更新
当我通过程序调用子生成器时
const yeoman = require('yeoman-environment'); const env = yeoman.createEnv();
env.lookup(function () {
env.run("tdk:deploy", {
replicas: 100
}, (err) => {
console.log("done", err);
});
});
Run Code Online (Sandbox Code Playgroud)
我有错误:
out from config undefined : undefined // undefind 来自子生成器的控制台
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)
Run Code Online (Sandbox Code Playgroud)
我在子生成器代码中放了一个 console.log 像
initializing() {
this.srvName = this.config.get("srvName");
this.appName = this.config.get("appName");
console.log("out from config", this.srvName, ":", this.appName);
}
Run Code Online (Sandbox Code Playgroud)
当我运行子生成器时,我.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
Run Code Online (Sandbox Code Playgroud)
目前,该deployment.yaml文件未生成
https://drive.google.com/drive/folders/1kBnZxpVcRR9qhGZagVtod7W4wFmt73C6
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?:(
如果子生成器有办法读取.yo-rc.json,它会有所帮助
configuring您可以在主生成器内部设置要配置的值,如下所示:
configuring() {
this.config.set('appName', this.props.appName);
this.config.set('srvName', this.props.srvName);
}
Run Code Online (Sandbox Code Playgroud)
并读取子生成器内的值:
initializing() {
this.srvName = this.config.get("srvName");
this.appName = this.config.get("appName");
}
Run Code Online (Sandbox Code Playgroud)
this.srvName因此,您可以通过书写来访问这些值this.appName。
示例代码:
应用程序/index.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
);
}
};
Run Code Online (Sandbox Code Playgroud)
部署/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: "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,
}
);
}
};
Run Code Online (Sandbox Code Playgroud)
和命令:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
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,
}
);
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
527 次 |
| 最近记录: |