Skip to content

Commit 2cecb79

Browse files
author
Jarek Kuliga
committed
feat: 🎸 add page structure generator
1 parent f2a6ffe commit 2cecb79

13 files changed

Lines changed: 239 additions & 2 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react";
2+
import { render } from "@testing-library/react";
3+
import { <%= name %> } from "./<%= name %>";
4+
5+
describe('<<%= name %> />', () => {
6+
7+
it('should render <<%= name %> />', () => {
8+
const { container } = render(<<%= name %> />);
9+
expect(container).toMatchSnapshot();
10+
});
11+
12+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from "react";
2+
import { storiesOf } from '@storybook/react';
3+
import { action } from '@storybook/addon-actions';
4+
import { <%= name %> } from "./<%= name %>";
5+
6+
storiesOf('./<%= name %>', module)
7+
.add('<%= name %>', () => <<%= name %> />);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import camelCase from "lodash/fp/camelCase";
2+
import toUpper from "lodash/fp/toUpper";
3+
import Generator from "yeoman-generator";
4+
5+
import { nameQuestion } from "../../src/questions";
6+
7+
interface FileConfig {
8+
templateName: string;
9+
fileNameSuffix?: string;
10+
}
11+
12+
interface Config {
13+
styling: "jss" | "styled-components" | "emotion";
14+
rootDir: string;
15+
}
16+
17+
export = class StructureApiGenerator extends Generator {
18+
private name: string;
19+
private module: string;
20+
private fileConfigs: FileConfig[];
21+
22+
public async prompting() {
23+
const { name } = await this.prompt(nameQuestion("Page", this.options.name));
24+
25+
this.name = this.options.name || name;
26+
}
27+
28+
public makeModule() {
29+
this.module = this.options.module || this.name;
30+
}
31+
32+
public makeConfig() {
33+
this.fileConfigs = this.getFileConfig(this.config.getAll() as Config);
34+
}
35+
36+
public writing() {
37+
this.fileConfigs.forEach(fileConfig => {
38+
this.fs.copyTpl(
39+
this.templatePath(`${fileConfig.templateName}`),
40+
this.destinationPath(
41+
`./${this.config.get("rootDir")}/${this.module}/page/${
42+
this.name
43+
}Page/${this.name}Page.${fileConfig.fileNameSuffix}`
44+
),
45+
{
46+
name: this.name,
47+
nameCamelCase: camelCase(this.name),
48+
nameUpperCase: toUpper(this.name)
49+
}
50+
);
51+
});
52+
}
53+
54+
private getFileConfig = (config: Config): FileConfig[] => [
55+
{
56+
templateName: `main.tsx`,
57+
fileNameSuffix: "tsx"
58+
},
59+
{
60+
templateName: `load.tsx`,
61+
fileNameSuffix: "load.tsx"
62+
},
63+
{
64+
templateName: `style.${config.styling}.ts`,
65+
fileNameSuffix: "style.ts"
66+
},
67+
{
68+
templateName: `spec.tsx`,
69+
fileNameSuffix: "spec.tsx"
70+
}
71+
];
72+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as React from "react";
2+
import { Loadable } from "App/component/Loadable/Loadable";
3+
import { <%= name %>Page as <%= name %>PageType } from "./<%= name %>Page";
4+
5+
export const <%= name %>PageLoadable = Loadable<<%= name %>PageType.Props>({
6+
loader: () => import("./<%= name %>Page" /* webpackChunkName: "<%= name %>Page" */),
7+
render: ({ <%= name %>Page }, props) => <<%= name %>Page {...props} />
8+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as React from 'react';
2+
import { FC } from 'react';
3+
4+
export namespace <%= name %>Page {
5+
export type Props = { };
6+
}
7+
8+
export const <%= name %>Page: FC<<%= name %>Page.Props> = (props) => {
9+
return null;
10+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react";
2+
import { render } from "@testing-library/react";
3+
import { <%= name %>Page } from "./<%= name %>Page";
4+
5+
describe('<<%= name %>Page />', () => {
6+
7+
it('should render <<%= name %>Page />', () => {
8+
const { container } = render(<<%= name %>Page />);
9+
expect(container).toMatchSnapshot();
10+
});
11+
12+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styled from '@emotion/styled';
2+
3+
export const Styled = styled.div``;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styled from 'jss';
2+
3+
export const Styled = styled.div``;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styled from 'styled-components';
2+
3+
export const Styled = styled.div``;

‎generators/structure/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export = class StructureGenerator extends Generator {
3232
{
3333
name: `Store`,
3434
value: Task.STORE
35+
},
36+
{
37+
name: `Page`,
38+
value: Task.PAGE
3539
}
3640
]
3741
}
@@ -45,6 +49,7 @@ export = class StructureGenerator extends Generator {
4549
case Task.API:
4650
case Task.MODEL:
4751
case Task.STORE:
52+
case Task.PAGE:
4853
this.composeWith(require.resolve(`../structure-inner`), { task });
4954
}
5055
}

0 commit comments

Comments
 (0)