Skip to content

Commit 7c8456a

Browse files
author
Jarek Kuliga
committed
feat: 🎸 add router structure generator
1 parent b1729ff commit 7c8456a

12 files changed

Lines changed: 197 additions & 13 deletions

File tree

‎generators/structure-component/index.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Question } from "inquirer";
22
import camelCase from "lodash/fp/camelCase";
33
import Generator from "yeoman-generator";
44
import { nameQuestion } from "../../src/questions";
5+
import { Config } from "../../src/config/Config";
56

67
interface FileConfigWithStringTemplate {
78
templateName: string;
@@ -26,11 +27,6 @@ interface FileConfigAnswers {
2627
type: ComponentType;
2728
}
2829

29-
interface Config {
30-
styling: "jss" | "styled-components" | "emotion";
31-
rootDir: string;
32-
}
33-
3430
export = class StructureComponentGenerator extends Generator {
3531
private name: string;
3632
private module: string;

‎generators/structure-page/index.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import camelCase from "lodash/fp/camelCase";
22
import toUpper from "lodash/fp/toUpper";
33
import Generator from "yeoman-generator";
4-
54
import { nameQuestion } from "../../src/questions";
5+
import { Config } from "../../src/config/Config";
66

77
interface FileConfig {
88
templateName: string;
99
fileNameSuffix?: string;
1010
}
1111

12-
interface Config {
13-
styling: "jss" | "styled-components" | "emotion";
14-
rootDir: string;
15-
}
16-
1712
export = class StructureApiGenerator extends Generator {
1813
private name: string;
1914
private module: string;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
export = class StructureApiGenerator extends Generator {
13+
private name: string;
14+
private module: string;
15+
private fileConfigs: FileConfig[];
16+
17+
public async prompting() {
18+
const { name } = await this.prompt(
19+
nameQuestion("Router", this.options.name)
20+
);
21+
22+
this.name = this.options.name || name;
23+
}
24+
25+
public makeModule() {
26+
this.module = this.options.module || this.name;
27+
}
28+
29+
public makeConfig() {
30+
this.fileConfigs = this.getFileConfig(this.name);
31+
}
32+
33+
public writing() {
34+
this.fileConfigs.forEach(fileConfig => {
35+
this.fs.copyTpl(
36+
this.templatePath(`${fileConfig.templateName}`),
37+
this.destinationPath(
38+
`./${this.config.get("rootDir")}/${this.module}/router/${this.name}${
39+
fileConfig.fileNameSuffix
40+
}`
41+
),
42+
{
43+
name: this.name,
44+
nameCamelCase: camelCase(this.name),
45+
nameUpperCase: toUpper(this.name)
46+
}
47+
);
48+
});
49+
}
50+
51+
private getFileConfig = (name: string): FileConfig[] => [
52+
{
53+
templateName: `main.tsx`,
54+
fileNameSuffix: "Router.tsx"
55+
},
56+
{
57+
templateName: `route.ts`,
58+
fileNameSuffix: ".route.ts"
59+
},
60+
{
61+
templateName: `data.tsx`,
62+
fileNameSuffix: "Router.data.tsx"
63+
},
64+
{
65+
templateName: `load.tsx`,
66+
fileNameSuffix: "Router.load.tsx"
67+
},
68+
{
69+
templateName: `spec.tsx`,
70+
fileNameSuffix: "Router.spec.tsx"
71+
}
72+
];
73+
};
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 { AppRouterData } from "App/router/AppRouter.data";
3+
4+
export namespace <%= name %>RouterData {
5+
import RouteItem = AppRouterData.RouteItem;
6+
export const ROUTE_ITEMS: RouteItem[] = []
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as React from "react";
2+
import { Loadable } from "App/component/Loadable/Loadable";
3+
4+
const <%= name %>RouterLoadable = Loadable({
5+
loader: () =>
6+
import(
7+
"./<%= name %>Router" /* webpackChunkName: "<%= name %>Router" */
8+
),
9+
render: ({ <%= name %>Router }, props) => (
10+
<<%= name %>Router {...props} />
11+
)
12+
});
13+
14+
export { <%= name %>RouterLoadable };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from "react";
2+
import { FC } from "react";
3+
import { Route, Switch } from "react-router";
4+
import { <%= name %>RouterData } from "./<%= name %>Router.data";
5+
6+
export namespace <%= name %>Router {
7+
export type Props = { };
8+
}
9+
10+
export const <%= name %>Router: FC<<%= name %>Router.Props> = (props) => (
11+
<>
12+
<Switch>
13+
{<%= name %>RouterData.ROUTE_ITEMS.map(route => (
14+
<Route {...route} key={route.path} />
15+
))}
16+
</Switch>
17+
</>
18+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export namespace <%= name %>Route {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from "react";
2+
import { render } from "@testing-library/react";
3+
import { <%= name %>Router } from "./<%= name %>Router";
4+
5+
describe('<<%= name %>Router />', () => {
6+
7+
beforeEach(() => {
8+
const renderer = render(<<%= name %>Router />);
9+
});
10+
11+
});

‎generators/structure/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export = class StructureGenerator extends Generator {
3636
{
3737
name: `Page`,
3838
value: Task.PAGE
39+
},
40+
{
41+
name: `Router`,
42+
value: Task.ROUTER
3943
}
4044
]
4145
}
@@ -50,6 +54,7 @@ export = class StructureGenerator extends Generator {
5054
case Task.MODEL:
5155
case Task.STORE:
5256
case Task.PAGE:
57+
case Task.ROUTER:
5358
this.composeWith(require.resolve(`../structure-inner`), { task });
5459
}
5560
}

‎src/config/Config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Config {
2+
styling: "jss" | "styled-components" | "emotion";
3+
rootDir: string;
4+
}

0 commit comments

Comments
 (0)