官方网站
用于快速形成脚手架工程的工具
安装
$ npm install -g yo
在 Discovering generators 可以查看国际友人创建的 generators。在 Creating a generator 里可以创建自己的generators。
下面就要用 generator-generator 来创建构建器项目
安装 generator-generator
$ npm install -g generator-generator
使用下命令创建自己的脚手架基础项目
$ yo generator
根据以下提示进行操作
? Your generator name (generator-demo) # 名字格式要求必须是 generator- 开头# 我们输入 generator-zbl-gulp? Description# 输入描述 this is a gulp simple project? Project homepage url# 例如你有这个项目的github地址或者主页,输入? Author‘s Name# 输入你的名字? Author’s Email# 输入你的邮箱? Author‘s Homepage# 你的个人主页? Package Keywords# 输入关键字 gulp browsersync sass uglify? Send coverage reports to coveralls(Y/n)# 是否发送报告 y? Enter Node versions (comma separated)# 12.11 我们这里使用的node版本是12.11? Github username or organization# 可以选择提供github的用户名# 然后选择合适你的协议 这里我们选择的MIT# 然后就会自动安装并且形成一个目录
或许会遇到卡顿不前的情况,解决方法就是找一个已经发布过的库名
接着会提示你这个名字已存在,是否更换名字
选择是,这样在输入名字就可以成功继续啦~
使用
修改 package.json
接下来,打开文件夹的 package.json** **文件
第一个注意点是版本version得大于等于1.0.0
{"name": "generator-zbl-gulp-init","version": "1.0.1","description": "this is a gulp simple peoject","homepage": "","author": {"name": "zhubinglin","email": "2690420490@qq.com","url": "zbl.ink"},······
更改 generators/app/index.js
接下来在 generators/app/index.js 中修改一下配置
'use strict';const Generator = require('yeoman-generator');const chalk = require('chalk');const yosay = require('yosay');module.exports = class extends Generator {prompting() {// Have Yeoman greet the user.this.log(yosay(`Welcome to the groovy ${chalk.red('generator-gulp-zbl')} generator!`));const prompts = [{type: 'confirm',name: 'install',message: 'Would you like to enable this option?',default: true}];return this.prompt(prompts).then(props => {// To access props later use this.props.someAnswer;this.props = props;});}writing() {this.fs.copy(// 复制文件this.templatePath('**'),this.destinationPath('./'));}install() {this.installDependencies({// 不适用 bower 包管理工具bower: false});}};
复制项目
之后,把项目复制到 /generators/app/templates 里
打开终端工具,将这个项目在本地测试
首先链接到全局
$ npm link
下面,找个空目录打开终端工具,输入:
$ yo zbl-gulp-init# 会将/generators/app/templates 里的项目都复制到文件夹里
遇到问题
如上操作,并不会满足所有需求
不会将 .开头的文件复制
默认安装 install 导致速度很慢
不会让用户输入一些项目介绍
所以,对于 generators/app/index.js 文件进行调整:
"use strict";const Generator = require("yeoman-generator");const chalk = require("chalk");const yosay = require("yosay");module.exports = class extends Generator {prompting() {// 问候用户this.log(yosay(`Welcome to the luminous ${chalk.red("generator-imooc-glup")} generator!`));const prompts = [{type: "input",name: "name",message: "请输入你的项目名字:",default: "gulp-init"},{type: "input",name: "description",message: "请输入项目描述:",default: "a simple description"},{type: 'input',name: 'author',message: '请输入项目作者:',default: 'zbl-handsome'}];return this.prompt(prompts).then(props => {// To access props later use this.props.someAnswer;this.props = props;});}writing() {/***的意思是将template文件夹中的所有文件都复制到新的项目中*/this.fs.copy(this.templatePath("**"), this.destinationPath("./"));const specFileList = ['.browserslistrc']specFileList.forEach(file => {this.fs.copy(this.templatePath(file), this.destinationPath(file));})// 将用户信息保存到 package.json 里this.initPackage()}initPackage () {let pkg = this.fs.readJSON(this.templatePath('./package.json'), {});const { props } = this;Object.assign(pkg, {name: props.name,description: props.description});this.fs.extendJSON(this.destinationPath('./package.json'), pkg);}};
这样就大功告成啦!
