当前位置:网站首页>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
效果:
边栏推荐
- Using macro code to generate handwriting automatically in word or WPS
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- 108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
- 51单片机多机通信
- 4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
- 指南针炒股软件怎么样?安全吗?
- Text border format and text block of rich text
- The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
- 【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
- A website for programmers with a monthly salary of 30K
猜你喜欢

Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022

LLVM TargetPassConfig

Scala IO read by character

Scala IO reads by lexical units and numbers

丹麦技术大学首创将量子计算应用于能源系统潮流建模

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

汇编语言(4)函数传参

How to store dataframe data in pandas into MySQL
最新QQ微信域名防红PHP程序源码+强制跳转打开

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
随机推荐
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
Text editor for QT project practice -- Episode 9
LLVM TargetPassConfig
bindservice方法实现音乐播放暂停
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
网上开户选哪个证券公司?网上开户安全么?
Mobile security tool jarsigner
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
51 single chip microcomputer multi computer communication
腾讯云WeCity解决方案
[microservices sentinel] cluster link | microservices cluster environment construction
[redis realizes seckill service ②] solution to oversold problem
Introduction to smart contract security audit delegatecall (2)
腾讯云WeCity丨你好 2022!
Cobalt strike installation tutorial
2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
Scala template method pattern
Syntax highlighting of rich text
汇编语言(4)函数传参
Activity lifecycle