11import chalk from "chalk" ;
2- import fs from "fs" ;
32import BaseGenerator from "./BaseGenerator" ;
4- import { parse , print , types } from "recast" ;
53
64export default class NextGenerator extends BaseGenerator {
75 constructor ( params ) {
@@ -23,86 +21,22 @@ export default class NextGenerator extends BaseGenerator {
2321 "interfaces/foo.ts" ,
2422
2523 // pages
26- "pages/foo .tsx" ,
27- "pages/foos.tsx" ,
24+ "pages/foos/[id] .tsx" ,
25+ "pages/foos/index .tsx" ,
2826
2927 // utils
3028 "utils/dataAccess.ts"
3129 ] ) ;
3230 }
3331
34- checkDependencies ( dir , serverPath ) {
35- const dependencies = this . getTargetDependencies ( dir ) ;
36-
37- if ( ! dependencies . length ) {
38- return ;
39- }
40-
41- if ( ! dependencies . includes ( "@zeit/next-typescript" ) ) {
42- console . log (
43- chalk . yellow (
44- "It seems next-typescript is not installed but generator needs typescript to work efficiently."
45- )
46- ) ;
47- }
48-
49- if ( ! dependencies . includes ( "express" ) ) {
50- console . log (
51- chalk . yellow (
52- "It seems express is not installed but generator needs a custom express server to work efficiently."
53- )
54- ) ;
55- }
56-
57- if ( serverPath ) {
58- if ( ! fs . existsSync ( serverPath ) ) {
59- console . log ( chalk . red ( "Express server file doesn't exists." ) ) ;
60- return ;
61- }
62-
63- const { mode } = fs . statSync ( serverPath ) ;
64- if ( "200" !== ( mode & parseInt ( "200" , 8 ) ) . toString ( 8 ) ) {
65- console . log ( chalk . red ( "Express server file is not writable." ) ) ;
66- }
67- }
68- }
69-
70- checkImports ( directory , imports , extension = ".ts" ) {
71- imports . forEach ( ( { file } ) => {
72- if ( fs . existsSync ( directory + file + extension ) ) {
73- return ;
74- }
75-
76- console . log (
77- chalk . yellow (
78- 'An import for the file "%s" has been generated but the file doesn\'t exists.'
79- ) ,
80- file
81- ) ;
82- } ) ;
83- }
84-
85- help ( resource , dir ) {
32+ help ( resource ) {
8633 console . log (
8734 chalk . green ( 'Code for the "%s" resource type has been generated!' ) ,
8835 resource . title
8936 ) ;
90-
91- // missing import
92- const { imports } = this . parseFields ( resource ) ;
93- this . checkImports ( `${ dir } /interfaces/` , imports ) ;
94-
95- // server route configuration
96- if ( ! this . routeAddedtoServer ) {
97- const lc = resource . title . toLowerCase ( ) ;
98- console . log (
99- "Paste the following route to your server configuration file:"
100- ) ;
101- console . log ( chalk . green ( this . getShowRoute ( lc ) ) ) ;
102- }
10337 }
10438
105- generate ( api , resource , dir , serverPath ) {
39+ generate ( api , resource , dir ) {
10640 const lc = resource . title . toLowerCase ( ) ;
10741 const ucf = this . ucFirst ( resource . title ) ;
10842 const { fields, imports } = this . parseFields ( resource ) ;
@@ -113,7 +47,7 @@ export default class NextGenerator extends BaseGenerator {
11347 uc : resource . title . toUpperCase ( ) ,
11448 ucf,
11549 fields,
116- formFields : this . buildFields ( resource . writableFields ) ,
50+ formFields : this . buildFields ( fields ) ,
11751 imports,
11852 hydraPrefix : this . hydraPrefix ,
11953 title : resource . title
@@ -132,15 +66,16 @@ export default class NextGenerator extends BaseGenerator {
13266
13367 // copy with patterned name
13468 this . createDir ( `${ dir } /components/${ context . lc } ` ) ;
69+ this . createDir ( `${ dir } /pages/${ context . lc } s` ) ;
13570 [
13671 // components
13772 "components/%s/List.tsx" ,
13873 "components/%s/ListItem.tsx" ,
13974 "components/%s/Show.tsx" ,
14075
14176 // pages
142- "pages/%s .tsx" ,
143- "pages/%ss.tsx"
77+ "pages/%ss/[id] .tsx" ,
78+ "pages/%ss/index .tsx"
14479 ] . forEach ( pattern =>
14580 this . createFileFromPattern ( pattern , dir , context . lc , context )
14681 ) ;
@@ -169,48 +104,6 @@ export default class NextGenerator extends BaseGenerator {
169104
170105 // API config
171106 this . createEntrypoint ( api . entrypoint , `${ dir } /config/entrypoint.ts` ) ;
172-
173- if ( serverPath ) {
174- this . createExpressRoute ( serverPath , lc , this . getShowRoute ( lc ) ) ;
175- }
176- }
177-
178- getShowRoute ( name ) {
179- return `server.get('/${ name } /:id', (req, res) => {
180- return app.render(req, res, '/${ name } ', { id: req.params.id })
181- });` ;
182- }
183-
184- createExpressRoute ( path , resourceName , toInsert ) {
185- const content = fs . readFileSync ( path , "utf-8" ) ;
186- const code = parse ( content ) ;
187- const { namedTypes } = types ;
188-
189- types . visit ( code , {
190- visitExpressionStatement : function ( path ) {
191- const args = path . value . expression . arguments ;
192- if (
193- 2 === args . length &&
194- namedTypes . Literal . check ( args [ 0 ] ) &&
195- "*" === args [ 0 ] . value &&
196- namedTypes . ArrowFunctionExpression . check ( args [ 1 ] )
197- ) {
198- // insert route before "*" route
199- path . parent . value . body . splice ( path . name , 0 , toInsert ) ;
200-
201- return false ;
202- }
203-
204- this . traverse ( path ) ;
205- }
206- } ) ;
207-
208- fs . writeFileSync ( path , print ( code ) . code ) ;
209- console . log (
210- chalk . green ( "'Show' route for %s has been added to your server" ) ,
211- resourceName
212- ) ;
213- this . routeAddedtoServer = true ;
214107 }
215108
216109 getDescription ( field ) {
0 commit comments