ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

脚手架工具之自定义 Generator

2022-07-01 18:02:12  阅读:204  来源: 互联网

标签:src Generator generator js sample 脚手架 模板 自定义


目录

脚手架工具之自定义自定义 Generator

基于 Yeoman 搭建自己的脚手架

创建 Generator 模块

Generator 本质上就是一个 NPM 模块

Generator 基本结构

image

多个 Sub-Generator

image

命名要求

必须以 generator- 开头,如 generator-<name>

演示创建 generator-sample

  • 引入 yeoman-generator (生成器基类)

    1. mkdir generator-sample
    2. cd generator-sample
    3. yarn init
    4. yarn add yeoman-generator (生成器基类)
    5. code . (使用 vscode 打开当前目录)

    image

  • 使用 yeoman-generator

    // 此文件作为 Generator 的核心入口
    // 需要导出一个继承自 Yeoman Generator 的类型
    // Yeoman Generator 在工作时会自动调用我们在此类型中定义的一些生命周期方法
    // 我们在这些方法中可以通过调用父类提供的一些工具方法实现一些功能,例如文件写入
    
    // 使用自定义 generator
    // 1. yarn link
    // 2. yo sample
    
    const Generator = require('yeoman-generator')
    
    module.exports = class extends Generator {
      writing() {
    	// Yeoman 自动在生成文件阶段调用此方法
    	// 1.我们在这里尝试往项目目录中写入文件
    	this.fs.write(
    	 this.destinationPath('temp.txt'),
    	   Math.random().toString()
    	)
    }
    

    image

  • 使用 sample 命令

    1. yarn link (将 sample 模块链接到全局范围,使之成为全局模块包)
    2. 运行生成器
      1. cdd ..
      2. mkdir my-proj
      3. yo sample

根据模板创建文件

  • 模板文件

    // generator-sample/generators/app/templates/foo.txt
    这是一个模板文件
    内部可以使用 EJS 模板标记输出数据
    例如:<%= title %>
    
    其他 EJS 语法也支持
    
    <% if (success) { %>
    哈哈哈
    <% } %>
    
  • 编写代码

    const Generator = require('yeoman-generator')
    
    module.exports = class extends Generator {
      writing() {
    	// 通过模板方式写入文件到目标目录(根据模板创建文件)
    
    	// 模板文件路径
    	const tmpl = this.templatePath('foo.txt')
    	// 输出目标路径
    	const output = this.destinationPath('foo.txt')
    	// 模板数据上下文
    	const context = { title: 'Hello, Robert~', success: false }
    
    	this.fs.copyTpl(tmpl, output, context)
      }
    }
    
  • 运行模块

    // d:\robert\Desktop\my-proj
    yo sample
    
  • 生成的文件

    // d:\robert\Desktop\my-proj\foo.txt
    这是一个模板文件
    内部可以使用 EJS 模板标记输出数据
    例如:Hello Robert~
    
    其他 EJS 语法也支持
    

接收用户输入

  • 模板文件
    // generator-sample/generators/app/templates/bar.txt
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title><%= name %></title>
    </head>
    <body>
      <h1><%= name %></h1>
    </body>
    </html>
    
  • 编写代码
    // generator-sample/generators/app/templates/index.js
    const Generator = require('yeoman-generator')
    
    module.exports = class extends Generator {
      prompting() {
    	// Yeoman 在询问用户环节会自动调用此方法
    	// 在此方法中可以调用父类的 prompt() 方法发出对用户的命令行询问
    	return this.prompt([
    	  {
    		type: 'input',
    		name: 'name',
    		message: 'Your project name',
    		default: this.appname // appname 为项目生成目录名称
    	  }
    	])
    	.then(answers => {
    	  // answers => { name: 'user input value' }
    	  this.answers = answers
    	})
      }
    
      writing() {
    	// 通过模板方式写入文件到目标目录(根据模板创建文件)
    
    	// 模板文件路径
    	const tmpl = this.templatePath('bar.html')
    	// 输出目标路径
    	const output = this.destinationPath('bar.html')
    	// 模板数据上下文
    	const context = this.answers
    
    	this.fs.copyTpl(tmpl, output, context)
      }
    }
    
  • 运行模块
    // d:\robert\Desktop\my-proj
    yo sample
    
  • 生成的文件
    // d:\robert\Desktop\my-proj\bar.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>my-project</title>
    </head>
    <body>
      <h1>my-project</h1>
    </body>
    </html>
    

Vue Generator 案例

  • 初始化
    image
  • 编写代码
    // generator-dwy-vue/generators/app/index.js
    const Generator = require('yeoman-generator')
    
    module.exports = class extends Generator {
      prompting () {
    	return this.prompt([
    	  {
    		type: 'input',
    		name: 'name',
    		message: 'Your project name',
    		default: this.appname
    	  }
    	])
    	.then(answers => {
    	  this.answers = answers
    	})
      }
    
      writing () {
    	// 把每一个文件都通过模板转换到目标路径
    
    	const templates = [
    	  '.browserslistrc',
    	  '.editorconfig',
    	  '.env.development',
    	  '.env.production',
    	  '.eslintrc.js',
    	  '.gitignore',
    	  'babel.config.js',
    	  'package.json',
    	  'postcss.config.js',
    	  'README.md',
    	  'public/favicon.ico',
    	  'public/index.html',
    	  'src/App.vue',
    	  'src/main.js',
    	  'src/router.js',
    	  'src/assets/logo.png',
    	  'src/components/HelloWorld.vue',
    	  'src/store/actions.js',
    	  'src/store/getters.js',
    	  'src/store/index.js',
    	  'src/store/mutations.js',
    	  'src/store/state.js',
    	  'src/utils/request.js',
    	  'src/views/About.vue',
    	  'src/views/Home.vue'
    	]
    
    	templates.forEach(item => {
    	  // item => 每个文件路径
    	  this.fs.copyTpl(
    		this.templatePath(item),
    		this.destinationPath(item),
    		this.answers
    	  )
    	})
      }
    }
    
  • 准备模板文件
    image
  • 模板文件挖坑(需要使用用户输入的内容替换的 EJS 标记)
    image
    image
  • 设置成全局模块 => yarn link
  • 使用 dwy-vue 模块
    • 先定位到全新目录
    • yo dwy-vue
      • 解决 BASE_URL is not defined
        image
      • 原封不动的输出 EJS 标记
        <!-- 原封不动输出 EJS 模板标记的方式:<% 后面 + % -->
        <link rel="icon" href="<%%= BASE_URL %>favicon.ico">
        

发布 Generator

  • 托管到公开的源代码仓库
    1. 创建 gitignore,echo node_modules > .gitignore
    2. git init
    3. git status
    4. git add .
    5. git commit -m "feat: initial commit"
    6. 同步到远端仓库(git remote add origin https://github.com/...)
    7. git push -u origin master/main
  • 创建 github 仓库
    image
    image
  • 发布到 npm
    yarn publish
    • 如配置了淘宝镜像,可能会出现以下错误,因为淘宝镜像是只读的
      image
    • 解决办法
      yarn publish --registry=https://registry.yarnpkg.com
  • 更新自己发布的 npm 包,参考
    • yarn version patch
    • yarn pubilsh

标签:src,Generator,generator,js,sample,脚手架,模板,自定义
来源: https://www.cnblogs.com/dwyWeb/p/16435519.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有