官方网站
用于快速形成脚手架工程的工具

安装

  1. $ npm install -g yo

在 Discovering generators 可以查看国际友人创建的 generators。在 Creating a generator 里可以创建自己的generators。

下面就要用 generator-generator 来创建构建器项目
安装 generator-generator

  1. $ npm install -g generator-generator

使用下命令创建自己的脚手架基础项目

  1. $ yo generator

根据以下提示进行操作

  1. ? Your generator name (generator-demo) # 名字格式要求必须是 generator- 开头
  2. # 我们输入 generator-zbl-gulp
  3. ? Description
  4. # 输入描述 this is a gulp simple project
  5. ? Project homepage url
  6. # 例如你有这个项目的github地址或者主页,输入
  7. ? Author‘s Name
  8. # 输入你的名字
  9. ? Author’s Email
  10. # 输入你的邮箱
  11. ? Author‘s Homepage
  12. # 你的个人主页
  13. ? Package Keywords
  14. # 输入关键字 gulp browsersync sass uglify
  15. ? Send coverage reports to coveralls(Y/n)
  16. # 是否发送报告 y
  17. ? Enter Node versions (comma separated)
  18. # 12.11 我们这里使用的node版本是12.11
  19. ? Github username or organization
  20. # 可以选择提供github的用户名
  21. # 然后选择合适你的协议 这里我们选择的MIT
  22. # 然后就会自动安装并且形成一个目录

或许会遇到卡顿不前的情况,解决方法就是找一个已经发布过的库名
接着会提示你这个名字已存在,是否更换名字
选择是,这样在输入名字就可以成功继续啦~
image.png

使用

修改 package.json

接下来,打开文件夹的 package.json** **文件
第一个注意点是版本version得大于等于1.0.0

  1. {
  2. "name": "generator-zbl-gulp-init",
  3. "version": "1.0.1",
  4. "description": "this is a gulp simple peoject",
  5. "homepage": "",
  6. "author": {
  7. "name": "zhubinglin",
  8. "email": "2690420490@qq.com",
  9. "url": "zbl.ink"
  10. },
  11. ······

更改 generators/app/index.js

接下来在 generators/app/index.js 中修改一下配置

  1. 'use strict';
  2. const Generator = require('yeoman-generator');
  3. const chalk = require('chalk');
  4. const yosay = require('yosay');
  5. module.exports = class extends Generator {
  6. prompting() {
  7. // Have Yeoman greet the user.
  8. this.log(
  9. yosay(`Welcome to the groovy ${chalk.red('generator-gulp-zbl')} generator!`)
  10. );
  11. const prompts = [
  12. {
  13. type: 'confirm',
  14. name: 'install',
  15. message: 'Would you like to enable this option?',
  16. default: true
  17. }
  18. ];
  19. return this.prompt(prompts).then(props => {
  20. // To access props later use this.props.someAnswer;
  21. this.props = props;
  22. });
  23. }
  24. writing() {
  25. this.fs.copy(
  26. // 复制文件
  27. this.templatePath('**'),
  28. this.destinationPath('./')
  29. );
  30. }
  31. install() {
  32. this.installDependencies({
  33. // 不适用 bower 包管理工具
  34. bower: false
  35. });
  36. }
  37. };

复制项目

之后,把项目复制到 /generators/app/templates 里
打开终端工具,将这个项目在本地测试
首先链接到全局

  1. $ npm link

下面,找个空目录打开终端工具,输入:

  1. $ yo zbl-gulp-init
  2. # 会将/generators/app/templates 里的项目都复制到文件夹里

遇到问题

如上操作,并不会满足所有需求
不会将 .开头的文件复制
默认安装 install 导致速度很慢
不会让用户输入一些项目介绍
所以,对于 generators/app/index.js 文件进行调整:

  1. "use strict";
  2. const Generator = require("yeoman-generator");
  3. const chalk = require("chalk");
  4. const yosay = require("yosay");
  5. module.exports = class extends Generator {
  6. prompting() {
  7. // 问候用户
  8. this.log(yosay(
  9. `Welcome to the luminous ${chalk.red("generator-imooc-glup")} generator!`
  10. ));
  11. const prompts = [
  12. {
  13. type: "input",
  14. name: "name",
  15. message: "请输入你的项目名字:",
  16. default: "gulp-init"
  17. },
  18. {
  19. type: "input",
  20. name: "description",
  21. message: "请输入项目描述:",
  22. default: "a simple description"
  23. },
  24. {
  25. type: 'input',
  26. name: 'author',
  27. message: '请输入项目作者:',
  28. default: 'zbl-handsome'
  29. }
  30. ];
  31. return this.prompt(prompts).then(props => {
  32. // To access props later use this.props.someAnswer;
  33. this.props = props;
  34. });
  35. }
  36. writing() {
  37. /*
  38. **的意思是将template文件夹中的所有文件都复制到新的项目中
  39. */
  40. this.fs.copy(this.templatePath("**"), this.destinationPath("./"));
  41. const specFileList = ['.browserslistrc']
  42. specFileList.forEach(file => {
  43. this.fs.copy(this.templatePath(file), this.destinationPath(file));
  44. })
  45. // 将用户信息保存到 package.json 里
  46. this.initPackage()
  47. }
  48. initPackage () {
  49. let pkg = this.fs.readJSON(this.templatePath('./package.json'), {});
  50. const { props } = this;
  51. Object.assign(pkg, {
  52. name: props.name,
  53. description: props.description
  54. });
  55. this.fs.extendJSON(this.destinationPath('./package.json'), pkg);
  56. }
  57. };

这样就大功告成啦!