当前位置:网站首页>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 :
边栏推荐
- 网上开户选哪个证券公司?网上开户安全么?
- 欢迎来到联想智能大屏的新世界
- MySQL multi condition matching fuzzy query
- Bi-sql Union
- 卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
- Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
- 归并排序模板 & 理解
- vb学习什么[通俗易懂]
- Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
- 音频PCM数据计算声音分贝值,实现简单VAD功能
猜你喜欢

Deep learning LSTM model for stock analysis and prediction

Bi SQL drop & alter

void* 指针

Use redis' sorted set to make weekly hot Reviews

Bi-sql index

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

Reading notes at night -- deep into virtual function

Assembly language (4) function transfer parameters

丹麥技術大學首創將量子計算應用於能源系統潮流建模
随机推荐
Go language operators (under Lesson 8)
Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
LLVM TargetPassConfig
新手看过来,带你一次性了解“软考”
yasea apk 下载 镜像
Tencent moved!
Novice, let me show you the "soft test" at one time
What to learn in VB [easy to understand]
Which securities company should I choose to open an account online? Is it safe to open an account online?
This national day! Tencent cloud wecity will accompany you to travel and light up the city landmark
Scala adapter pattern
The latest QQ wechat domain name anti red PHP program source code + forced jump to open
归并排序求逆序数
matlab 取整
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
MySQL common basic statements (collation)
Scala classes inherit multiple attributes
程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?
15. several methods of thread synchronization
指南针炒股软件怎么样?安全吗?