当前位置:网站首页>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
效果:
边栏推荐
- Syntax highlighting of rich text
- Mysql database Chapter 1 Summary
- Text editor of QT project practice ---------- episode 11
- EVM简略
- [microservices sentinel] sentinel quick start | building an image | starting the console
- 腾讯云WeCity丨产业联合 协同创新 共贺新春!
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
- 腾讯搬家了!
- activity生命周期
猜你喜欢

How to store dataframe data in pandas into MySQL

JS Chapter 1 Summary

Text editor for QT project practice - Episode 10

Library management system code source code (php+css+js+mysql) complete code source code

Deploy a production cluster using Loki microservice pattern

Custom control - round dot progress bar (imitating one key acceleration in security guard)

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版

Scala IO read by line
The latest QQ wechat domain name anti red PHP program source code + forced jump to open

移动安全工具-jar
随机推荐
使用 Loki 微服务模式部署生产集群
Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
Using macro code to generate handwriting automatically in word or WPS
How about compass stock trading software? Is it safe?
Rich text tables, lists, pictures
Distinguish between i++ and ++i seconds
【无标题】
51 single chip microcomputer multi computer communication
欢迎来到联想智能大屏的新世界
指南针炒股软件怎么样?安全吗?
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
Previous basic review
腾讯云WeCity解决方案
15.线程同步的几种方法
Is it reliable to open an account on the flush with a mobile phone? Is there any hidden danger in this way
扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
Scala sample class case calculate