美文网首页
yeoman自定义脚手架工具

yeoman自定义脚手架工具

作者: 风小逸 | 来源:发表于2021-06-30 18:09 被阅读0次

yeoman的安装和基础使用

  1. 安装yeoman
yarn global add yo
  1. 安装对应的generator
yarn add generator-node global
  1. 通过yo运行generator
yo node(generator的名字)

自定义generator

  1. 创建一个generator-开头的文件夹,并且初始化
mkdir generator-test
cd generator-test
yarn init
  1. 安装yeoman-generator模块
yarn add yeoman-generator
  1. 在文件夹的generators/app/index.js中编写初始化操作
// 此文件作为 Generator 的核心入口
// 需要导出一个继承自 Yeoman Generator 的类型
// Yeoman Generator 在工作时会自动调用我们在此类型中定义的一些生命周期方法
// 我们在这些方法中可以通过调用父类提供的一些工具方法实现一些功能,例如文件写入

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 () {
    // Yeoman 自动在生成文件阶段调用此方法

    // // 我们这里尝试往项目目录中写入文件
    // this.fs.write(
    //   this.destinationPath('temp.txt'),
    //   Math.random().toString()
    // )

    // -------------------------------------------------------

    // // 通过模板方式写入文件到目标目录

    // // 模板文件路径
    // const tmpl = this.templatePath('foo.txt')
    // // 输出目标路径
    // const output = this.destinationPath('foo.txt')
    // // 模板数据上下文
    // const context = { title: 'Hello zce~', success: false }

    // this.fs.copyTpl(tmpl, output, context)

    // -------------------------------------------------------

    // 模板文件路径
    const tmpl = this.templatePath('bar.html')
    // 输出目标路径
    const output = this.destinationPath('bar.html')
    // 模板数据上下文
    const context = this.answers

    this.fs.copyTpl(tmpl, output, context)
  }
}

相关文章

  • 115.脚手架工作流程

    脚手架工作流程 通过yeoman来构建 自定义构建脚手架 自定义构建脚手架 通过命令行工具启动一个程序,获取用户输...

  • Yeoman

    Yeoman 脚手架生产工具

  • yeoman+webpack+react

    yeoman+webpack+react yeoman 脚手架工具,项目开始阶段,使用yeoman来生成项目的文件...

  • 前端工程化系列[07] Yeoman-generator的创建

    在Yeoman脚手架使用入门和Yeoman脚手架核心机制这两篇文章中已经对Yeoman脚手架工具的基本使用以及去核...

  • 自动化构建

    脚手架的作用 通用脚手架Yeoman Yeoman是一款脚手架工具,可以帮助开发人员创建项目的基础结构代码 yo是...

  • yeoman脚手架

    yeoman脚手架yeoman - 搭建自己的脚手架 - 1

  • Grunt - Yeoman

    什么是Yeoman? 现代 Web App 的脚手架工具 Yeoman 的作用 在 Web 项目的立项阶段,使用 ...

  • Yeoman 安装及使用

    Yeoman介绍 Yeoman主要有三部分组成:yo(脚手架工具)、grunt(构建工具)、bower(包管理器)...

  • 实现简单的vscode插件

    安装工具 脚手架工具 Yeoman 和 Generator-code cnpm install -g yo gen...

  • 使用yeoman-generator 构建自己的脚手架

    yeoman-generator yeoman-generator是一个构建脚手架的工具,如果你还不了解你自行go...

网友评论

      本文标题:yeoman自定义脚手架工具

      本文链接:https://www.haomeiwen.com/subject/hnusektx.html