Skip to content

Commit

Permalink
Merge branch 'main' of github.com:secjs/Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Nov 18, 2021
2 parents 0a91dd9 + 350b371 commit 1792a94
Show file tree
Hide file tree
Showing 72 changed files with 321 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ yo secjs Foo --path=./Bar

...

## License
---

MIT © [João Lenon](https://github.com/SecJS/Generator/blob/master/LICENSE)
Made with 🖤 by [jlenon7](https://github.com/jlenon7) :wave
40 changes: 35 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,52 @@ class SecGenerator extends Generator {
name: 'framework',
message: 'Select your framework:',
choices: [
{ name: 'NestJS', value: 'nestjs' },
{ name: 'ReactJS', value: 'reactjs' },
{ name: 'Laravel', value: 'laravel' },
{ name: 'NestJS TypeORM', value: 'nestjsTypeOrm' },
],
},
{
when: answers => answers.framework === 'reactjs',
type: 'list',
name: 'template',
message: 'Select your template:',
choices: [
{ name: 'ReactJS Hook', value: 'reactjsHook' },
{ name: 'ReactJS Component', value: 'reactjsComponent' },
{ name: 'ReactJS ContextAPI', value: 'reactjsContext' },
{ name: 'ReactJS Material-UI', value: 'reactjsMui' },
{ name: 'ReactJS StyledComponents', value: 'reactjsStyled' },
],
},
{
when: answers => answers.framework === 'nestjs',
type: 'list',
name: 'template',
message: 'Select your template:',
choices: [
{ name: 'NestJS TypeOrm', value: 'nestjsTypeOrm' },
{ name: 'NestJS Mongoose', value: 'nestjsMongoose' },
{ name: 'NestJS PrismaORM', value: 'nestjsPrismaOrm' },
{ name: 'NestJS PrismaOrm', value: 'nestjsPrismaOrm' },
],
},
{
when: answers => answers.framework === 'laravel',
type: 'list',
name: 'template',
message: 'Select your template:',
choices: [{ name: 'Default', value: 'default' }],
},
])
}

async writing() {
const framework = this.answers.framework
const dir = path.join(__dirname, `templates/${framework}/`)
const template = `${this.answers.framework}/${this.answers.template}`
const dir = path.join(__dirname, `templates/${template}/`)

for await (const f of makeFileTemplate(dir, this.variables.name)) {
this.fs.copyTpl(
this.templatePath(`${framework}/${f.src}`),
this.templatePath(`${template}/${f.src}`),
this.destinationPath(`${this.variables.path}/${f.dist}`),
this.variables,
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

interface Props {
title: string;
}

const <%= namePascal %> = ({title}: Props): JSX.Element => {
const [data, setData] = React.useState<boolean>(false)

const handleClick = React.useCallback(() => {
setData(data => !data)
}, []);

return (
<div>
<h1>{title}</h1>
<button onClick={handleClick}>Click Me!</button>
</div>
);
}

export default <%= namePascal %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './'
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createContext, useContext, useReducer } from 'react';

interface <%= namePascal %> {
id: number;
title: string;
}

interface <%= namePascal %>State {
<%= namePluralCamel %>: <%= namePascal %>[];
}

interface Action {
type: 'ADD' | 'DELETE';
<%= nameCamel %>: <%= namePascal %>;
id: string;
}

const <%= namePascal %>StateContext = createContext<<%= namePascal %>State>({
<%= nameCamel %>: [],
});

const <%= namePascal %>DispatchContext = createContext<React.Dispatch<Action>>(
null as unknown as React.Dispatch<Action>,
);

function <%= namePascal %>Reducer(state: <%= namePascal %>State, action: Action) {
switch (action.type) {
case 'ADD': {
return {
...state,
<%= nameCamel %>: [...state.<%= namePluralCamel %>, action.<%= nameCamel %>],
};
}
case 'DELETE': {
const updated<%= namePascal %> = state.<%= namePluralCamel %>.filter((item) => item.id != action.id);
return {
...state,
<%= namePluralCamel %>: updated<%= namePascal %>,
};
}
default: {
throw new Error('unhandled action type');
}
}
}

interface <%= namePascal %>Provider {
children: React.ReactNode;
}

/*
Use the <%= namePascal %>ContextProvider to wrap your app and set up the <%= namePascal %>StateContext and <%= namePascal %>DispatchContext.
*/

export function <%= namePascal %>ContextProvider({ children }: <%= namePascal %>Provider): JSX.Element {
const [state, dispatch] = useReducer(<%= namePascal %>Reducer, {
<%= namePluralPascal %>: [],
});

return (
<<%= namePascal %>StateContext.Provider value={state}>
<<%= namePascal %>DispatchContext.Provider value={dispatch}>
{children}
</<%= namePascal %>DispatchContext.Provider>
</<%= namePascal %>StateContext.Provider>
);
}

export const use<%= namePascal %>DispatchContext = (): React.Dispatch<Action> =>
useContext(<%= namePascal %>DispatchContext);
export const use<%= namePascal %>StateContext = (): <%= namePascal %>State => useContext(<%= namePascal %>StateContext);
10 changes: 10 additions & 0 deletions app/templates/reactjs/reactjsHook/hooks/use__name__.tsx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Reducer, useReducer } from 'react';

const <%= namePascal %>Reducer = (state: boolean, nextValue?: any) =>
typeof nextValue === 'boolean' ? nextValue : !state;

const use<%= namePascal %> = (initialValue: boolean): [boolean, (nextValue?: any) => void] => {
return useReducer<Reducer<boolean, any>>(<%= namePascal %>Reducer, initialValue);
};

export default use<%= namePascal %>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import { purple } from '@mui/material/colors';

const ColorButton = styled(Button)(({ theme }) => ({
color: theme.palette.getContrastText(purple[500]),
backgroundColor: purple[500],
'&:hover': {
backgroundColor: purple[700],
},
}));

interface Props {
title: string;
}

const <%= namePascal %> = ({title}: Props): JSX.Element => {
const [data, setData] = React.useState<boolean>(false)

const handleClick = React.useCallback(() => {
setData(data => !data)
}, []);

return (
<Stack spacing={2} direction="row">
<ColorButton variant="contained" onClick={handleClick}>
{title}
</ColorButton>
</Stack>
);
}

export default <%= namePascal %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import styled from 'styled-components'

const Button = styled.button`
/* ... */
`

interface Props {
title: string;
}

const <%= namePascal %> = ({title}: Props): JSX.Element => {
const [data, setData] = React.useState<boolean>(false)

const handleClick = React.useCallback(() => {
setData(data => !data)
}, []);

return (
<div>
<Button onClick={handleClick}>
{title}
</Button>
</div>
);
}

export default <%= namePascal %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './'
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-secjs",
"version": "1.2.9",
"version": "1.3.0",
"description": "🧬 Generator for any NodeJS Project or Framework",
"main": "app/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions tests/laravel.spec.js → tests/laravel/default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
describe('\n Laravel 😸', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../app'))
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'laravel' })
.withPrompts({ framework: 'laravel', template: 'default' })
})

it('should create all files from resource Bar in folder Foo', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
describe('\n NestJS Mongoose 😸', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../app'))
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'nestjsMongoose' })
.withPrompts({ framework: 'nestjs', template: 'nestjsMongoose' })
})

it('should create all files from resource Bar in folder Foo', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
describe('\n NestJS PrismaORM 😸', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../app'))
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'nestjsPrismaOrm' })
.withPrompts({ framework: 'nestjs', template: 'nestjsPrismaOrm' })
})

it('should create all files from resource Bar in folder Foo', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const assert = require('yeoman-assert')
describe('\n NestJS TypeORM 😸', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../app'))
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'nestjsTypeOrm' })
.withPrompts({ framework: 'nestjs', template: 'nestjsTypeOrm' })
})

it('should create all files from resource Bar in folder Foo', () => {
Expand Down
22 changes: 22 additions & 0 deletions tests/reactjs/reactjsComponent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const path = require('path')
const helpers = require('yeoman-test')
const assert = require('yeoman-assert')

describe('\n ReactJS Component ⚛️', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'reactjs', template: 'reactjsComponent' })
})

it('should create all files from resource Bar in folder Foo', () => {
assert.file([
'./Foo/components/Bar/component.tsx',
'./Foo/components/Bar/index.tsx',
])
})
})
19 changes: 19 additions & 0 deletions tests/reactjs/reactjsContext.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const path = require('path')
const helpers = require('yeoman-test')
const assert = require('yeoman-assert')

describe('\n ReactJS Context ⚛️', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'reactjs', template: 'reactjsContext' })
})

it('should create all files from resource Bar in folder Foo', () => {
assert.file(['./Foo/contexts/BarContext.tsx'])
})
})
19 changes: 19 additions & 0 deletions tests/reactjs/reactjsHook.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const path = require('path')
const helpers = require('yeoman-test')
const assert = require('yeoman-assert')

describe('\n ReactJS Hook ⚛️', () => {
beforeAll(() => {
return helpers
.run(path.join(__dirname, '../../app'))
.withOptions({ path: './Foo' })
.withArguments('Bar')
.withPrompts({ framework: 'reactjs', template: 'reactjsHook' })
})

it('should create all files from resource Bar in folder Foo', () => {
assert.file(['./Foo/hooks/useBar.tsx'])
})
})
Loading

0 comments on commit 1792a94

Please sign in to comment.