@@ -9,12 +9,6 @@ const is = require('./utils/is')
99const load = require ( './utils/load' )
1010const jsonServer = require ( '../server' )
1111
12- const example = {
13- posts : [ { id : 1 , title : 'json-server' , author : 'typicode' } ] ,
14- comments : [ { id : 1 , body : 'some comment' , postId : 1 } ] ,
15- profile : { name : 'typicode' }
16- }
17-
1812function prettyPrint ( argv , object , rules ) {
1913 const host = argv . host === '0.0.0.0' ? 'localhost' : argv . host
2014 const port = argv . port
@@ -40,22 +34,15 @@ function prettyPrint(argv, object, rules) {
4034 console . log ( )
4135}
4236
43- function createApp ( source , object , routes , middlewares , argv ) {
37+ function createApp ( db , routes , middlewares , argv ) {
4438 const app = jsonServer . create ( )
4539
46- let router
47-
4840 const { foreignKeySuffix } = argv
49- try {
50- router = jsonServer . router (
51- is . JSON ( source ) ? source : object ,
52- foreignKeySuffix ? { foreignKeySuffix } : undefined
53- )
54- } catch ( e ) {
55- console . log ( )
56- console . error ( chalk . red ( e . message . replace ( / ^ / gm, ' ' ) ) )
57- process . exit ( 1 )
58- }
41+
42+ let router = jsonServer . router (
43+ db ,
44+ foreignKeySuffix ? { foreignKeySuffix } : undefined
45+ )
5946
6047 const defaultsOpts = {
6148 logger : ! argv . quiet ,
@@ -113,20 +100,10 @@ module.exports = function(argv) {
113100 function start ( cb ) {
114101 console . log ( )
115102
116- // Be nice and create a default db.json if it doesn't exist
117- if ( is . JSON ( source ) && ! fs . existsSync ( source ) ) {
118- console . log ( chalk . yellow ( ` Oops, ${ source } doesn't seem to exist` ) )
119- console . log ( chalk . yellow ( ` Creating ${ source } with some default data` ) )
120- console . log ( )
121- fs . writeFileSync ( source , JSON . stringify ( example , null , 2 ) )
122- }
123-
124103 console . log ( chalk . gray ( ' Loading' , source ) )
125104
126- // Load JSON, JS or HTTP database
127- load ( source , ( err , data ) => {
128- if ( err ) throw err
129-
105+ // create db and load object, JSON file, JS or HTTP database
106+ return load ( source ) . then ( db => {
130107 // Load additional routes
131108 let routes
132109 if ( argv . routes ) {
@@ -147,109 +124,114 @@ module.exports = function(argv) {
147124 console . log ( chalk . gray ( ' Done' ) )
148125
149126 // Create app and server
150- app = createApp ( source , data , routes , middlewares , argv )
127+ app = createApp ( db , routes , middlewares , argv )
151128 server = app . listen ( argv . port , argv . host )
152129
153130 // Enhance with a destroy function
154131 enableDestroy ( server )
155132
156133 // Display server informations
157- prettyPrint ( argv , data , routes )
158-
159- cb && cb ( )
134+ prettyPrint ( argv , db . getState ( ) , routes )
160135 } )
161136 }
162137
163138 // Start server
164- start ( ( ) => {
165- // Snapshot
166- console . log (
167- chalk . gray (
168- ' Type s + enter at any time to create a snapshot of the database'
169- )
170- )
171-
172- // Support nohup
173- // https://github.com/typicode/json-server/issues/221
174- process . stdin . on ( 'error' , ( ) => {
175- console . log ( ` Error, can't read from stdin` )
176- console . log ( ` Creating a snapshot from the CLI won't be possible` )
177- } )
178- process . stdin . setEncoding ( 'utf8' )
179- process . stdin . on ( 'data' , chunk => {
180- if ( chunk . trim ( ) . toLowerCase ( ) === 's' ) {
181- const filename = `db-${ Date . now ( ) } .json`
182- const file = path . join ( argv . snapshots , filename )
183- const state = app . db . getState ( )
184- fs . writeFileSync ( file , JSON . stringify ( state , null , 2 ) , 'utf-8' )
185- console . log (
186- ` Saved snapshot to ${ path . relative ( process . cwd ( ) , file ) } \n`
139+ start ( )
140+ . then ( ( ) => {
141+ // Snapshot
142+ console . log (
143+ chalk . gray (
144+ ' Type s + enter at any time to create a snapshot of the database'
187145 )
188- }
189- } )
190-
191- // Watch files
192- if ( argv . watch ) {
193- console . log ( chalk . gray ( ' Watching...' ) )
194- console . log ( )
195- const source = argv . _ [ 0 ]
196-
197- // Can't watch URL
198- if ( is . URL ( source ) ) throw new Error ( "Can't watch URL" )
199-
200- // Watch .js or .json file
201- // Since lowdb uses atomic writing, directory is watched instead of file
202- const watchedDir = path . dirname ( source )
203- let readError = false
204- fs . watch ( watchedDir , ( event , file ) => {
205- // https://github.com/typicode/json-server/issues/420
206- // file can be null
207- if ( file ) {
208- const watchedFile = path . resolve ( watchedDir , file )
209- if ( watchedFile === path . resolve ( source ) ) {
210- if ( is . JSON ( watchedFile ) ) {
211- let obj
212- try {
213- obj = jph . parse ( fs . readFileSync ( watchedFile ) )
214- if ( readError ) {
215- console . log ( chalk . green ( ` Read error has been fixed :)` ) )
216- readError = false
217- }
218- } catch ( e ) {
219- readError = true
220- console . log ( chalk . red ( ` Error reading ${ watchedFile } ` ) )
221- console . error ( e . message )
222- return
223- }
146+ )
224147
225- // Compare .json file content with in memory database
226- const isDatabaseDifferent = ! _ . isEqual ( obj , app . db . getState ( ) )
227- if ( isDatabaseDifferent ) {
228- console . log ( chalk . gray ( ` ${ source } has changed, reloading...` ) )
229- server && server . destroy ( )
230- start ( )
231- }
232- }
233- }
148+ // Support nohup
149+ // https://github.com/typicode/json-server/issues/221
150+ process . stdin . on ( 'error' , ( ) => {
151+ console . log ( ` Error, can't read from stdin` )
152+ console . log ( ` Creating a snapshot from the CLI won't be possible` )
153+ } )
154+ process . stdin . setEncoding ( 'utf8' )
155+ process . stdin . on ( 'data' , chunk => {
156+ if ( chunk . trim ( ) . toLowerCase ( ) === 's' ) {
157+ const filename = `db-${ Date . now ( ) } .json`
158+ const file = path . join ( argv . snapshots , filename )
159+ const state = app . db . getState ( )
160+ fs . writeFileSync ( file , JSON . stringify ( state , null , 2 ) , 'utf-8' )
161+ console . log (
162+ ` Saved snapshot to ${ path . relative ( process . cwd ( ) , file ) } \n`
163+ )
234164 }
235165 } )
236166
237- // Watch routes
238- if ( argv . routes ) {
239- const watchedDir = path . dirname ( argv . routes )
167+ // Watch files
168+ if ( argv . watch ) {
169+ console . log ( chalk . gray ( ' Watching...' ) )
170+ console . log ( )
171+ const source = argv . _ [ 0 ]
172+
173+ // Can't watch URL
174+ if ( is . URL ( source ) ) throw new Error ( "Can't watch URL" )
175+
176+ // Watch .js or .json file
177+ // Since lowdb uses atomic writing, directory is watched instead of file
178+ const watchedDir = path . dirname ( source )
179+ let readError = false
240180 fs . watch ( watchedDir , ( event , file ) => {
181+ // https://github.com/typicode/json-server/issues/420
182+ // file can be null
241183 if ( file ) {
242184 const watchedFile = path . resolve ( watchedDir , file )
243- if ( watchedFile === path . resolve ( argv . routes ) ) {
244- console . log (
245- chalk . gray ( ` ${ argv . routes } has changed, reloading...` )
246- )
247- server && server . destroy ( )
248- start ( )
185+ if ( watchedFile === path . resolve ( source ) ) {
186+ if ( is . FILE ( watchedFile ) ) {
187+ let obj
188+ try {
189+ obj = jph . parse ( fs . readFileSync ( watchedFile ) )
190+ if ( readError ) {
191+ console . log ( chalk . green ( ` Read error has been fixed :)` ) )
192+ readError = false
193+ }
194+ } catch ( e ) {
195+ readError = true
196+ console . log ( chalk . red ( ` Error reading ${ watchedFile } ` ) )
197+ console . error ( e . message )
198+ return
199+ }
200+
201+ // Compare .json file content with in memory database
202+ const isDatabaseDifferent = ! _ . isEqual ( obj , app . db . getState ( ) )
203+ if ( isDatabaseDifferent ) {
204+ console . log (
205+ chalk . gray ( ` ${ source } has changed, reloading...` )
206+ )
207+ server && server . destroy ( )
208+ start ( )
209+ }
210+ }
249211 }
250212 }
251213 } )
214+
215+ // Watch routes
216+ if ( argv . routes ) {
217+ const watchedDir = path . dirname ( argv . routes )
218+ fs . watch ( watchedDir , ( event , file ) => {
219+ if ( file ) {
220+ const watchedFile = path . resolve ( watchedDir , file )
221+ if ( watchedFile === path . resolve ( argv . routes ) ) {
222+ console . log (
223+ chalk . gray ( ` ${ argv . routes } has changed, reloading...` )
224+ )
225+ server && server . destroy ( )
226+ start ( )
227+ }
228+ }
229+ } )
230+ }
252231 }
253- }
254- } )
232+ } )
233+ . catch ( err => {
234+ console . log ( err )
235+ process . exit ( 1 )
236+ } )
255237}
0 commit comments