1
1
import chalk from "chalk" ;
2
+ import handlebars from "handlebars" ;
3
+ import hbhComparison from "handlebars-helpers/lib/comparison.js" ;
2
4
import BaseGenerator from "./BaseGenerator.js" ;
3
5
4
- export default class extends BaseGenerator {
6
+ export default class ReactGenerator extends BaseGenerator {
5
7
constructor ( params ) {
6
8
super ( params ) ;
7
9
8
- this . registerTemplates ( "common /" , [
10
+ this . registerTemplates ( "react /" , [
9
11
// utils
10
- "utils/mercure.js" ,
11
- ] ) ;
12
-
13
- this . registerTemplates ( "react-common/" , [
14
- // actions
15
- "actions/foo/create.js" ,
16
- "actions/foo/delete.js" ,
17
- "actions/foo/list.js" ,
18
- "actions/foo/update.js" ,
19
- "actions/foo/show.js" ,
20
-
21
- // utils
22
- "utils/dataAccess.js" ,
23
-
24
- // reducers
25
- "reducers/foo/create.js" ,
26
- "reducers/foo/delete.js" ,
27
- "reducers/foo/index.js" ,
28
- "reducers/foo/list.js" ,
29
- "reducers/foo/update.js" ,
30
- "reducers/foo/show.js" ,
31
- ] ) ;
12
+ "utils/dataAccess.ts" ,
13
+ "utils/types.ts" ,
14
+
15
+ // hooks
16
+ "hooks/create.ts" ,
17
+ "hooks/delete.ts" ,
18
+ "hooks/fetch.ts" ,
19
+ "hooks/index.ts" ,
20
+ "hooks/list.ts" ,
21
+ "hooks/mercure.ts" ,
22
+ "hooks/retrieve.ts" ,
23
+ "hooks/update.ts" ,
24
+ "hooks/show.ts" ,
25
+
26
+ // interfaces
27
+ "interfaces/Collection.ts" ,
28
+ "interfaces/foo.ts" ,
32
29
33
- this . registerTemplates ( `react/` , [
34
30
// components
35
- "components/foo/Create.js" ,
36
- "components/foo/Form.js" ,
37
- "components/foo/index.js" ,
38
- "components/foo/List.js" ,
39
- "components/foo/Update.js" ,
40
- "components/foo/Show.js" ,
31
+ "components/foo/Create.tsx" ,
32
+ "components/foo/Form.tsx" ,
33
+ "components/foo/index.ts" ,
34
+ "components/foo/List.tsx" ,
35
+ "components/foo/Update.tsx" ,
36
+ "components/foo/type.ts" ,
37
+ "components/foo/Show.tsx" ,
38
+ "components/Field.tsx" ,
39
+ "components/Links.tsx" ,
40
+ "components/Pagination.tsx" ,
41
41
42
42
// routes
43
- "routes/foo.js " ,
43
+ "routes/foo.tsx " ,
44
44
] ) ;
45
+
46
+ handlebars . registerHelper ( "compare" , hbhComparison . compare ) ;
45
47
}
46
48
47
49
help ( resource ) {
@@ -52,20 +54,14 @@ export default class extends BaseGenerator {
52
54
resource . title
53
55
) ;
54
56
console . log (
55
- "Paste the following definitions in your application configuration (`client/src/index.js ` by default):"
57
+ "Paste the following definitions in your application configuration (`client/src/index.tsx ` by default):"
56
58
) ;
57
59
console . log (
58
60
chalk . green ( `
59
- // import reducers
60
- import ${ titleLc } from './reducers/${ titleLc } /';
61
-
62
- //import routes
61
+ // import routes
63
62
import ${ titleLc } Routes from './routes/${ titleLc } ';
64
63
65
- // Add the reducer
66
- combineReducers({ ${ titleLc } ,/* ... */ }),
67
-
68
- // Add routes to <Switch>
64
+ // Add routes to <Routes>
69
65
{ ${ titleLc } Routes }
70
66
` )
71
67
) ;
@@ -74,70 +70,87 @@ combineReducers({ ${titleLc},/* ... */ }),
74
70
generate ( api , resource , dir ) {
75
71
const lc = resource . title . toLowerCase ( ) ;
76
72
const ucf = this . ucFirst ( resource . title ) ;
73
+ const fields = this . parseFields ( resource ) ;
77
74
78
75
const context = {
79
- title : resource . title ,
80
76
name : resource . name ,
81
77
lc,
82
78
uc : resource . title . toUpperCase ( ) ,
83
79
ucf,
84
- fields : this . parseFields ( resource ) ,
85
- formFields : this . buildFields ( resource . writableFields ) ,
80
+ fields,
81
+ formFields : this . buildFields ( fields ) ,
82
+ hasRelations : fields . some ( ( field ) => field . reference || field . embedded ) ,
83
+ hasManyRelations : fields . some (
84
+ ( field ) => field . isReferences || field . isEmbeddeds
85
+ ) ,
86
86
hydraPrefix : this . hydraPrefix ,
87
+ title : resource . title ,
87
88
} ;
88
89
89
90
// Create directories
90
91
// These directories may already exist
91
- [ `${ dir } /utils` , `${ dir } /config` , `${ dir } /routes` ] . forEach ( ( dir ) =>
92
- this . createDir ( dir , false )
93
- ) ;
94
-
95
92
[
96
- `${ dir } /actions/${ lc } ` ,
93
+ `${ dir } /utils` ,
94
+ `${ dir } /config` ,
95
+ `${ dir } /interfaces` ,
96
+ `${ dir } /routes` ,
97
97
`${ dir } /components/${ lc } ` ,
98
- `${ dir } /reducers/ ${ lc } ` ,
99
- ] . forEach ( ( dir ) => this . createDir ( dir ) ) ;
98
+ `${ dir } /hooks ` ,
99
+ ] . forEach ( ( dir ) => this . createDir ( dir , false ) ) ;
100
100
101
101
[
102
- // actions
103
- "actions/%s/create.js" ,
104
- "actions/%s/delete.js" ,
105
- "actions/%s/list.js" ,
106
- "actions/%s/update.js" ,
107
- "actions/%s/show.js" ,
108
-
109
102
// components
110
- "components/%s/Create.js" ,
111
- "components/%s/Form.js" ,
112
- "components/%s/index.js" ,
113
- "components/%s/List.js" ,
114
- "components/%s/Update.js" ,
115
- "components/%s/Show.js" ,
116
-
117
- // reducers
118
- "reducers/%s/create.js" ,
119
- "reducers/%s/delete.js" ,
120
- "reducers/%s/index.js" ,
121
- "reducers/%s/list.js" ,
122
- "reducers/%s/update.js" ,
123
- "reducers/%s/show.js" ,
103
+ "components/%s/Create.tsx" ,
104
+ "components/%s/Form.tsx" ,
105
+ "components/%s/index.ts" ,
106
+ "components/%s/List.tsx" ,
107
+ "components/%s/Update.tsx" ,
108
+ "components/%s/type.ts" ,
109
+ "components/%s/Show.tsx" ,
124
110
125
111
// routes
126
- "routes/%s.js " ,
112
+ "routes/%s.tsx " ,
127
113
] . forEach ( ( pattern ) =>
128
114
this . createFileFromPattern ( pattern , dir , lc , context )
129
115
) ;
130
116
131
- // utils
117
+ // interface pattern should be camel cased
132
118
this . createFile (
133
- "utils/dataAccess.js" ,
134
- `${ dir } /utils/dataAccess.js` ,
135
- context ,
136
- false
119
+ "interfaces/foo.ts" ,
120
+ `${ dir } /interfaces/${ context . ucf } .ts` ,
121
+ context
122
+ ) ;
123
+
124
+ // copy with regular name
125
+ [
126
+ // interfaces
127
+ "interfaces/Collection.ts" ,
128
+
129
+ // components
130
+ "components/Field.tsx" ,
131
+ "components/Links.tsx" ,
132
+ "components/Pagination.tsx" ,
133
+
134
+ // hooks
135
+ "hooks/create.ts" ,
136
+ "hooks/delete.ts" ,
137
+ "hooks/fetch.ts" ,
138
+ "hooks/index.ts" ,
139
+ "hooks/list.ts" ,
140
+ "hooks/mercure.ts" ,
141
+ "hooks/retrieve.ts" ,
142
+ "hooks/update.ts" ,
143
+ "hooks/show.ts" ,
144
+
145
+ // utils
146
+ "utils/dataAccess.ts" ,
147
+ "utils/types.ts" ,
148
+ ] . forEach ( ( file ) =>
149
+ this . createFile ( file , `${ dir } /${ file } ` , context , false )
137
150
) ;
138
- this . createFile ( "utils/mercure.js" , `${ dir } /utils/mercure.js` ) ;
139
151
140
- this . createEntrypoint ( api . entrypoint , `${ dir } /config/entrypoint.js` ) ;
152
+ // API config
153
+ this . createEntrypoint ( api . entrypoint , `${ dir } /config/entrypoint.ts` ) ;
141
154
}
142
155
143
156
getDescription ( field ) {
@@ -160,6 +173,7 @@ combineReducers({ ${titleLc},/* ... */ }),
160
173
...list ,
161
174
[ field . name ] : {
162
175
...field ,
176
+ type : this . getType ( field ) ,
163
177
description : this . getDescription ( field ) ,
164
178
readonly : false ,
165
179
isReferences,
@@ -169,7 +183,7 @@ combineReducers({ ${titleLc},/* ... */ }),
169
183
} ;
170
184
} , { } ) ;
171
185
172
- return fields ;
186
+ return Object . values ( fields ) ;
173
187
}
174
188
175
189
ucFirst ( target ) {
0 commit comments