Skip to content

Commit

Permalink
feat(tool): ✨ 添加微型代码生成器plop
Browse files Browse the repository at this point in the history
可通过`npm run plop`生成指定类型代码,例如:视图代码 或 组件代码
  • Loading branch information
ShanYi-Hui committed Apr 7, 2024
1 parent e03c8d2 commit 124dee0
Show file tree
Hide file tree
Showing 8 changed files with 1,799 additions and 69 deletions.
1,752 changes: 1,683 additions & 69 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"format": "prettier --write src/",
"test:unit": "vitest",
"prepare": "husky",
"plop": "plop",
"release": "standard-version"
},
"dependencies": {
Expand Down Expand Up @@ -69,6 +70,7 @@
"less": "^4.2.0",
"lint-staged": "^15.2.2",
"npm-run-all2": "^6.1.2",
"plop": "^4.0.1",
"postcss-html": "^1.6.0",
"postcss-less": "^6.0.0",
"prettier": "^3.2.5",
Expand Down
6 changes: 6 additions & 0 deletions plop/component/component.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script setup lang="ts">
</script>

<template>
<div class="{{ kebabName }}">{{ upperFirstName }}</div>
</template>
3 changes: 3 additions & 0 deletions plop/component/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {{ upperFirstName }} from './src/{{ upperFirstName }}.vue'

export { {{ upperFirstName }} }
50 changes: 50 additions & 0 deletions plop/component/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const toUpperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1)

export default {
description: '创建组件',
prompts: [
{
type: 'input',
name: 'name',
message: '请输入组件名称',
default: 'NewComponent',
validate: (value) => {
if (/^[A-Za-z]+$/.test(value)) {
return true
}
return '组件名称只能为英文字符'
}
}
],
actions: (data) => {
const { name } = data
const upperFirstName = toUpperCase(name)
const kebabName = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()

const actions = []
if (name) {
actions.push(
{
type: 'add',
path: `./src/components/${upperFirstName}/src/${upperFirstName}.vue`,
templateFile: './plop/component/component.hbs',
data: {
name,
upperFirstName,
kebabName
}
},
{
type: 'add',
path: `./src/components/${upperFirstName}/index.ts`,
templateFile: './plop/component/index.hbs',
data: {
upperFirstName
}
}
)
}

return actions
}
}
40 changes: 40 additions & 0 deletions plop/view/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const toUpperCase = (str) => str.charAt(0).toUpperCase() + str.slice(1)

export default {
description: '创建视图',
prompts: [
{
type: 'input',
name: 'path',
message: '请输入路径',
default: 'views/NewView'
},
{
type: 'input',
name: 'name',
message: '请输入模块名称',
default: 'NewPage'
}
],
actions: (data) => {
const { name, path } = data
const upperFirstName = toUpperCase(name)
const kebabName = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()

const actions = []
if (name) {
actions.push({
type: 'add',
path: `./src/${path}/${upperFirstName}.vue`,
templateFile: './plop/view/view.hbs',
data: {
name,
upperFirstName,
kebabName
}
})
}

return actions
}
}
7 changes: 7 additions & 0 deletions plop/view/view.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
</script>

<template>
<ContentWrap class="{{ kebabName }}" title="{{ upperFirstName }}"> {{ name }} </ContentWrap>
</template>
8 changes: 8 additions & 0 deletions plopfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import viewGenerator from './plop/view/prompt.js'
import componentGenerator from './plop/component/prompt.js'

export default function (plop) {
plop.setWelcomeMessage('请选择代码生成器类型')
plop.setGenerator('view', viewGenerator)
plop.setGenerator('component', componentGenerator)
}

0 comments on commit 124dee0

Please sign in to comment.