Skip to content

Commit 896b736

Browse files
authored
feature: add create template (PanJiaChen#1762)
1 parent a9b4467 commit 896b736

File tree

7 files changed

+181
-1
lines changed

7 files changed

+181
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"lint": "eslint --ext .js,.vue src",
1414
"test": "npm run lint",
1515
"test:unit": "vue-cli-service test:unit",
16-
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml"
16+
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml",
17+
"new": "plop"
1718
},
1819
"husky": {
1920
"hooks": {
@@ -87,6 +88,7 @@
8788
"lint-staged": "7.2.2",
8889
"mockjs": "1.0.1-beta3",
8990
"node-sass": "^4.9.0",
91+
"plop": "2.3.0",
9092
"runjs": "^4.3.2",
9193
"sass-loader": "^7.1.0",
9294
"script-ext-html-webpack-plugin": "2.1.3",

plop-templates/component/index.hbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{#if template}}
2+
<template>
3+
<div />
4+
</template>
5+
{{/if}}
6+
7+
{{#if script}}
8+
<script>
9+
export default {
10+
name: '{{ properCase name }}',
11+
props: {},
12+
data() {
13+
return {}
14+
},
15+
created() {},
16+
mounted() {},
17+
methods: {}
18+
}
19+
</script>
20+
{{/if}}
21+
22+
{{#if style}}
23+
<style lang="scss" scoped>
24+
25+
</style>
26+
{{/if}}

plop-templates/component/prompt.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { notEmpty } = require('../utils.js')
2+
3+
module.exports = {
4+
description: 'generate vue component',
5+
prompts: [{
6+
type: 'input',
7+
name: 'name',
8+
message: 'component name please',
9+
validate: notEmpty('name')
10+
},
11+
{
12+
type: 'checkbox',
13+
name: 'blocks',
14+
message: 'Blocks:',
15+
choices: [{
16+
name: '<template>',
17+
value: 'template',
18+
checked: true
19+
},
20+
{
21+
name: '<script>',
22+
value: 'script',
23+
checked: true
24+
},
25+
{
26+
name: 'style',
27+
value: 'style',
28+
checked: true
29+
}
30+
],
31+
validate(value) {
32+
if (value.indexOf('script') === -1 && value.indexOf('template') === -1) {
33+
return 'Components require at least a <script> or <template> tag.'
34+
}
35+
return true
36+
}
37+
}
38+
],
39+
actions: data => {
40+
const name = '{{properCase name}}'
41+
const actions = [{
42+
type: 'add',
43+
path: `src/components/${name}/index.vue`,
44+
templateFile: 'plop-templates/component/index.hbs',
45+
data: {
46+
name: name,
47+
template: data.blocks.includes('template'),
48+
script: data.blocks.includes('script'),
49+
style: data.blocks.includes('style')
50+
}
51+
}]
52+
53+
return actions
54+
}
55+
}

plop-templates/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports.notEmpty = name => {
2+
return v => {
3+
if (!v || v.trim === '') {
4+
return `${name} is required`
5+
} else {
6+
return true
7+
}
8+
}
9+
}

plop-templates/view/index.hbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{#if template}}
2+
<template>
3+
<div />
4+
</template>
5+
{{/if}}
6+
7+
{{#if script}}
8+
<script>
9+
export default {
10+
name: '{{ properCase name }}',
11+
props: {},
12+
data() {
13+
return {}
14+
},
15+
created() {},
16+
mounted() {},
17+
methods: {}
18+
}
19+
</script>
20+
{{/if}}
21+
22+
{{#if style}}
23+
<style lang="scss" scoped>
24+
25+
</style>
26+
{{/if}}

plop-templates/view/prompt.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { notEmpty } = require('../utils.js')
2+
3+
module.exports = {
4+
description: 'generate a view',
5+
prompts: [{
6+
type: 'input',
7+
name: 'name',
8+
message: 'view name please',
9+
validate: notEmpty('name')
10+
},
11+
{
12+
type: 'checkbox',
13+
name: 'blocks',
14+
message: 'Blocks:',
15+
choices: [{
16+
name: '<template>',
17+
value: 'template',
18+
checked: true
19+
},
20+
{
21+
name: '<script>',
22+
value: 'script',
23+
checked: true
24+
},
25+
{
26+
name: 'style',
27+
value: 'style',
28+
checked: true
29+
}
30+
],
31+
validate(value) {
32+
if (value.indexOf('script') === -1 && value.indexOf('template') === -1) {
33+
return 'View require at least a <script> or <template> tag.'
34+
}
35+
return true
36+
}
37+
}
38+
],
39+
actions: data => {
40+
const name = '{{name}}'
41+
const actions = [{
42+
type: 'add',
43+
path: `src/views/${name}/index.vue`,
44+
templateFile: 'plop-templates/view/index.hbs',
45+
data: {
46+
name: name,
47+
template: data.blocks.includes('template'),
48+
script: data.blocks.includes('script'),
49+
style: data.blocks.includes('style')
50+
}
51+
}]
52+
53+
return actions
54+
}
55+
}

plopfile.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const viewGenerator = require('./plop-templates/view/prompt')
2+
const componentGenerator = require('./plop-templates/component/prompt')
3+
4+
module.exports = function(plop) {
5+
plop.setGenerator('view', viewGenerator)
6+
plop.setGenerator('component', componentGenerator)
7+
}

0 commit comments

Comments
 (0)