0.1.9 • Published 5 years ago

template-generation v0.1.9

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

template-generation

根据模板文件自动生成新文件,减少无用的重复劳动力

只需编写模板文件(支持简易的模板语法),配置需要的变量,即可快速生成新的模板/页面文件,提高开发效率

Install

$ npm i template-generation --dev
// or
$ yarn add template-generation --dev

Usage

引入:

const Generator = require('template-generation')

example

// script.js
new Generator({
  promptList: [
    {
      type: 'input',
      message: '请输入该页面的标题',
      name: 'title'
    }
  ],
  outputName: '',
  outputPath: './demo',
  templatePath: './test'
})
// package.json
"scripts": {
  "new": "node ./script.js"
},

params

Generator构造函数接受一个配置对象,配置对象有四个参数

  • 第一个参数是 Inquirer.js 的问题数组
  • 第二个参数是输出文件名(仅在输入模板 templatePath 为文件时有效)
  • 第三个参数是输出文件路径(输入为相对路径时,是相对于运行脚本的位置)
  • 第四个参数是模板文件路径

模板文件可以使用模板语法: 其中注意插入的变量名要与 Inquirer.js 的问题数组变量名相对应

// 插入变量名(一定要添加this开头)
<%= this.title %>

// 插入代码
<% if( this.title  ) {%>
const c = 10
<%}%>
0.1.9

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago