Skip to content

Commit 350b371

Browse files
authored
Merge pull request #22 from Henriquecesp/feat/hen-add-react
Feat/hen add react
2 parents 78c1ae1 + d4d84d6 commit 350b371

File tree

72 files changed

+321
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+321
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions

app/index.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,52 @@ class SecGenerator extends Generator {
4141
name: 'framework',
4242
message: 'Select your framework:',
4343
choices: [
44+
{ name: 'NestJS', value: 'nestjs' },
45+
{ name: 'ReactJS', value: 'reactjs' },
4446
{ name: 'Laravel', value: 'laravel' },
45-
{ name: 'NestJS TypeORM', value: 'nestjsTypeOrm' },
47+
],
48+
},
49+
{
50+
when: answers => answers.framework === 'reactjs',
51+
type: 'list',
52+
name: 'template',
53+
message: 'Select your template:',
54+
choices: [
55+
{ name: 'ReactJS Hook', value: 'reactjsHook' },
56+
{ name: 'ReactJS Component', value: 'reactjsComponent' },
57+
{ name: 'ReactJS ContextAPI', value: 'reactjsContext' },
58+
{ name: 'ReactJS Material-UI', value: 'reactjsMui' },
59+
{ name: 'ReactJS StyledComponents', value: 'reactjsStyled' },
60+
],
61+
},
62+
{
63+
when: answers => answers.framework === 'nestjs',
64+
type: 'list',
65+
name: 'template',
66+
message: 'Select your template:',
67+
choices: [
68+
{ name: 'NestJS TypeOrm', value: 'nestjsTypeOrm' },
4669
{ name: 'NestJS Mongoose', value: 'nestjsMongoose' },
47-
{ name: 'NestJS PrismaORM', value: 'nestjsPrismaOrm' },
70+
{ name: 'NestJS PrismaOrm', value: 'nestjsPrismaOrm' },
4871
],
4972
},
73+
{
74+
when: answers => answers.framework === 'laravel',
75+
type: 'list',
76+
name: 'template',
77+
message: 'Select your template:',
78+
choices: [{ name: 'Default', value: 'default' }],
79+
},
5080
])
5181
}
5282

5383
async writing() {
54-
const framework = this.answers.framework
55-
const dir = path.join(__dirname, `templates/${framework}/`)
84+
const template = `${this.answers.framework}/${this.answers.template}`
85+
const dir = path.join(__dirname, `templates/${template}/`)
5686

5787
for await (const f of makeFileTemplate(dir, this.variables.name)) {
5888
this.fs.copyTpl(
59-
this.templatePath(`${framework}/${f.src}`),
89+
this.templatePath(`${template}/${f.src}`),
6090
this.destinationPath(`${this.variables.path}/${f.dist}`),
6191
this.variables,
6292
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
interface Props {
4+
title: string;
5+
}
6+
7+
const <%= namePascal %> = ({title}: Props): JSX.Element => {
8+
const [data, setData] = React.useState<boolean>(false)
9+
10+
const handleClick = React.useCallback(() => {
11+
setData(data => !data)
12+
}, []);
13+
14+
return (
15+
<div>
16+
<h1>{title}</h1>
17+
<button onClick={handleClick}>Click Me!</button>
18+
</div>
19+
);
20+
}
21+
22+
export default <%= namePascal %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './'
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { createContext, useContext, useReducer } from 'react';
2+
3+
interface <%= namePascal %> {
4+
id: number;
5+
title: string;
6+
}
7+
8+
interface <%= namePascal %>State {
9+
<%= namePluralCamel %>: <%= namePascal %>[];
10+
}
11+
12+
interface Action {
13+
type: 'ADD' | 'DELETE';
14+
<%= nameCamel %>: <%= namePascal %>;
15+
id: string;
16+
}
17+
18+
const <%= namePascal %>StateContext = createContext<<%= namePascal %>State>({
19+
<%= nameCamel %>: [],
20+
});
21+
22+
const <%= namePascal %>DispatchContext = createContext<React.Dispatch<Action>>(
23+
null as unknown as React.Dispatch<Action>,
24+
);
25+
26+
function <%= namePascal %>Reducer(state: <%= namePascal %>State, action: Action) {
27+
switch (action.type) {
28+
case 'ADD': {
29+
return {
30+
...state,
31+
<%= nameCamel %>: [...state.<%= namePluralCamel %>, action.<%= nameCamel %>],
32+
};
33+
}
34+
case 'DELETE': {
35+
const updated<%= namePascal %> = state.<%= namePluralCamel %>.filter((item) => item.id != action.id);
36+
return {
37+
...state,
38+
<%= namePluralCamel %>: updated<%= namePascal %>,
39+
};
40+
}
41+
default: {
42+
throw new Error('unhandled action type');
43+
}
44+
}
45+
}
46+
47+
interface <%= namePascal %>Provider {
48+
children: React.ReactNode;
49+
}
50+
51+
/*
52+
Use the <%= namePascal %>ContextProvider to wrap your app and set up the <%= namePascal %>StateContext and <%= namePascal %>DispatchContext.
53+
*/
54+
55+
export function <%= namePascal %>ContextProvider({ children }: <%= namePascal %>Provider): JSX.Element {
56+
const [state, dispatch] = useReducer(<%= namePascal %>Reducer, {
57+
<%= namePluralPascal %>: [],
58+
});
59+
60+
return (
61+
<<%= namePascal %>StateContext.Provider value={state}>
62+
<<%= namePascal %>DispatchContext.Provider value={dispatch}>
63+
{children}
64+
</<%= namePascal %>DispatchContext.Provider>
65+
</<%= namePascal %>StateContext.Provider>
66+
);
67+
}
68+
69+
export const use<%= namePascal %>DispatchContext = (): React.Dispatch<Action> =>
70+
useContext(<%= namePascal %>DispatchContext);
71+
export const use<%= namePascal %>StateContext = (): <%= namePascal %>State => useContext(<%= namePascal %>StateContext);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Reducer, useReducer } from 'react';
2+
3+
const <%= namePascal %>Reducer = (state: boolean, nextValue?: any) =>
4+
typeof nextValue === 'boolean' ? nextValue : !state;
5+
6+
const use<%= namePascal %> = (initialValue: boolean): [boolean, (nextValue?: any) => void] => {
7+
return useReducer<Reducer<boolean, any>>(<%= namePascal %>Reducer, initialValue);
8+
};
9+
10+
export default use<%= namePascal %>;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as React from 'react';
2+
import { styled } from '@mui/material/styles';
3+
import Button from '@mui/material/Button';
4+
import Stack from '@mui/material/Stack';
5+
import { purple } from '@mui/material/colors';
6+
7+
const ColorButton = styled(Button)(({ theme }) => ({
8+
color: theme.palette.getContrastText(purple[500]),
9+
backgroundColor: purple[500],
10+
'&:hover': {
11+
backgroundColor: purple[700],
12+
},
13+
}));
14+
15+
interface Props {
16+
title: string;
17+
}
18+
19+
const <%= namePascal %> = ({title}: Props): JSX.Element => {
20+
const [data, setData] = React.useState<boolean>(false)
21+
22+
const handleClick = React.useCallback(() => {
23+
setData(data => !data)
24+
}, []);
25+
26+
return (
27+
<Stack spacing={2} direction="row">
28+
<ColorButton variant="contained" onClick={handleClick}>
29+
{title}
30+
</ColorButton>
31+
</Stack>
32+
);
33+
}
34+
35+
export default <%= namePascal %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import styled from 'styled-components'
3+
4+
const Button = styled.button`
5+
/* ... */
6+
`
7+
8+
interface Props {
9+
title: string;
10+
}
11+
12+
const <%= namePascal %> = ({title}: Props): JSX.Element => {
13+
const [data, setData] = React.useState<boolean>(false)
14+
15+
const handleClick = React.useCallback(() => {
16+
setData(data => !data)
17+
}, []);
18+
19+
return (
20+
<div>
21+
<Button onClick={handleClick}>
22+
{title}
23+
</Button>
24+
</div>
25+
);
26+
}
27+
28+
export default <%= namePascal %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './'

package-lock.json

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "generator-secjs",
3-
"version": "1.2.9",
3+
"version": "1.3.0",
44
"description": "🧬 Generator for any NodeJS Project or Framework",
55
"main": "app/index.js",
66
"scripts": {

tests/laravel.spec.js renamed to tests/laravel/default.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
77
describe('\n Laravel 😸', () => {
88
beforeAll(() => {
99
return helpers
10-
.run(path.join(__dirname, '../app'))
10+
.run(path.join(__dirname, '../../app'))
1111
.withOptions({ path: './Foo' })
1212
.withArguments('Bar')
13-
.withPrompts({ framework: 'laravel' })
13+
.withPrompts({ framework: 'laravel', template: 'default' })
1414
})
1515

1616
it('should create all files from resource Bar in folder Foo', () => {

tests/nestjsMongoose.spec.js renamed to tests/nestjs/nestjsMongoose.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
77
describe('\n NestJS Mongoose 😸', () => {
88
beforeAll(() => {
99
return helpers
10-
.run(path.join(__dirname, '../app'))
10+
.run(path.join(__dirname, '../../app'))
1111
.withOptions({ path: './Foo' })
1212
.withArguments('Bar')
13-
.withPrompts({ framework: 'nestjsMongoose' })
13+
.withPrompts({ framework: 'nestjs', template: 'nestjsMongoose' })
1414
})
1515

1616
it('should create all files from resource Bar in folder Foo', () => {

tests/nestjsPrismaOrm.spec.js renamed to tests/nestjs/nestjsPrismaOrm.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
77
describe('\n NestJS PrismaORM 😸', () => {
88
beforeAll(() => {
99
return helpers
10-
.run(path.join(__dirname, '../app'))
10+
.run(path.join(__dirname, '../../app'))
1111
.withOptions({ path: './Foo' })
1212
.withArguments('Bar')
13-
.withPrompts({ framework: 'nestjsPrismaOrm' })
13+
.withPrompts({ framework: 'nestjs', template: 'nestjsPrismaOrm' })
1414
})
1515

1616
it('should create all files from resource Bar in folder Foo', () => {

tests/nestjsTypeOrm.spec.js renamed to tests/nestjs/nestjsTypeOrm.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
77
describe('\n NestJS TypeORM 😸', () => {
88
beforeAll(() => {
99
return helpers
10-
.run(path.join(__dirname, '../app'))
10+
.run(path.join(__dirname, '../../app'))
1111
.withOptions({ path: './Foo' })
1212
.withArguments('Bar')
13-
.withPrompts({ framework: 'nestjsTypeOrm' })
13+
.withPrompts({ framework: 'nestjs', template: 'nestjsTypeOrm' })
1414
})
1515

1616
it('should create all files from resource Bar in folder Foo', () => {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const helpers = require('yeoman-test')
5+
const assert = require('yeoman-assert')
6+
7+
describe('\n ReactJS Component ⚛️', () => {
8+
beforeAll(() => {
9+
return helpers
10+
.run(path.join(__dirname, '../../app'))
11+
.withOptions({ path: './Foo' })
12+
.withArguments('Bar')
13+
.withPrompts({ framework: 'reactjs', template: 'reactjsComponent' })
14+
})
15+
16+
it('should create all files from resource Bar in folder Foo', () => {
17+
assert.file([
18+
'./Foo/components/Bar/component.tsx',
19+
'./Foo/components/Bar/index.tsx',
20+
])
21+
})
22+
})

tests/reactjs/reactjsContext.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const helpers = require('yeoman-test')
5+
const assert = require('yeoman-assert')
6+
7+
describe('\n ReactJS Context ⚛️', () => {
8+
beforeAll(() => {
9+
return helpers
10+
.run(path.join(__dirname, '../../app'))
11+
.withOptions({ path: './Foo' })
12+
.withArguments('Bar')
13+
.withPrompts({ framework: 'reactjs', template: 'reactjsContext' })
14+
})
15+
16+
it('should create all files from resource Bar in folder Foo', () => {
17+
assert.file(['./Foo/contexts/BarContext.tsx'])
18+
})
19+
})

tests/reactjs/reactjsHook.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const helpers = require('yeoman-test')
5+
const assert = require('yeoman-assert')
6+
7+
describe('\n ReactJS Hook ⚛️', () => {
8+
beforeAll(() => {
9+
return helpers
10+
.run(path.join(__dirname, '../../app'))
11+
.withOptions({ path: './Foo' })
12+
.withArguments('Bar')
13+
.withPrompts({ framework: 'reactjs', template: 'reactjsHook' })
14+
})
15+
16+
it('should create all files from resource Bar in folder Foo', () => {
17+
assert.file(['./Foo/hooks/useBar.tsx'])
18+
})
19+
})

0 commit comments

Comments
 (0)