当前位置:网站首页>1. 封装自己的脚手架 2.创建代码模块
1. 封装自己的脚手架 2.创建代码模块
2022-06-24 20:33:00 【yanling.zhang】
使用node脚本封装
使用yeoman-generator封装自己的脚手架
mkdir generator-ai-cli
npm init -y
npm install yeoman-generator --save
目录结构

mkdir generators/app/index.js
// 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
)
})
}
}
package.json
{
"name": "generator-ai-cli",
"version": "0.1.0",
"main": "index.js",
"author": "",
"license": "MIT",
"dependencies": {
"yeoman-generator": "^4.0.1"
}
}
- 准备模版文件,模版文件的存放路径:generators/app/templates
generators/app/templates
文件
- yarn link
- yo ai-cli
plop:使用plop创建代码模块
1.场景:使用plop创建代码模块
必须是用yarn,使用npm好像生成不成功。
- yarn install plop --save
- touch plopfile.js
plop的入口文件:
// Plop 入口文件,需要导出一个函数
// 此函数接收一个 plop 对象,用于创建生成器任务
module.exports = plop => {
/** setGenerator参数: component: 生成器的名字 {}: 生成器的配置 */
plop.setGenerator('component', {
description: 'create a component',
prompts: [
{
type: 'input', // // 输入方式
name: 'name', // 问题返回值的键
message: 'component name', // 屏幕上给出的提示
default: 'MyComponent', // 问题的默认答案
}
],
actions: [
{
type: 'add', // 代表添加文件
path: 'src/components/{
{name}}/{
{name}}.js', // 添加文件的具体路径
templateFile: 'plop-templates/component.hbs' // 添加文件的模版文件路径
},
{
type: 'add', // 代表添加文件
path: 'src/components/{
{name}}/{
{name}}.css',
templateFile: 'plop-templates/component.css.hbs'
},
{
type: 'add', // 代表添加文件
path: 'src/components/{
{name}}/{
{name}}.test.js',
templateFile: 'plop-templates/component.test.hbs'
}
]
})
}
模版文件:
// plop-templates/component.css.hbs
import React from 'react';
export default () => (
<div className="{
{name}}">
<h1>{
{
name}} Component</h1>
</div>
)
// plop-templates/component.css.hbs
.{
{
name}} {
}
// plop-templates/component.test.hbs
import React from 'react';
import ReactDOM from 'react-dom';
import {
{
name}} from './{
{name}}';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<{
{
name}} />, div);
ReactDOM.unmountComponentAtNode(div);
});
yarn plop component (生成器的名字)
执行命令:yarn plop component
效果:
边栏推荐
- Scala IO read by character
- 4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
- LLVM TargetPassConfig
- QT(35)-操作EXCEL-QXlsx-QAxObject
- Mobile security tool -dex2jar
- 图片旋转移动缩放渐变
- Ecological escort cloud service providers wave "Intel flag"
- Scala sample class case calculate
- How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
- 联想童夫尧:11倍于大势,我们一路攻城拔寨
猜你喜欢

Powerbi - for you who are learning

2022熔化焊接与热切割复训题库模拟考试平台操作

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
![[microservices sentinel] cluster link | microservices cluster environment construction](/img/75/3cc6abf1e72d651b082415db9db46c.png)
[microservices sentinel] cluster link | microservices cluster environment construction

I brush the question I - copy the linked list with random pointer

QT(35)-操作EXCEL-QXlsx-QAxObject

Databinding quick start (still using findviewbyid?)

2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination

15.线程同步的几种方法

The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
随机推荐
yasea apk 下载 镜像
[redis realizes seckill service ②] solution to oversold problem
Redis + Lua implementation of distributed interface current limiting
Using macro code to generate handwriting automatically in word or WPS
QT(36)-rapidjson解析嵌套的json
生成订单30分钟未支付,则自动取消,该怎么实现?
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
腾讯云WeCity丨产业联合 协同创新 共贺新春!
Mobile security tool -dex2jar
图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
Scala classes inherit multiple attributes
[practical series] full WiFi coverage at home
Thingsboard - rest API obtains and refreshes tokens
JS Chapter 1 Summary
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
EVM Brief
A website for programmers with a monthly salary of 30K
Sanic服务启动失败
Convert MySQL query timestamp to date format