当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Basic knowledge of assembly language (2) -debug

Powerbi - for you who are learning

Boutique enterprise class powerbi application pipeline deployment

Reading notes at night -- deep into virtual function

Bi-sql between

新一代可级联的以太网远程I/O数据采集模块

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?

Bi SQL constraints

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

Abnova丨CSV 磁珠中英文说明
随机推荐
Yasea APK Download Image
Tianshu night reading notes -- disassembly engine xde32
2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
汇编语言(2)基础知识-debug
Scala template method pattern
Syntax highlighting of rich text
MySQL gets the primary key and table structure of the table
PMP考试“临门一脚”如何踢得漂亮?
The optimism about technology is making Dell achieve more than expected
生态护航 云服务商挥起“英特尔大旗”
腾讯云WeCity解决方案
Bi-sql index
Bi-sql create
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
新一代可级联的以太网远程I/O数据采集模块
利用 Redis 的 sorted set 做每周热评的功能
How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
Convolution and transpose convolution
How about compass stock trading software? Is it safe?