@@ -8,45 +8,46 @@ var chalk = require('chalk')
88var  got  =  require ( 'got' ) 
99var  pkg  =  require ( '../package.json' ) 
1010var  jsonServer  =  require ( '../src' ) 
11+ var  jsMockGenerator  =  null 
1112
1213updateNotifier ( { packageName : pkg . name ,  packageVersion : pkg . version } ) . notify ( ) 
1314
1415// Parse arguments 
1516var  argv  =  yargs 
16-   . usage ( '$0 [options] <source>' ) 
17-   . options ( { 
18-     port : { 
19-       alias : 'p' , 
20-       description : 'Set port' , 
21-       default : 3000 
22-     } , 
23-     host : { 
24-       alias : 'H' , 
25-       description : 'Set host' , 
26-       default : '0.0.0.0' 
27-     } , 
28-     watch : { 
29-       alias : 'w' , 
30-       description : 'Reload database on JSON file change' 
31-     } , 
32-     routes : { 
33-       alias : 'r' , 
34-       description : 'Load routes file' 
35-     } , 
36-     id : { 
37-       description : 'Set database id property (e.g. _id)' , 
38-       default : 'id' 
39-     } 
40-   } ) 
41-   . boolean ( 'watch' ) 
42-   . help ( 'help' ) . alias ( 'help' ,  'h' ) 
43-   . version ( pkg . version ) . alias ( 'version' ,  'v' ) 
44-   . example ( '$0 db.json' ,  '' ) 
45-   . example ( '$0 file.js' ,  '' ) 
46-   . example ( '$0 http://example.com/db.json' ,  '' ) 
47-   . epilog ( 'https://github.com/typicode/json-server' ) 
48-   . require ( 1 ,  'Missing <source> argument' ) 
49-   . argv 
17+      . usage ( '$0 [options] <source>' ) 
18+      . options ( { 
19+        port : { 
20+          alias : 'p' , 
21+          description : 'Set port' , 
22+          default : 3000 
23+        } , 
24+        host : { 
25+          alias : 'H' , 
26+          description : 'Set host' , 
27+          default : '0.0.0.0' 
28+        } , 
29+        watch : { 
30+          alias : 'w' , 
31+          description : 'Reload database on JSON file change' 
32+        } , 
33+        routes : { 
34+          alias : 'r' , 
35+          description : 'Load routes file' 
36+        } , 
37+        id : { 
38+          description : 'Set database id property (e.g. _id)' , 
39+          default : 'id' 
40+        } 
41+      } ) 
42+      . boolean ( 'watch' ) 
43+      . help ( 'help' ) . alias ( 'help' ,  'h' ) 
44+      . version ( pkg . version ) . alias ( 'version' ,  'v' ) 
45+      . example ( '$0 db.json' ,  '' ) 
46+      . example ( '$0 file.js' ,  '' ) 
47+      . example ( '$0 http://example.com/db.json' ,  '' ) 
48+      . epilog ( 'https://github.com/typicode/json-server' ) 
49+      . require ( 1 ,  'Missing <source> argument' ) 
50+      . argv 
5051
5152function  showResources  ( hostname ,  port ,  object )  { 
5253  for  ( var  prop  in  object )  { 
@@ -62,11 +63,11 @@ function start (object, filename) {
6263  showResources ( hostname ,  port ,  object ) 
6364  console . log ( ) 
6465  console . log ( 
65-     'You can now go to '  +  chalk . gray ( 'http://'  +  hostname  +  ':'  +  port ) 
66+        'You can now go to '  +  chalk . gray ( 'http://'  +  hostname  +  ':'  +  port ) 
6667  ) 
6768  console . log ( ) 
6869  console . log ( 
69-     'Enter '  +  chalk . cyan ( 's' )  +  ' at any time to create a snapshot of the db' 
70+        'Enter '  +  chalk . cyan ( 's' )  +  ' at any time to create a snapshot of the db' 
7071  ) 
7172
7273  // Snapshot 
@@ -84,7 +85,7 @@ function start (object, filename) {
8485  var  router  =  jsonServer . router ( filename  ? filename  : object ) 
8586
8687  // Watcher 
87-   if  ( filename   &&   argv . watch )  { 
88+   if  ( argv . watch )  { 
8889    console . log ( 'Watching' ,  chalk . cyan ( source ) ) 
8990
9091    var  db  =  router . db 
@@ -94,12 +95,16 @@ function start (object, filename) {
9495    fs . watch ( watchedDir ,  function  ( event ,  changedFile )  { 
9596      // lowdb generates 'rename' event on watchedFile 
9697      // using it to know if file has been modified by the user 
97-       if  ( event  ===  'change'  &&  changedFile  ===  watchedFile )  { 
98+       if  ( ( event  ===  'change'  ||   event   ===   'rename' )   &&  ( changedFile  ===  watchedFile   ||   changedFile   ===   source ) )  { 
9899        console . log ( chalk . cyan ( source ) ,  'has changed, reloading database' ) 
99100
100101        try  { 
101-           var  watchedFileObject  =  JSON . parse ( fs . readFileSync ( filename ) ) 
102-           db . object  =  watchedFileObject 
102+           if  ( filename )  { 
103+             db . object  =  JSON . parse ( fs . readFileSync ( filename ) ) 
104+           }  else  { 
105+             require . cache [ jsMockGenerator ]  =  null 
106+             db . object  =  require ( jsMockGenerator ) ( ) 
107+           } 
103108          showResources ( hostname ,  port ,  db . object ) 
104109        }  catch  ( e )  { 
105110          console . log ( 'Can\'t parse' ,  chalk . cyan ( source ) ) 
@@ -154,6 +159,7 @@ if (/^(http|https):/.test(source)) {
154159  start ( object ,  filename ) 
155160// JS file 
156161}  else  if  ( / \. j s $ / . test ( source ) )  { 
157-   var  object  =  require ( process . cwd ( )  +  '/'  +  source ) ( ) 
162+   jsMockGenerator  =  process . cwd ( )  +  '/'  +  source 
163+   var  object  =  require ( jsMockGenerator ) ( ) 
158164  start ( object ) 
159165} 
0 commit comments