首页 技术 正文
技术 2022年11月14日
0 收藏 401 点赞 4,756 浏览 1511 个字

yoman作为一个模板工具,能够创建自己的模板,下面具体介绍下。

首先 安装一个模板工具(npm install -g generator-generator),此工具会自动创建一些必要的文件。安装完成后,yo generator 就行。

最重要的一个文件就是generators中的index.js,生成器的所有逻辑都在此文件中。

文件里面的日志输出,同一用this.log(“”);

constructor

在此构造函数里,通常用来定义命令行的参数。一般用不到,通过prompt交互更加友好。

// Next, add your custom code

    this.option('coffee'); // This method adds support for a `--coffee` flag   这样就添加了一个coffee 命令行参数。

prompting

用来和client交互,控制台的输出:

this.log(yosay(
'Welcome to the magnificent ' + chalk.red('generator-ryanfirst') + ' generator!'
));
会在界面上显示:

yoman 创建generator

prompting是个对象可以定义多个交互方法:

   prompting:{
dir: function () { if (this.options.createDirectory !== undefined) {
return true;
}
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the magnificent ' + chalk.red('generator-ryanfirst') + ' generator!'
)); var prompt = [{
type: 'confirm',
name: 'createDirectory',
message: 'Would you like to create a new directory for your project?'
}]; return this.prompt(prompt).then(function (response) {
this.options.createDirectory = response.createDirectory;
}.bind(this));
},
dirname: function () {
if (!this.options.createDirectory || this.options.dirname) {
return true;
} var prompt = [{
type: 'input',
name: 'dirname',
message: 'Enter directory name'
}]; return this.prompt(prompt).then(function (response) {
this.options.dirname = response.dirname;
}.bind(this));
}
}

这样就会有以下的输出

yoman 创建generator

writing

主要的执行逻辑,创建文件和同步模板文件等。作为示例,仅仅做文件的同步:

  if(this.options.createDirectory){
this.destinationRoot(this.options.dirname);
this.appname = this.options.dirname;
}
this.fs.copy(
this.templatePath('.'), this.destinationPath('.')
);

如上,首先根据promt中的输入,创建文件夹,然后再同步所有的模板文件中的文件。

install

安装所有的依赖项。

以上即是所有的主要方法,可自定义方法,输出需要信息。yoman的执行顺序是依次从上到下执行。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859