当前位置:网站首页>1. package your own scaffold 2 Create code module
1. package your own scaffold 2 Create code module
2022-06-25 01:19:00 【yanling. zhang】
Use node Script encapsulation
Use yeoman-generator Encapsulate your own scaffolding
mkdir generator-ai-cli
npm init -y
npm install yeoman-generator --save
Directory structure

mkdir generators/app/index.js
// generators/app/index.js
const Generator = require('yeoman-generator')
module.exports = class extends Generator {
prompting () {
// Command line interaction mode
return this.prompt([
{
type: 'input',
name: 'name',
message: 'Your project name',
default: this.appname
}
])
.then(answers => {
this.answers = answers
})
}
writing () {
// Convert every file to the target path through the template
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 => Each file path
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"
}
}
- Prepare template file , The storage path of the template file :generators/app/templates
generators/app/templates
file
- yarn link
- yo ai-cli
plop: Use plop Create code modules
1. scene : Use plop Create code modules
It must be with yarn, Use npm It seems that the generation was unsuccessful .
- yarn install plop --save
- touch plopfile.js
plop Entry file :
// Plop Entrance file , You need to export a function
// This function receives a plop object , Used to create generator tasks
module.exports = plop => {
/** setGenerator Parameters : component: The name of the generator {}: Generator configuration */
plop.setGenerator('component', {
description: 'create a component',
prompts: [
{
type: 'input', // // Input mode
name: 'name', // Key of the return value of the problem
message: 'component name', // The prompt given on the screen
default: 'MyComponent', // The default answer to the question
}
],
actions: [
{
type: 'add', // Add representative file
path: 'src/components/{
{name}}/{
{name}}.js', // Add the specific path of the file
templateFile: 'plop-templates/component.hbs' // Add the template file path of the file
},
{
type: 'add', // Add representative file
path: 'src/components/{
{name}}/{
{name}}.css',
templateFile: 'plop-templates/component.css.hbs'
},
{
type: 'add', // Add representative file
path: 'src/components/{
{name}}/{
{name}}.test.js',
templateFile: 'plop-templates/component.test.hbs'
}
]
})
}
Template file :
// 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 ( The name of the generator )
Carry out orders :yarn plop component
effect :
边栏推荐
- 重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
- Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
- Scala responsibility chain pattern
- 卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
- Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
- 汇编语言(2)基础知识-debug
- 丹麦技术大学首创将量子计算应用于能源系统潮流建模
- 图片旋转移动缩放渐变
- Bi-sql create
- 天书夜读笔记——反汇编引擎xde32
猜你喜欢

Linux64Bit下安装MySQL5.6-不能修改root密码

Bi-sql between

15. several methods of thread synchronization

汇编语言(2)基础知识-debug

Bi-sql - different join

Bi-sql top

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)

Using macro code to generate handwriting automatically in word or WPS

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

QT electronic clock
随机推荐
AUTOCAD——两种延伸方式
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
Powerbi - for you who are learning
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
网上开户选哪个证券公司?网上开户安全么?
bindservice方法实现音乐播放暂停
Deep learning LSTM model for stock analysis and prediction
Bi-sql between
Go language operators (under Lesson 8)
MySQL common basic statements (collation)
PHP 利用getid3 获取mp3、mp4、wav等媒体文件时长等数据
mysql查询时间戳转换成日期格式
Bi-sql like
归并排序模板 & 理解
Start service 11111
Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse
腾讯完成全面上云 打造国内最大云原生实践
天书夜读笔记——反汇编引擎xde32
新一代可级联的以太网远程I/O数据采集模块
Using bindservice method to pause music playing