当前位置:网站首页>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 :
边栏推荐
- 胰蛋白酶中英文说明书
- vb学习什么[通俗易懂]
- [untitled]
- Scala IO reads by lexical units and numbers
- MySQL multi condition matching fuzzy query
- 【实用系列】家内wifi全覆盖
- Cobalt Strike安装教程
- 汇编语言(3)16位汇编基础框架与加减循环
- Go language operators (under Lesson 8)
- Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
猜你喜欢

Deploy a production cluster using Loki microservice pattern

汇编语言(3)16位汇编基础框架与加减循环

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

Bi-sql - different join

Scala IO read by line

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

Bi skill - judge 0 and null

Rich text tables, lists, pictures

论文翻译 | RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds

天书夜读笔记——深入虚函数virtual
随机推荐
Reading notes at night -- deep into virtual function
【无标题】
Golang example renewal lock: redis+channel+sync Mutex
TC对象结构和简称
丹麦技术大学首创将量子计算应用于能源系统潮流建模
[practical series] full WiFi coverage at home
Default methods for Scala sample classes
胰蛋白酶中英文说明书
void* 指针
音频PCM数据计算声音分贝值,实现简单VAD功能
Bi-sql between
汇编语言(2)基础知识-debug
丹麥技術大學首創將量子計算應用於能源系統潮流建模
MySQL common basic statements (collation)
Boutique enterprise class powerbi application pipeline deployment
matlab 取整
Scala classes inherit multiple attributes
Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system
数组中关于sizeof()和strlen
VB learning notes