Skip to content

Commit 1001e08

Browse files
committed
feat: camelCase handling + index for default export
1 parent 8e73ea3 commit 1001e08

File tree

9 files changed

+87
-46
lines changed

9 files changed

+87
-46
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,32 @@ or
1919
### Usage
2020
You can substitute `yarn` with `npm run`
2121
- Create any components type using prompts:
22-
- `yarn create-component`
2322
- `npm run create-component`
23+
- `yarn run create-component`
2424
- Create styled components interactively using:
25-
- `yarn create-component`
2625
- `npm run create-component`
27-
- Or without prompt:
28-
- `yarn create-component <ComponentName> <stateless|class|pure> <targetDir> [styled] [story]`
29-
- `npm run create-component <ComponentName> <stateless|class|pure> <targetDir> [styled] [story]`
26+
- `yarn run create-component`
27+
- Or without any prompt:
28+
```
29+
npx create-component --
30+
--name <ComponentName>
31+
--componentType (stateless|class|pure)
32+
--componentCase (camel|kebab)
33+
--directory <targetDir>
34+
--story <Boolean>
35+
--syled <Boolean>
36+
```
3037
3138
### Examples
3239
33-
- `npx create-component -- --name PureComponent --directory src/components --comptype pure --styled true --story false` will generate a pure component without a story boook
34-
- `npx create-component avatar src/component/profile pure true true` will create a pure component `<Avatar />` in `src/component/profile` including test and story files
40+
- `npx create-component -- --name FunctionComponent --directory src/components --componentType stateless --styled true --story false --componentCase camel`
41+
will create a function component `<FunctionComponent />` in `src/component/profile` including test and story files
3542
3643
![screen recording](http://g.recordit.co/XMuQeSSrgy.gif)
3744
45+
### TODO
46+
- Handle typescript
47+
- Cypress template
48+
3849
Note: the template can generate unstyled component but you'll just have to remove styled component references.
3950
PR welcome for different handling templates with other conventions/libraries

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"author": "Yann VR",
2525
"license": "MIT",
2626
"dependencies": {
27+
"lodash": "^4.17.11",
2728
"plop": "^2.3.0"
2829
}
2930
}

plop-templates/component/index.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {{name}} from './{{name}}'
2+
3+
export default {{name}}

plopfile.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path')
2+
const { upperFirst, kebabCase, camelCase } = require('lodash')
23

34
module.exports = function(plop) {
45

@@ -23,10 +24,11 @@ module.exports = function(plop) {
2324
message: 'Target directory:',
2425
default: 'src/components',
2526
},
27+
2628
{
2729
type: 'list',
28-
name: 'comptype',
29-
message: 'What component type is it?',
30+
name: 'componentType',
31+
message: 'Component type?',
3032
choices: [
3133
{ name: 'stateless', value: 'stateless', checked: true },
3234
{ name: 'class', value: 'class' },
@@ -47,6 +49,17 @@ module.exports = function(plop) {
4749
message: 'Create story?',
4850
default: true,
4951
},
52+
53+
{
54+
type: 'list',
55+
name: 'componentCase',
56+
message: 'What component type is it?',
57+
choices: [
58+
{ name: 'camelCase', value: 'camel', checked: true },
59+
{ name: 'kebabCase', value: 'kebab' },
60+
],
61+
},
62+
5063
],
5164

5265
actions: function(data) {
@@ -55,53 +68,66 @@ module.exports = function(plop) {
5568
const targetDir = data['directory']
5669
const baseDir = cwd + '/' + targetDir
5770

58-
switch (data.comptype) {
71+
plop.setHelper('selectedComponentCase', function (text) {
72+
if (data.componentCase === 'kebab') {
73+
return kebabCase(text)
74+
}
75+
return upperFirst(camelCase(text))
76+
});
77+
78+
switch (data.componentType) {
5979
case 'class':
6080
actions.push({
6181
type: 'add',
62-
path: baseDir + '/{{kebabCase name}}/index.jsx',
63-
templateFile: 'plop-templates/component/index.class-component.hbs',
82+
path: baseDir + '/{{selectedComponentCase name}}/{{selectedComponentCase name}}.jsx',
83+
templateFile: 'plop-templates/component/component.class-component.hbs',
6484
})
6585
break
6686
case 'pure':
6787
actions.push({
6888
type: 'add',
69-
path: baseDir + '/{{kebabCase name}}/index.jsx',
70-
templateFile: 'plop-templates/component/index.pure-component.hbs',
89+
path: baseDir + '/{{selectedComponentCase name}}/{{selectedComponentCase name}}.jsx',
90+
templateFile: 'plop-templates/component/component.pure-component.hbs',
7191
})
7292
break
7393
case 'stateless':
7494
default:
7595
actions.push({
7696
type: 'add',
77-
path: baseDir + '/{{kebabCase name}}/index.jsx',
78-
templateFile: 'plop-templates/component/index.stateless-component.hbs',
97+
path: baseDir + '/{{selectedComponentCase name}}/{{selectedComponentCase name}}.jsx',
98+
templateFile: 'plop-templates/component/component.stateless-component.hbs',
7999
})
80100
break
81101
}
82102

83103
if (data.story) {
84104
actions.push({
85105
type: 'add',
86-
path: baseDir + '/{{kebabCase name}}/story.js',
106+
path: baseDir + '/{{selectedComponentCase name}}/story.js',
87107
templateFile: 'plop-templates/component/story.hbs',
88108
})
89109
}
90110

91111
if (data.styled) {
92112
actions.push({
93113
type: 'add',
94-
path: baseDir + '/{{kebabCase name}}/style.js',
114+
path: baseDir + '/{{selectedComponentCase name}}/style.js',
95115
templateFile: 'plop-templates/component/style.hbs',
96116
})
97117
}
98118

99119
actions.push({
100120
type: 'add',
101-
path: baseDir + '/{{kebabCase name}}/test.jsx',
121+
path: baseDir + '/{{selectedComponentCase name}}/test.jsx',
102122
templateFile: 'plop-templates/component/test.hbs',
103123
})
104124

125+
actions.push({
126+
type: 'add',
127+
path: baseDir + '/{{selectedComponentCase name}}/index.js',
128+
templateFile: 'plop-templates/component/index.hbs',
129+
})
130+
105131
return actions
106132
},
107133
})

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ pinkie@^2.0.0:
13491349
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
13501350
integrity sha1-clVrgM+g1IqXToDnckjoDtT3+HA=
13511351

1352-
plop@^2.1.0:
1352+
plop@^2.3.0:
13531353
version "2.3.0"
13541354
resolved "https://registry.yarnpkg.com/plop/-/plop-2.3.0.tgz#e9b3ccf9d8fdbce077ee7377b75fcc5f511df562"
13551355
integrity sha512-CfTMYmfeCwlipzVcDWI6edxRwdZx47d8BRL3VSv5g+BB8JcLXaSzATkU0flG/aIVl4cxe1Y0rGkrtPjpDm7ySQ==

0 commit comments

Comments
 (0)