Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 5404456

Browse files
committed
更新全局组件存放目录,更新plop模版
1 parent f094b11 commit 5404456

File tree

12 files changed

+78
-50
lines changed

12 files changed

+78
-50
lines changed

plop-templates/component/index.hbs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<template>
22
<div>
3-
{{#if isGlobal}}
4-
<!-- 全局组件:<{{ properCase name }} /> -->
5-
{{else}}
6-
<!-- 组件:<{{ properCase name }} /> -->
7-
{{/if}}
3+
<!-- 组件内容区 -->
84
</div>
95
</template>
106

@@ -17,7 +13,6 @@ export default {
1713
data() {
1814
return {}
1915
},
20-
created() {},
2116
mounted() {},
2217
methods: {}
2318
}

plop-templates/component/prompt.js

+38-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1+
const fs = require('fs')
2+
3+
function getFolder(path) {
4+
let components = []
5+
const files = fs.readdirSync(path)
6+
files.forEach(function(item) {
7+
let stat = fs.lstatSync(path + '/' + item)
8+
if (stat.isDirectory() === true && item != 'components') {
9+
components.push(path + '/' + item)
10+
components.push.apply(components, getFolder(path + '/' + item))
11+
}
12+
})
13+
return components
14+
}
15+
116
module.exports = {
217
description: '创建组件',
318
prompts: [
19+
{
20+
type: 'confirm',
21+
name: 'isGlobal',
22+
message: '是否为全局组件',
23+
default: false
24+
},
25+
{
26+
type: 'list',
27+
name: 'path',
28+
message: '请选择组件创建目录',
29+
choices: getFolder('src/views'),
30+
when: answers => {
31+
return !answers.isGlobal
32+
}
33+
},
434
{
535
type: 'input',
636
name: 'name',
@@ -12,25 +42,20 @@ module.exports = {
1242
return true
1343
}
1444
}
15-
},
16-
{
17-
type: 'confirm',
18-
name: 'isGlobal',
19-
message: '是否为全局组件',
20-
default: false
2145
}
2246
],
2347
actions: data => {
24-
const name = '{{properCase name}}'
48+
let path = ''
49+
if (data.isGlobal) {
50+
path = 'src/components/{{properCase name}}/index.vue'
51+
} else {
52+
path = `${data.path}/components/{{properCase name}}/index.vue`
53+
}
2554
const actions = [
2655
{
2756
type: 'add',
28-
path: `src/components/${data.isGlobal ? 'global/' : ''}${name}/index.vue`,
29-
templateFile: 'plop-templates/component/index.hbs',
30-
data: {
31-
name: data.name,
32-
isGlobal: data.isGlobal
33-
}
57+
path: path,
58+
templateFile: 'plop-templates/component/index.hbs'
3459
}
3560
]
3661
return actions

plop-templates/page/index.hbs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<template>
22
<div>
3-
{{#if isLayout}}
4-
<!-- 布局页面:{{ properCase name }} -->
5-
<RouterView />
6-
{{else}}
7-
<!-- 页面:{{ properCase name }} -->
8-
{{/if}}
3+
<page-header title="{{ cname }}" />
4+
<page-main>
5+
页面内容区域
6+
</page-main>
97
</div>
108
</template>
119

1210
<script>
1311
export default {
14-
props: {},
12+
name: '{{ properCase componentName }}',
1513
data() {
1614
return {}
1715
},
18-
created() {},
1916
mounted() {},
2017
methods: {}
2118
}

plop-templates/page/prompt.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
4+
function getFolder(path) {
5+
let components = []
6+
const files = fs.readdirSync(path)
7+
files.forEach(function(item) {
8+
let stat = fs.lstatSync(path + '/' + item)
9+
if (stat.isDirectory() === true && item != 'components') {
10+
components.push(path + '/' + item)
11+
components.push.apply(components, getFolder(path + '/' + item))
12+
}
13+
})
14+
return components
15+
}
16+
117
module.exports = {
218
description: '创建页面',
319
prompts: [
420
{
521
type: 'list',
6-
name: 'type',
7-
message: '请选择页面类型',
8-
choices: ['view', 'layout'],
9-
default: 'view'
10-
},
11-
{
12-
type: 'input',
13-
name: 'viewPath',
14-
message: '请输入页面存放路径(./views/???)',
15-
when: answers => {
16-
return answers.type == 'view'
17-
}
22+
name: 'path',
23+
message: '请选择页面创建目录',
24+
choices: getFolder('src/views')
1825
},
1926
{
2027
type: 'input',
2128
name: 'name',
22-
message: '请输入页面名称',
23-
default: 'index',
29+
message: '请输入文件名',
2430
validate: v => {
2531
if (!v || v.trim === '') {
26-
return '页面名称不能为空'
32+
return '文件名不能为空'
2733
} else {
2834
return true
2935
}
3036
}
37+
},
38+
{
39+
type: 'input',
40+
name: 'cname',
41+
message: '请输入页面中文名称',
42+
default: '默认页面'
3143
}
3244
],
3345
actions: data => {
34-
const path = data.type == 'view' ? `views/${data.viewPath}` : 'layout'
46+
let relativePath = path.relative('src/views', data.path)
3547
const actions = [
3648
{
3749
type: 'add',
38-
path: `src/${path}/${data.name}.vue`,
50+
path: `${data.path}/{{dotCase name}}.vue`,
3951
templateFile: 'plop-templates/page/index.hbs',
4052
data: {
41-
name: data.name,
42-
isLayout: data.type == 'layout'
53+
componentName: `${relativePath} ${data.name}`
4354
}
4455
}
4556
]

plop-templates/store/prompt.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module.exports = {
55
type: 'input',
66
name: 'name',
77
message: '请输入模块名称',
8-
default: 'index',
98
validate: v => {
109
if (!v || v.trim === '') {
1110
return '模块名称不能为空'

plopfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = function(plop) {
2+
plop.setWelcomeMessage('请选择需要创建的模式:')
23
plop.setGenerator('page', require('./plop-templates/page/prompt'))
34
plop.setGenerator('component', require('./plop-templates/component/prompt'))
45
plop.setGenerator('store', require('./plop-templates/store/prompt'))

src/components/autoRegister.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
22
* 全局组件自动注册
33
*
4-
* 全局组件统一放在 ./global 目录下,需要注意各个组件按文件夹区分,文件夹名称与组件名无关联
4+
* 全局组件各个组件按文件夹区分,文件夹名称与组件名无关联,但建议与组件名保持一致
55
* 文件夹内至少保留一个文件名为 index 的组件入口,例如 index.vue
66
* 普通组件必须设置 name 并保证其唯一,自动注册会将组件的 name 设为组件名,可参考 SvgIcon 组件写法
77
* 如果组件是通过 js 进行调用,则确保组件入口文件为 index.js,可参考 ExampleNotice 组件
88
*/
99

1010
import Vue from 'vue'
1111

12-
const componentsContext = require.context('./global', true, /index.(vue|js)$/)
12+
const componentsContext = require.context('./', true, /index.(vue|js)$/)
1313
componentsContext.keys().forEach(file_name => {
1414
// 获取文件中的 default 模块
1515
const componentConfig = componentsContext(file_name).default

src/views/example/component.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</template>
77

88
<script>
9-
import ExampleList from '@/components/ExampleList'
9+
import ExampleList from './components/ExampleList/index'
1010
1111
export default {
1212
components: {

0 commit comments

Comments
 (0)